Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Simplify the project acts as customizable patch, load all the field data in one query and sort it via sql instead of ruby.

Dombi Attilab5866a620af0
diff --git a/app/components/projects/settings/project_custom_field_sections/show_component.html.erb b/app/components/projects/settings/project_custom_field_sections/show_component.html.erb
index 78f28405247..3571e88b9e7 100644
--- a/app/components/projects/settings/project_custom_field_sections/show_component.html.erb
+++ b/app/components/projects/settings/project_custom_field_sections/show_component.html.erb
@@ -59,7 +59,7 @@
render(Primer::Beta::Text.new(color: :subtle)) { t("settings.project_attributes.label_no_project_custom_fields") }
end
else
- sorted_project_custom_fields.each do |project_custom_field|
+ @project_custom_fields.each do |project_custom_field|
component.with_row(data: { 'projects--settings--project-custom-fields-mapping-filter-target': 'searchItem' }) do
render(Projects::Settings::ProjectCustomFieldSections::CustomFieldRowComponent.new(
project: @project,
diff --git a/app/components/projects/settings/project_custom_field_sections/show_component.rb b/app/components/projects/settings/project_custom_field_sections/show_component.rb
index beb8c9dee73..a1ef3289deb 100644
--- a/app/components/projects/settings/project_custom_field_sections/show_component.rb
+++ b/app/components/projects/settings/project_custom_field_sections/show_component.rb
@@ -53,10 +53,6 @@ module Projects
def wrapper_uniq_by
@project_custom_field_section.id
end
-
- def sorted_project_custom_fields
- @project_custom_fields.sort_by { |pcf| pcf.position_in_custom_field_section }
- end
end
end
end
diff --git a/app/controllers/projects/settings/project_custom_fields_controller.rb b/app/controllers/projects/settings/project_custom_fields_controller.rb
index caec7e41d2b..b4c3e7527a0 100644
--- a/app/controllers/projects/settings/project_custom_fields_controller.rb
+++ b/app/controllers/projects/settings/project_custom_fields_controller.rb
@@ -89,7 +89,7 @@ class Projects::Settings::ProjectCustomFieldsController < Projects::SettingsCont
.includes(:custom_fields)
.merge(ProjectCustomField.visible)
.group(:id, "custom_fields.id")
- .order(:position)
+ .order(:position, :position_in_custom_field_section)
@project_custom_field_project_mappings = ProjectCustomFieldProjectMapping
.where(project_id: @project.id)
diff --git a/app/forms/projects/custom_fields/form.rb b/app/forms/projects/custom_fields/form.rb
index 5d4c00542f7..3a7ba99892b 100644
--- a/app/forms/projects/custom_fields/form.rb
+++ b/app/forms/projects/custom_fields/form.rb
@@ -28,7 +28,7 @@
module Projects::CustomFields
class Form < ApplicationForm
form do |custom_fields_form|
- sorted_custom_fields.each do |custom_field|
+ custom_fields.each do |custom_field|
custom_fields_form.fields_for(:custom_field_values) do |builder|
custom_field_input(builder, custom_field)
end
@@ -50,16 +50,17 @@ module Projects::CustomFields
private
- def sorted_custom_fields
- return @custom_fields if @custom_fields.present?
-
- @custom_fields = if @custom_field.present?
- [@custom_field]
- elsif @custom_field_section.present?
- @project.sorted_available_custom_fields_by_section(@custom_field_section)
- else
- @project.sorted_available_custom_fields
- end
+ def custom_fields
+ @custom_fields ||=
+ if @custom_field.present?
+ [@custom_field]
+ elsif @custom_field_section.present?
+ @project
+ .available_custom_fields
+ .where(custom_field_section: @custom_field_section)
+ else
+ @project.available_custom_fields
+ end
end
def custom_field_input(builder, custom_field)
diff --git a/app/models/projects/acts_as_customizable_patches.rb b/app/models/projects/acts_as_customizable_patches.rb
index cde7c21725f..41441fe6cc7 100644
--- a/app/models/projects/acts_as_customizable_patches.rb
+++ b/app/models/projects/acts_as_customizable_patches.rb
@@ -120,6 +120,7 @@ module Projects::ActsAsCustomizablePatches
custom_fields = ProjectCustomField
.visible
.includes(:project_custom_field_section)
+ .order("custom_field_sections.position", :position_in_custom_field_section)
# available_custom_fields is called from within the acts_as_customizable module
# we don't want to adjust these calls, but need a way to query the available custom fields on a global level in some cases
@@ -137,22 +138,6 @@ module Projects::ActsAsCustomizablePatches
custom_fields
end
- def available_project_custom_fields_grouped_by_section
- sorted_available_custom_fields
- .group_by(&:custom_field_section_id)
- end
-
- def sorted_available_custom_fields
- available_custom_fields
- .sort_by { |pcf| [pcf.project_custom_field_section&.position, pcf.position_in_custom_field_section] }
- end
-
- def sorted_available_custom_fields_by_section(section)
- available_custom_fields
- .where(custom_field_section_id: section.id)
- .sort_by(&:position_in_custom_field_section)
- end
-
def validate_custom_values
# validate custom values only of a specified section
# instead of validating ALL custom values like done in acts_as_customizable
diff --git a/modules/overviews/app/components/project_custom_fields/sections/show_component.html.erb b/modules/overviews/app/components/project_custom_fields/sections/show_component.html.erb
index 57be4beb911..b42340b6e21 100644
--- a/modules/overviews/app/components/project_custom_fields/sections/show_component.html.erb
+++ b/modules/overviews/app/components/project_custom_fields/sections/show_component.html.erb
@@ -25,7 +25,7 @@
end
end
- sorted_project_custom_fields.each do |project_custom_field|
+ @project_custom_fields.each do |project_custom_field|
details_container.with_row(mb: 3) do
render(ProjectCustomFields::Sections::ProjectCustomFields::ShowComponent.new(
project_custom_field: project_custom_field,
diff --git a/modules/overviews/app/components/project_custom_fields/sections/show_component.rb b/modules/overviews/app/components/project_custom_fields/sections/show_component.rb
index e3352cad3e9..bce4f3bf995 100644
--- a/modules/overviews/app/components/project_custom_fields/sections/show_component.rb
+++ b/modules/overviews/app/components/project_custom_fields/sections/show_component.rb
@@ -60,10 +60,6 @@ module ProjectCustomFields
.to_a
end
- def sorted_project_custom_fields
- @project_custom_fields.sort_by { |pcf| pcf.position_in_custom_field_section }
- end
-
def get_eager_loaded_project_custom_field_values_for(custom_field_id)
@eager_loaded_project_custom_field_values.select { |pcfv| pcfv.custom_field_id == custom_field_id }
end
diff --git a/modules/overviews/app/components/project_custom_fields/sidebar_component.html.erb b/modules/overviews/app/components/project_custom_fields/sidebar_component.html.erb
index de5672b3b16..6f8f746c66e 100644
--- a/modules/overviews/app/components/project_custom_fields/sidebar_component.html.erb
+++ b/modules/overviews/app/components/project_custom_fields/sidebar_component.html.erb
@@ -2,11 +2,11 @@
component_wrapper do
if available_project_custom_fields_grouped_by_section.any?
flex_layout(data: { qa_selector: "project-custom-fields-sidebar-async-content" }) do |sections_container|
- available_project_custom_fields_grouped_by_section.each do |project_custom_field_section_id, project_custom_fields|
+ available_project_custom_fields_grouped_by_section.each do |project_custom_field_section, project_custom_fields|
sections_container.with_row(mb: 3) do
render(ProjectCustomFields::Sections::ShowComponent.new(
project: @project,
- project_custom_field_section: get_eager_loaded_project_custom_field_section(project_custom_field_section_id),
+ project_custom_field_section: ,
project_custom_fields: project_custom_fields
))
end
diff --git a/modules/overviews/app/components/project_custom_fields/sidebar_component.rb b/modules/overviews/app/components/project_custom_fields/sidebar_component.rb
index 2b6731ca762..73350be6ecd 100644
--- a/modules/overviews/app/components/project_custom_fields/sidebar_component.rb
+++ b/modules/overviews/app/components/project_custom_fields/sidebar_component.rb
@@ -32,21 +32,16 @@ module ProjectCustomFields
include OpPrimer::ComponentHelpers
include OpTurbo::Streamable
- def initialize(project:, eager_loaded_project_custom_field_sections:)
+ def initialize(project:)
super
@project = project
- @eager_loaded_project_custom_field_sections = eager_loaded_project_custom_field_sections
end
private
-
- def get_eager_loaded_project_custom_field_section(project_custom_field_section_id)
- @eager_loaded_project_custom_field_sections.find { |pcfs| pcfs.id == project_custom_field_section_id }
- end
-
def available_project_custom_fields_grouped_by_section
- @available_project_custom_fields_grouped_by_section ||= @project.available_project_custom_fields_grouped_by_section
+ @available_project_custom_fields_grouped_by_section ||=
+ @project.available_custom_fields.group_by(&:project_custom_field_section)
end
end
end
diff --git a/modules/overviews/app/controllers/overviews/overviews_controller.rb b/modules/overviews/app/controllers/overviews/overviews_controller.rb
index 9bd492f62c7..386b86988a5 100644
--- a/modules/overviews/app/controllers/overviews/overviews_controller.rb
+++ b/modules/overviews/app/controllers/overviews/overviews_controller.rb
@@ -8,8 +8,6 @@ module ::Overviews
menu_item :overview
def project_custom_fields_sidebar
- @eager_loaded_project_custom_field_sections = eager_loaded_project_custom_field_sections
-
render :project_custom_fields_sidebar, layout: false
end
@@ -70,15 +68,8 @@ module ::Overviews
def update_sidebar_component
update_via_turbo_stream(
- component: ProjectCustomFields::SidebarComponent.new(
- project: @project,
- eager_loaded_project_custom_field_sections:
- )
+ component: ProjectCustomFields::SidebarComponent.new(project: @project)
)
end
-
- def eager_loaded_project_custom_field_sections
- ProjectCustomFieldSection.all.to_a
- end
end
end
diff --git a/modules/overviews/app/views/overviews/overviews/project_custom_fields_sidebar.html.erb b/modules/overviews/app/views/overviews/overviews/project_custom_fields_sidebar.html.erb
index f156037180d..33deb6558c3 100644
--- a/modules/overviews/app/views/overviews/overviews/project_custom_fields_sidebar.html.erb
+++ b/modules/overviews/app/views/overviews/overviews/project_custom_fields_sidebar.html.erb
@@ -27,8 +27,5 @@ See COPYRIGHT and LICENSE files for more details.
++#%>
<%= content_tag("turbo-frame", id: "project-custom-fields-sidebar") do %>
- <%= render(ProjectCustomFields::SidebarComponent.new(
- project: @project,
- eager_loaded_project_custom_field_sections: @eager_loaded_project_custom_field_sections
- )) %>
+ <%= render(ProjectCustomFields::SidebarComponent.new(project: @project)) %>
<% end %>
\ No newline at end of file