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

refactored project attributes sidebar and dialog following previous refactorings

Jonas Jabari2901b47f0461
diff --git a/app/models/project_custom_field.rb b/app/models/project_custom_field.rb
index 69560870d83..c11c4e00cf0 100644
--- a/app/models/project_custom_field.rb
+++ b/app/models/project_custom_field.rb
@@ -33,6 +33,8 @@ class ProjectCustomField < CustomField
acts_as_list column: :position_in_custom_field_section, scope: [:custom_field_section_id]
+ after_create :activate_in_all_projects
+
def type_name
:label_project_plural
end
@@ -44,4 +46,10 @@ class ProjectCustomField < CustomField
where(visible: true)
end
end
+
+ def activate_in_all_projects
+ # until we have the project mapping UI, activate the custom field in all projects
+ mappings = Project.active.map { |project| { project_id: project.id, custom_field_id: id } }
+ ProjectCustomFieldProjectMapping.create!(mappings)
+ end
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 86d30f31b5a..15b593c5579 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1972,6 +1972,7 @@ en:
label_no_data: "No data to display"
label_no_parent_page: "No parent page"
label_nothing_display: "Nothing to display"
+ label_not_set_yet: "Not set yet"
label_nobody: "nobody"
label_not_found: 'not found'
label_none: "none"
diff --git a/modules/overviews/app/components/project_attributes/section/custom_field_value/show_component.html.erb b/modules/overviews/app/components/project_attributes/section/custom_field_value/show_component.html.erb
deleted file mode 100644
index 7b8062a52b6..00000000000
--- a/modules/overviews/app/components/project_attributes/section/custom_field_value/show_component.html.erb
+++ /dev/null
@@ -1,12 +0,0 @@
-<%=
- flex_layout(align_items: :flex_start, justify_content: :space_between) do |custom_field_value_container|
- # temporarily using inline styles in order to align the content as desired
- custom_field_value_container.with_column(mr: 2, style: "width: 130px;") do
- render(Primer::Beta::Text.new(font_weight: :bold)) { @custom_field_value.custom_field.name }
- end
-
- custom_field_value_container.with_column(flex: 1) do
- render(Primer::Beta::Text.new()) { formated_value }
- end
- end
-%>
diff --git a/modules/overviews/app/components/project_attributes/sidebar_component.html.erb b/modules/overviews/app/components/project_attributes/sidebar_component.html.erb
deleted file mode 100644
index 85a01ffe93e..00000000000
--- a/modules/overviews/app/components/project_attributes/sidebar_component.html.erb
+++ /dev/null
@@ -1,16 +0,0 @@
-<%=
- content_tag("turbo-frame", id: "project-attributes-sidebar") do
- component_wrapper do
- flex_layout do |sections_container|
- ProjectCustomFieldSection.all.order(created_at: :asc).each do |project_custom_field_section|
- sections_container.with_row(mb: 3) do
- render(ProjectAttributes::Section::ShowComponent.new(
- project: @project,
- project_custom_field_section: project_custom_field_section
- ))
- end
- end
- end
- end
- end
-%>
diff --git a/modules/overviews/app/components/project_attributes/section/edit_dialog_component.html.erb b/modules/overviews/app/components/project_custom_fields/sections/edit_dialog_component.html.erb
similarity index 84%
rename from modules/overviews/app/components/project_attributes/section/edit_dialog_component.html.erb
rename to modules/overviews/app/components/project_custom_fields/sections/edit_dialog_component.html.erb
index 239a5d08205..93ab06ee19c 100644
--- a/modules/overviews/app/components/project_attributes/section/edit_dialog_component.html.erb
+++ b/modules/overviews/app/components/project_custom_fields/sections/edit_dialog_component.html.erb
@@ -10,9 +10,9 @@
component_collection do |collection|
collection.with_component(Primer::Alpha::Dialog::Body.new(style: "max-height: 500px;")) do
flex_layout(my: 3) do |flex|
- project_custom_field_values_of_section.group_by { |cfv| cfv.custom_field_id }.sort.each do |custom_field_id, custom_field_values|
+ @active_project_custom_fields_of_section.each do |project_custom_field|
flex.with_row(mb: 2) do
- render_custom_field_value_input(f, custom_field_id, custom_field_values)
+ render_custom_field_value_input(f, project_custom_field, project_custom_field_values_for(project_custom_field.id))
end
end
end
diff --git a/modules/overviews/app/components/project_attributes/section/edit_dialog_component.rb b/modules/overviews/app/components/project_custom_fields/sections/edit_dialog_component.rb
similarity index 81%
rename from modules/overviews/app/components/project_attributes/section/edit_dialog_component.rb
rename to modules/overviews/app/components/project_custom_fields/sections/edit_dialog_component.rb
index d608059e1e2..d914a9975eb 100644
--- a/modules/overviews/app/components/project_attributes/section/edit_dialog_component.rb
+++ b/modules/overviews/app/components/project_custom_fields/sections/edit_dialog_component.rb
@@ -26,32 +26,42 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-module ProjectAttributes
- module Section
+module ProjectCustomFields
+ module Sections
class EditDialogComponent < ApplicationComponent
include ApplicationHelper
include OpTurbo::Streamable
include OpPrimer::ComponentHelpers
- def initialize(project:, project_custom_field_section:, custom_field_values:)
+ def initialize(project:,
+ project_custom_field_section:,
+ active_project_custom_fields_of_section:,
+ project_custom_field_values:)
super
@project = project
@project_custom_field_section = project_custom_field_section
- @custom_field_values = custom_field_values
+ @active_project_custom_fields_of_section = active_project_custom_fields_of_section
+ @project_custom_field_values = project_custom_field_values
end
private
- def project_custom_field_values_of_section
- @custom_field_values.to_a.select do |cfv|
- @project_custom_field_section.project_custom_fields.pluck(:id).include?(cfv.custom_field_id)
+ def project_custom_field_values_for(project_custom_field_id)
+ values = @project_custom_field_values.select { |pcfv| pcfv.custom_field_id == project_custom_field_id }
+
+ if values.empty?
+ [CustomValue.new(
+ custom_field_id: project_custom_field_id,
+ customized_id: @project.id,
+ customized_type: "Project"
+ )]
+ else
+ values
end
end
- def render_custom_field_value_input(form, custom_field_id, custom_field_values)
- custom_field = CustomField.find(custom_field_id)
-
+ def render_custom_field_value_input(form, custom_field, custom_field_values)
if custom_field.multi_value?
render_multi_value_custom_field_input(form, custom_field, custom_field_values)
else
diff --git a/modules/overviews/app/components/project_custom_fields/sections/project_custom_fields/show_component.html.erb b/modules/overviews/app/components/project_custom_fields/sections/project_custom_fields/show_component.html.erb
new file mode 100644
index 00000000000..ae66097c7c2
--- /dev/null
+++ b/modules/overviews/app/components/project_custom_fields/sections/project_custom_fields/show_component.html.erb
@@ -0,0 +1,16 @@
+<%=
+ flex_layout(align_items: :flex_start, justify_content: :space_between) do |custom_field_value_container|
+ # temporarily using inline styles in order to align the content as desired
+ custom_field_value_container.with_row(mb: 1) do
+ render(Primer::Beta::Text.new(font_weight: :bold)) { @project_custom_field.name }
+ end
+
+ custom_field_value_container.with_row do
+ if @project_custom_field_value.blank?
+ render(Primer::Beta::Text.new(color: :subtle)) { t('label_not_set_yet') }
+ else
+ render(Primer::Beta::Text.new()) { formated_value }
+ end
+ end
+ end
+%>
diff --git a/modules/overviews/app/components/project_attributes/section/custom_field_value/show_component.rb b/modules/overviews/app/components/project_custom_fields/sections/project_custom_fields/show_component.rb
similarity index 74%
rename from modules/overviews/app/components/project_attributes/section/custom_field_value/show_component.rb
rename to modules/overviews/app/components/project_custom_fields/sections/project_custom_fields/show_component.rb
index a9f86db6c93..602bb6f1820 100644
--- a/modules/overviews/app/components/project_attributes/section/custom_field_value/show_component.rb
+++ b/modules/overviews/app/components/project_custom_fields/sections/project_custom_fields/show_component.rb
@@ -26,29 +26,32 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-module ProjectAttributes
- module Section
- module CustomFieldValue
+module ProjectCustomFields
+ module Sections
+ module ProjectCustomFields
class ShowComponent < ApplicationComponent
include ApplicationHelper
include OpPrimer::ComponentHelpers
- def initialize(custom_field_value:)
+ def initialize(project_custom_field:, project_custom_field_value:)
super
- @custom_field_value = custom_field_value
+ @project_custom_field = project_custom_field
+ @project_custom_field_value = project_custom_field_value
end
private
def formated_value
- case @custom_field_value.custom_field.field_format
+ return if @project_custom_field_value.blank?
+
+ case @project_custom_field.field_format
when "text"
- ::OpenProject::TextFormatting::Renderer.format_text(@custom_field_value.typed_value)
+ ::OpenProject::TextFormatting::Renderer.format_text(@project_custom_field_value.typed_value)
when "date"
- format_date(@custom_field_value.typed_value)
+ format_date(@project_custom_field_value.typed_value)
else
- @custom_field_value.typed_value&.to_s
+ @project_custom_field_value.typed_value&.to_s
end
end
end
diff --git a/modules/overviews/app/components/project_attributes/section/show_component.html.erb b/modules/overviews/app/components/project_custom_fields/sections/show_component.html.erb
similarity index 66%
rename from modules/overviews/app/components/project_attributes/section/show_component.html.erb
rename to modules/overviews/app/components/project_custom_fields/sections/show_component.html.erb
index 5946816e993..6a7f26a3d00 100644
--- a/modules/overviews/app/components/project_attributes/section/show_component.html.erb
+++ b/modules/overviews/app/components/project_custom_fields/sections/show_component.html.erb
@@ -12,7 +12,7 @@
id: "edit-project-attributes-dialog-#{@project_custom_field_section.id}",
src: project_attribute_section_dialog_path(project_id: @project.id, section_id: @project_custom_field_section.id),
size: :medium_portrait,
- title: "Edit #{ @project_custom_field_section.name }",
+ title: "#{t('label_edit')} #{ @project_custom_field_section.name }",
button_icon: :pencil,
button_attributes: { scheme: :invisible }
))
@@ -20,9 +20,12 @@
end
end
- project_custom_field_values_of_section.each do |custom_field_value|
- details_container.with_row(mb: 1) do
- render(ProjectAttributes::Section::CustomFieldValue::ShowComponent.new(custom_field_value: custom_field_value))
+ sorted_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,
+ project_custom_field_value: get_eager_loaded_project_custom_field_value_for(project_custom_field.id)
+ ))
end
end
end
diff --git a/modules/overviews/app/components/project_attributes/section/show_component.rb b/modules/overviews/app/components/project_custom_fields/sections/show_component.rb
similarity index 63%
rename from modules/overviews/app/components/project_attributes/section/show_component.rb
rename to modules/overviews/app/components/project_custom_fields/sections/show_component.rb
index aea681c0158..d7463350800 100644
--- a/modules/overviews/app/components/project_attributes/section/show_component.rb
+++ b/modules/overviews/app/components/project_custom_fields/sections/show_component.rb
@@ -26,34 +26,42 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-module ProjectAttributes
- module Section
+module ProjectCustomFields
+ module Sections
class ShowComponent < ApplicationComponent
include ApplicationHelper
include OpPrimer::ComponentHelpers
include OpTurbo::Streamable
- def initialize(project:, project_custom_field_section:)
+ def initialize(project:, project_custom_field_section:, project_custom_fields:)
super
@project = project
@project_custom_field_section = project_custom_field_section
+ @project_custom_fields = project_custom_fields
+
+ eager_load_project_custom_field_values
end
private
- def project_custom_field_values
- active_custom_field_ids_of_project = ProjectCustomFieldProjectMapping
- .where(project_id: @project.id)
- .pluck(:custom_field_id)
+ def eager_load_project_custom_field_values
+ # TODO: move to service
+ @eager_loaded_project_custom_field_values = CustomValue
+ .includes(custom_field: :custom_options)
+ .where(
+ custom_field_id: @project_custom_fields.pluck(:id),
+ customized_id: @project.id
+ )
+ .to_a
+ end
- CustomValue.where(custom_field_id: active_custom_field_ids_of_project, customized_id: @project.id)
+ def sorted_project_custom_fields
+ @project_custom_fields.sort_by { |pcf| pcf.position_in_custom_field_section }
end
- def project_custom_field_values_of_section
- project_custom_field_values.select do |cfv|
- @project_custom_field_section.project_custom_fields.pluck(:id).include?(cfv.custom_field_id)
- end
+ def get_eager_loaded_project_custom_field_value_for(custom_field_id)
+ @eager_loaded_project_custom_field_values.find { |pcfv| pcfv.custom_field_id == custom_field_id }
end
end
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
new file mode 100644
index 00000000000..43669a2f995
--- /dev/null
+++ b/modules/overviews/app/components/project_custom_fields/sidebar_component.html.erb
@@ -0,0 +1,17 @@
+<%=
+ content_tag("turbo-frame", id: "project-attributes-sidebar") do
+ component_wrapper do
+ flex_layout do |sections_container|
+ @active_project_custom_fields_grouped_by_section.each do |project_custom_field_section_id, 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_fields: project_custom_fields
+ ))
+ end
+ end
+ end
+ end
+ end
+%>
diff --git a/modules/overviews/app/components/project_attributes/sidebar_component.rb b/modules/overviews/app/components/project_custom_fields/sidebar_component.rb
similarity index 73%
rename from modules/overviews/app/components/project_attributes/sidebar_component.rb
rename to modules/overviews/app/components/project_custom_fields/sidebar_component.rb
index 7ae51c2cbc0..207f065c038 100644
--- a/modules/overviews/app/components/project_attributes/sidebar_component.rb
+++ b/modules/overviews/app/components/project_custom_fields/sidebar_component.rb
@@ -26,16 +26,24 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-module ProjectAttributes
+module ProjectCustomFields
class SidebarComponent < ApplicationComponent
include ApplicationHelper
include OpPrimer::ComponentHelpers
include OpTurbo::Streamable
- def initialize(project:)
+ def initialize(project:, project_custom_field_sections:, active_project_custom_fields_grouped_by_section:)
super
@project = project
+ @project_custom_field_sections = project_custom_field_sections
+ @active_project_custom_fields_grouped_by_section = active_project_custom_fields_grouped_by_section
+ end
+
+ private
+
+ def get_eager_loaded_project_custom_field_section(project_custom_field_section_id)
+ @project_custom_field_sections.find { |pcfs| pcfs.id == project_custom_field_section_id }
end
end
end
diff --git a/modules/overviews/app/controllers/overviews/overviews_controller.rb b/modules/overviews/app/controllers/overviews/overviews_controller.rb
index fdceb3ffa1a..e09105c6d52 100644
--- a/modules/overviews/app/controllers/overviews/overviews_controller.rb
+++ b/modules/overviews/app/controllers/overviews/overviews_controller.rb
@@ -8,8 +8,10 @@ module ::Overviews
def attributes_sidebar
render(
- ProjectAttributes::SidebarComponent.new(
- project: @project
+ ProjectCustomFields::SidebarComponent.new(
+ project: @project,
+ project_custom_field_sections: ProjectCustomFieldSection.all,
+ active_project_custom_fields_grouped_by_section:
),
layout: false
)
@@ -18,39 +20,46 @@ module ::Overviews
def attribute_section_dialog
section = ProjectCustomFieldSection.find(params[:section_id])
- active_custom_field_ids_of_project = ProjectCustomFieldProjectMapping
- .where(project_id: @project.id)
- .pluck(:custom_field_id)
+ active_project_custom_fields_of_section = active_project_custom_fields_grouped_by_section[section.id]
+ .sort_by(&:position_in_custom_field_section)
- custom_field_values = CustomValue.where(custom_field_id: active_custom_field_ids_of_project, customized_id: @project.id)
+ eager_loaded_project_custom_field_values = CustomValue.where(
+ custom_field_id: active_project_custom_fields_of_section.pluck(:id),
+ customized_id: @project.id
+ ).to_a
render(
- ProjectAttributes::Section::EditDialogComponent.new(
+ ProjectCustomFields::Sections::EditDialogComponent.new(
project: @project,
project_custom_field_section: section,
- custom_field_values:
+ active_project_custom_fields_of_section:,
+ project_custom_field_values: eager_loaded_project_custom_field_values
),
layout: false
)
end
def update_attributes
+ # prototypical implementation
# manual nested attributes update as the project model is not yet natively supporting it
# needs refactoring
section = ProjectCustomFieldSection.find(params[:section_id])
+ active_project_custom_fields_of_section = active_project_custom_fields_grouped_by_section[section.id]
+ .sort_by(&:position_in_custom_field_section)
+
modified_custom_field_values = []
+
has_errors = false
+
ActiveRecord::Base.transaction do
# transaction to rollback if any of the custom field values fails to save
project_attribute_params[:custom_field_values_attributes]&.each do |custom_value_id, attributes|
custom_value = CustomValue.find(custom_value_id.to_i)
custom_value.value = attributes[:value]
- unless custom_value.save
- has_errors = true
- end
+ has_errors = true if custom_value.invalid?
modified_custom_field_values << custom_value
end
@@ -62,9 +71,7 @@ module ::Overviews
customized_id: @project.id
)
- unless custom_value.save
- has_errors = true
- end
+ has_errors = true if custom_value.invalid?
modified_custom_field_values << custom_value
end
@@ -85,26 +92,27 @@ module ::Overviews
customized_id: @project.id
)
- unless custom_value.save
- has_errors = true
- end
+ has_errors = true if custom_value.invalid?
modified_custom_field_values << custom_value
end
end
if has_errors
update_via_turbo_stream(
- component: ProjectAttributes::Section::EditDialogComponent.new(
+ component: ProjectCustomFields::Sections::EditDialogComponent.new(
project: @project,
project_custom_field_section: section,
- custom_field_values: modified_custom_field_values
+ active_project_custom_fields_of_section:,
+ project_custom_field_values: modified_custom_field_values
)
)
- raise ActiveRecord::Rollback
else
+ modified_custom_field_values.each(&:save!)
update_via_turbo_stream(
- component: ProjectAttributes::SidebarComponent.new(
- project: @project
+ component: ProjectCustomFields::SidebarComponent.new(
+ project: @project,
+ project_custom_field_sections: ProjectCustomFieldSection.all,
+ active_project_custom_fields_grouped_by_section:
)
)
end
@@ -129,5 +137,18 @@ module ::Overviews
multi_custom_field_values_attributes: [:custom_field_id, { values: [] }]
)
end
+
+ def active_project_custom_fields_grouped_by_section
+ # TODO: move to service
+ active_custom_field_ids_of_project = ProjectCustomFieldProjectMapping
+ .where(project_id: @project.id)
+ .pluck(:custom_field_id)
+
+ ProjectCustomField
+ .includes(:project_custom_field_section)
+ .where(id: active_custom_field_ids_of_project)
+ .sort_by { |pcf| pcf.project_custom_field_section.position }
+ .group_by(&:custom_field_section_id)
+ end
end
end