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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
introduce project custom field sections and project mapping
Jonas Jabaricc886f28d262
diff --git a/app/models/project_custom_field.rb b/app/models/project_custom_field.rbindex 101be6f0a38..faa827c5280 100644--- a/app/models/project_custom_field.rb+++ b/app/models/project_custom_field.rb@@ -27,7 +27,18 @@#++class ProjectCustomField < CustomField- # belongs_to :project_custom_field_section+ # don't pollute the custom_fields table with a section_id column which is only used by ProjectCustomFields+ # use a separate mapping table instead+ has_many :project_custom_field_section_mappings,+ dependent: :destroy,+ inverse_of: :project_custom_field,+ class_name: 'ProjectCustomFieldSectionMapping',+ foreign_key: 'custom_field_id'++ has_many :project_custom_field_sections,+ through: :project_custom_field_section_mappings++ validate :exactly_one_section_mappeddef type_name:label_project_plural@@ -40,4 +51,24 @@ class ProjectCustomField < CustomFieldwhere(visible: true)endend++ def exactly_one_section_mapped+ unless project_custom_field_sections.count == 1+ errors.add(:base, "Exactly one section must be mapped to this custom field.")+ end+ end++ def project_custom_field_section+ project_custom_field_sections.first+ end++ def project_custom_field_section=(section)+ # without this reload, a nil assignment is not recovered properly+ project_custom_field_sections.reload++ ActiveRecord::Base.transaction do+ project_custom_field_sections.clear+ project_custom_field_sections << section+ end+ endenddiff --git a/app/models/project_custom_field_project_mapping.rb b/app/models/project_custom_field_project_mapping.rbnew file mode 100644index 00000000000..e6d5c413875--- /dev/null+++ b/app/models/project_custom_field_project_mapping.rb@@ -0,0 +1,45 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++class ProjectCustomFieldProjectMapping < ApplicationRecord+ belongs_to :project+ belongs_to :project_custom_field, class_name: 'ProjectCustomField', foreign_key: 'custom_field_id',+ inverse_of: :project_custom_field_section_mappings++ # # Additionally to the database-level unique constraint, the application-level validation ensures that a+ # # custom_field is associated with only one section+ # validate :project_custom_field_uniqueness++ # private++ # def project_custom_field_uniqueness+ # if ProjectCustomFieldSectionMapping.where(custom_field_id:).where.not(id:).exists?+ # errors.add(:project_custom_field, "is already associated with another section")+ # end+ # end+enddiff --git a/app/models/project_custom_field_section.rb b/app/models/project_custom_field_section.rbnew file mode 100644index 00000000000..f849e376f09--- /dev/null+++ b/app/models/project_custom_field_section.rb@@ -0,0 +1,34 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++class ProjectCustomFieldSection < ApplicationRecord+ has_many :project_custom_field_section_mappings, dependent: :destroy+ has_many :project_custom_fields, through: :project_custom_field_section_mappings++ acts_as_list+enddiff --git a/app/models/project_custom_field_section_mapping.rb b/app/models/project_custom_field_section_mapping.rbnew file mode 100644index 00000000000..33bc324a151--- /dev/null+++ b/app/models/project_custom_field_section_mapping.rb@@ -0,0 +1,47 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++class ProjectCustomFieldSectionMapping < ApplicationRecord+ belongs_to :project_custom_field_section+ belongs_to :project_custom_field, class_name: 'ProjectCustomField', foreign_key: 'custom_field_id',+ inverse_of: :project_custom_field_section_mappings++ # Additionally to the database-level unique constraint, the application-level validation ensures that a+ # custom_field is associated with only one section+ validate :project_custom_field_uniqueness++ acts_as_list scope: :project_custom_field_section++ private++ def project_custom_field_uniqueness+ if ProjectCustomFieldSectionMapping.where(custom_field_id:).where.not(id:).exists?+ errors.add(:project_custom_field, "is already associated with another section")+ end+ end+enddiff --git a/db/migrate/20231030103212_create_project_custom_field_sections.rb b/db/migrate/20231030103212_create_project_custom_field_sections.rbnew file mode 100644index 00000000000..f79300b9d67--- /dev/null+++ b/db/migrate/20231030103212_create_project_custom_field_sections.rb@@ -0,0 +1,47 @@+class CreateProjectCustomFieldSections < ActiveRecord::Migration[7.0]+ def up+ create_table :project_custom_field_sections do |t|+ t.integer :position+ t.string :name++ t.timestamps+ end++ # don't pollute the custom_fields table with a section_id column which is only used by ProjectCustomFields+ # use a separate mapping table instead+ create_table :project_custom_field_section_mappings do |t|+ t.references :project_custom_field_section, foreign_key: true, index: {+ name: 'index_project_cfs_mappings_on_section_id'+ }+ t.references :custom_field, foreign_key: true+ t.integer :position++ t.timestamps+ end++ # Add a unique constraint to ensure that a custom_field can only be added to one section+ add_index :project_custom_field_section_mappings, :custom_field_id, unique: true,+ name: 'index_project_cfs_mappings_on_custom_field_id'++ create_and_assign_default_section+ end++ def down+ drop_table :project_custom_field_section_mappings+ drop_table :project_custom_field_sections+ end++ private++ def create_and_assign_default_section+ section = ProjectCustomFieldSection.create!(+ name: "Project attributes"+ )++ mappings = ProjectCustomField.pluck(:id).map do |id|+ { project_custom_field_section_id: section.id, custom_field_id: id }+ end++ ProjectCustomFieldSectionMapping.create!(mappings)+ end+enddiff --git a/db/migrate/20231031133334_create_project_custom_field_project_mappings.rb b/db/migrate/20231031133334_create_project_custom_field_project_mappings.rbnew file mode 100644index 00000000000..42462529531--- /dev/null+++ b/db/migrate/20231031133334_create_project_custom_field_project_mappings.rb@@ -0,0 +1,34 @@+class CreateProjectCustomFieldProjectMappings < ActiveRecord::Migration[7.0]+ def up+ create_table :project_custom_field_project_mappings do |t|+ t.references :custom_field, foreign_key: true, index: {+ name: 'index_project_cf_project_mappings_on_custom_field_id'+ }+ t.references :project, foreign_key: true++ t.timestamps+ end++ create_default_mapping+ end++ def down+ drop_table :project_custom_field_project_mappings+ end++ private++ def create_default_mapping+ project_ids = Project.pluck(:id)+ custom_field_ids = ProjectCustomField.pluck(:id)+ mappings = []++ project_ids.each do |project_id|+ custom_field_ids.each do |custom_field_id|+ mappings << { custom_field_id:, project_id: }+ end+ end++ ProjectCustomFieldProjectMapping.create!(mappings)+ end+enddiff --git a/modules/overviews/app/components/project_attributes/section/edit_dialog_component.html.erb b/modules/overviews/app/components/project_attributes/section/edit_dialog_component.html.erbindex a15d1256416..239a5d08205 100644--- a/modules/overviews/app/components/project_attributes/section/edit_dialog_component.html.erb+++ b/modules/overviews/app/components/project_attributes/section/edit_dialog_component.html.erb@@ -1,16 +1,16 @@<%=- content_tag("turbo-frame", id: "edit-project-attributes-dialog-frame") do+ content_tag("turbo-frame", id: "edit-project-attributes-dialog-#{@project_custom_field_section.id}-frame") docomponent_wrapper doprimer_form_with(model: @project,method: :put,- url: project_update_attributes_path(@project)+ url: project_update_attributes_path(project_id: @project.id, section_id: @project_custom_field_section.id),) do |f|component_collection do |collection|collection.with_component(Primer::Alpha::Dialog::Body.new(style: "max-height: 500px;")) doflex_layout(my: 3) do |flex|- @custom_field_values.group_by { |cfv| cfv.custom_field_id }.sort.each do |custom_field_id, custom_field_values|+ project_custom_field_values_of_section.group_by { |cfv| cfv.custom_field_id }.sort.each do |custom_field_id, custom_field_values|flex.with_row(mb: 2) dorender_custom_field_value_input(f, custom_field_id, custom_field_values)end@@ -21,7 +21,7 @@component_collection do |collection1|collection1.with_component(Primer::ButtonComponent.new(data: {- 'close-dialog-id': "edit-project-attributes-dialog"+ 'close-dialog-id': "edit-project-attributes-dialog-#{@project_custom_field_section.id}"})) dot("button_cancel")diff --git a/modules/overviews/app/components/project_attributes/section/edit_dialog_component.rb b/modules/overviews/app/components/project_attributes/section/edit_dialog_component.rbindex 471cd359bdd..d608059e1e2 100644--- a/modules/overviews/app/components/project_attributes/section/edit_dialog_component.rb+++ b/modules/overviews/app/components/project_attributes/section/edit_dialog_component.rb@@ -33,13 +33,22 @@ module ProjectAttributesinclude OpTurbo::Streamableinclude OpPrimer::ComponentHelpers- def initialize(project:, custom_field_values:)+ def initialize(project:, project_custom_field_section:, custom_field_values:)super@project = project+ @project_custom_field_section = project_custom_field_section@custom_field_values = custom_field_valuesend+ 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)+ end+ end+def render_custom_field_value_input(form, custom_field_id, custom_field_values)custom_field = CustomField.find(custom_field_id)@@ -69,7 +78,8 @@ module ProjectAttributeswhen "user"render(Project::CustomValueForm::SingleUserSelectList.new(form, custom_field:, custom_field_value:, project: @project))when "version"- render(Project::CustomValueForm::SingleVersionSelectList.new(form, custom_field:, custom_field_value:, project: @project))+ render(Project::CustomValueForm::SingleVersionSelectList.new(form, custom_field:, custom_field_value:,+ project: @project))endend@@ -80,7 +90,8 @@ module ProjectAttributeswhen "user"render(Project::CustomValueForm::MultiUserSelectList.new(form, custom_field:, custom_field_values:, project: @project))when "version"- render(Project::CustomValueForm::MultiVersionSelectList.new(form, custom_field:, custom_field_values:, project: @project))+ render(Project::CustomValueForm::MultiVersionSelectList.new(form, custom_field:, custom_field_values:,+ project: @project))endendenddiff --git a/modules/overviews/app/components/project_attributes/section/show_component.html.erb b/modules/overviews/app/components/project_attributes/section/show_component.html.erbindex 40c30b6328c..5946816e993 100644--- a/modules/overviews/app/components/project_attributes/section/show_component.html.erb+++ b/modules/overviews/app/components/project_attributes/section/show_component.html.erb@@ -4,15 +4,15 @@details_container.with_row(mb: 2) doflex_layout(align_items: :center, justify_content: :space_between) do |heading|heading.with_column(flex: 1) do- render(Primer::Beta::Heading.new(tag: :h4)) { "Basic info" }+ render(Primer::Beta::Heading.new(tag: :h4)) { @project_custom_field_section.name }endheading.with_column dorender(OpTurbo::OpPrimer::AsyncDialogComponent.new(- id: "edit-project-attributes-dialog",- src: project_attribute_section_dialog_path(@project),+ 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 basic info",+ title: "Edit #{ @project_custom_field_section.name }",button_icon: :pencil,button_attributes: { scheme: :invisible }))@@ -20,7 +20,7 @@endend- @project.custom_field_values.each do |custom_field_value|+ project_custom_field_values_of_section.each do |custom_field_value|details_container.with_row(mb: 1) dorender(ProjectAttributes::Section::CustomFieldValue::ShowComponent.new(custom_field_value: custom_field_value))enddiff --git a/modules/overviews/app/components/project_attributes/section/show_component.rb b/modules/overviews/app/components/project_attributes/section/show_component.rbindex b6f127600e9..aea681c0158 100644--- a/modules/overviews/app/components/project_attributes/section/show_component.rb+++ b/modules/overviews/app/components/project_attributes/section/show_component.rb@@ -26,7 +26,6 @@# See COPYRIGHT and LICENSE files for more details.#++-module ProjectAttributesmodule Sectionclass ShowComponent < ApplicationComponent@@ -34,10 +33,27 @@ module ProjectAttributesinclude OpPrimer::ComponentHelpersinclude OpTurbo::Streamable- def initialize(project:)+ def initialize(project:, project_custom_field_section:)super@project = project+ @project_custom_field_section = project_custom_field_section+ end++ private++ def project_custom_field_values+ active_custom_field_ids_of_project = ProjectCustomFieldProjectMapping+ .where(project_id: @project.id)+ .pluck(:custom_field_id)++ CustomValue.where(custom_field_id: active_custom_field_ids_of_project, customized_id: @project.id)+ 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)+ endendendenddiff --git a/modules/overviews/app/components/project_attributes/sidebar_component.html.erb b/modules/overviews/app/components/project_attributes/sidebar_component.html.erbindex ba2810aeba3..85a01ffe93e 100644--- a/modules/overviews/app/components/project_attributes/sidebar_component.html.erb+++ b/modules/overviews/app/components/project_attributes/sidebar_component.html.erb@@ -1,7 +1,16 @@<%=content_tag("turbo-frame", id: "project-attributes-sidebar") docomponent_wrapper do- render(ProjectAttributes::Section::ShowComponent.new(project: @project))+ 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+ endendend%>diff --git a/modules/overviews/app/components/project_attributes/sidebar_component.rb b/modules/overviews/app/components/project_attributes/sidebar_component.rbindex aa7fefa26fb..7ae51c2cbc0 100644--- a/modules/overviews/app/components/project_attributes/sidebar_component.rb+++ b/modules/overviews/app/components/project_attributes/sidebar_component.rb@@ -26,7 +26,6 @@# See COPYRIGHT and LICENSE files for more details.#++-module ProjectAttributesclass SidebarComponent < ApplicationComponentinclude ApplicationHelperdiff --git a/modules/overviews/app/controllers/overviews/overviews_controller.rb b/modules/overviews/app/controllers/overviews/overviews_controller.rbindex 27c12bc8205..fdceb3ffa1a 100644--- a/modules/overviews/app/controllers/overviews/overviews_controller.rb+++ b/modules/overviews/app/controllers/overviews/overviews_controller.rb@@ -16,10 +16,19 @@ module ::Overviewsenddef 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)++ custom_field_values = CustomValue.where(custom_field_id: active_custom_field_ids_of_project, customized_id: @project.id)+render(ProjectAttributes::Section::EditDialogComponent.new(project: @project,- custom_field_values: @project.custom_field_values+ project_custom_field_section: section,+ custom_field_values:),layout: false)@@ -29,6 +38,8 @@ module ::Overviews# manual nested attributes update as the project model is not yet natively supporting it# needs refactoring+ section = ProjectCustomFieldSection.find(params[:section_id])+modified_custom_field_values = []has_errors = falseActiveRecord::Base.transaction do@@ -85,6 +96,7 @@ module ::Overviewsupdate_via_turbo_stream(component: ProjectAttributes::Section::EditDialogComponent.new(project: @project,+ project_custom_field_section: section,custom_field_values: modified_custom_field_values))diff --git a/modules/overviews/config/routes.rb b/modules/overviews/config/routes.rbindex 64730d6247e..fb1db6b3c34 100644--- a/modules/overviews/config/routes.rb+++ b/modules/overviews/config/routes.rb@@ -1,8 +1,11 @@OpenProject::Application.routes.draw doconstraints(project_id: Regexp.new("(?!(#{Project::RESERVED_IDENTIFIERS.join('|')})$)(\\w|-)+")) doget 'projects/:project_id', to: "overviews/overviews#show", as: :project_overview, format: :html- get 'projects/:project_id/attributes_sidebar', to: "overviews/overviews#attributes_sidebar", as: :project_attributes_sidebar, format: :html- get 'projects/:project_id/attribute_section_dialog', to: "overviews/overviews#attribute_section_dialog", as: :project_attribute_section_dialog, format: :html- put 'projects/:project_id/attributes', to: "overviews/overviews#update_attributes", as: :project_update_attributes, format: :html+ get 'projects/:project_id/attributes_sidebar', to: "overviews/overviews#attributes_sidebar", as: :project_attributes_sidebar,+ format: :html+ get 'projects/:project_id/attribute_section_dialog/:section_id', to: "overviews/overviews#attribute_section_dialog",+ as: :project_attribute_section_dialog, format: :html+ put 'projects/:project_id/attributes/:section_id', to: "overviews/overviews#update_attributes", as: :project_update_attributes,+ format: :htmlendend