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
WIP: refactoring of prototypical code as discussed
Jonas Jabari5721538fba2f
diff --git a/app/forms/projects/custom_fields/form.rb b/app/forms/projects/custom_fields/form.rbnew file mode 100644index 00000000000..9f62702c3df--- /dev/null+++ b/app/forms/projects/custom_fields/form.rb@@ -0,0 +1,115 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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.+#+++module Projects::CustomFields+ class Form < ApplicationForm+ form do |custom_fields_form|+ sorted_custom_fields.each do |custom_field|+ custom_fields_form.fields_for(:custom_field_values) do |builder|+ custom_field_input(builder, custom_field)+ end+ end+ end++ def initialize(project:, custom_field_section: nil)+ super()+ @project = project+ @custom_field_section = custom_field_section+ end++ private++ def sorted_custom_fields+ # TODO: move to service/model+ return @custom_fields if @custom_fields.present?++ @custom_fields ||= @project.available_custom_fields++ if @custom_field_section.present?+ @custom_fields = @custom_fields+ .where(custom_field_section_id: @custom_field_section.id)+ end++ @custom_fields = @custom_fields.sort_by do |pcf|+ [pcf.project_custom_field_section.position, pcf.position_in_custom_field_section]+ end+ end++ def custom_field_input(builder, custom_field)+ if custom_field.multi_value?+ custom_values = @project.custom_values_for_custom_field(id: custom_field.id)+ multi_value_custom_field_input(builder, custom_field, custom_values)+ else+ custom_value = @project.custom_value_for(custom_field.id)+ single_value_custom_field_input(builder, custom_field, custom_value)+ end+ end++ # TBD: transform inputs called below to primer form dsl instead of form classes?+ # TODOS:+ # - list inputs cannot be resetted currently (worked before refactoring though)+ # - initial values for user inputs are not displayed++ def single_value_custom_field_input(builder, custom_field, custom_value)+ form_args = { custom_field:, custom_value:, project: @project }++ case custom_field.field_format+ when "string"+ Projects::CustomFields::Inputs::String.new(builder, **form_args)+ when "text"+ Projects::CustomFields::Inputs::Text.new(builder, **form_args)+ when "int"+ Projects::CustomFields::Inputs::Int.new(builder, **form_args)+ when "float"+ Projects::CustomFields::Inputs::Float.new(builder, **form_args)+ when "list"+ Projects::CustomFields::Inputs::SingleSelectList.new(builder, **form_args)+ when "date"+ Projects::CustomFields::Inputs::Date.new(builder, **form_args)+ when "bool"+ Projects::CustomFields::Inputs::Bool.new(builder, **form_args)+ when "user"+ Projects::CustomFields::Inputs::SingleUserSelectList.new(builder, **form_args)+ when "version"+ Projects::CustomFields::Inputs::SingleVersionSelectList.new(builder, **form_args)+ end+ end++ def multi_value_custom_field_input(builder, custom_field, custom_values)+ form_args = { custom_field:, custom_values:, project: @project }++ case custom_field.field_format+ when "list"+ Projects::CustomFields::Inputs::MultiSelectList.new(builder, **form_args)+ when "user"+ Projects::CustomFields::Inputs::MultiUserSelectList.new(builder, **form_args)+ when "version"+ Projects::CustomFields::Inputs::MultiVersionSelectList.new(builder, **form_args)+ end+ end+ end+enddiff --git a/modules/overviews/app/forms/project/custom_value_form/base/autocomplete/multi_value_input.rb b/app/forms/projects/custom_fields/inputs/base/autocomplete/multi_value_input.rbsimilarity index 75%rename from modules/overviews/app/forms/project/custom_value_form/base/autocomplete/multi_value_input.rbrename to app/forms/projects/custom_fields/inputs/base/autocomplete/multi_value_input.rbindex 9a97e4040d3..23d602f2deb 100644--- a/modules/overviews/app/forms/project/custom_value_form/base/autocomplete/multi_value_input.rb+++ b/app/forms/projects/custom_fields/inputs/base/autocomplete/multi_value_input.rb@@ -26,12 +26,12 @@# See COPYRIGHT and LICENSE files for more details.#++-class Project::CustomValueForm::Base::Autocomplete::MultiValueInput < ApplicationForm- include Project::CustomValueForm::Base::Utils+class Projects::CustomFields::Inputs::Base::Autocomplete::MultiValueInput < ApplicationForm+ include Projects::CustomFields::Inputs::Base::Utils- def initialize(custom_field:, custom_field_values:, project:)+ def initialize(custom_field:, custom_values:, project:)@custom_field = custom_field- @custom_field_values = custom_field_values+ @custom_values = custom_values@project = projectend@@ -57,15 +57,11 @@ class Project::CustomValueForm::Base::Autocomplete::MultiValueInput < Applicatioraise NotImplementedErrorend- def name- "project[multi_custom_field_values_attributes][#{@custom_field.id}][values]"- end-def invalid?- @custom_field_values.any? { |custom_field_value| custom_field_value.errors.any? }+ @custom_values.any? { |custom_value| custom_value.errors.any? }enddef validation_message- @custom_field_values.map { |custom_field_value| custom_field_value.errors.full_messages }.join(', ') if invalid?+ @custom_values.map { |custom_value| custom_value.errors.full_messages }.join(', ') if invalid?endenddiff --git a/modules/overviews/app/forms/project/custom_value_form/base/autocomplete/single_value_input.rb b/app/forms/projects/custom_fields/inputs/base/autocomplete/single_value_input.rbsimilarity index 83%rename from modules/overviews/app/forms/project/custom_value_form/base/autocomplete/single_value_input.rbrename to app/forms/projects/custom_fields/inputs/base/autocomplete/single_value_input.rbindex f681142c79f..d6d11942942 100644--- a/modules/overviews/app/forms/project/custom_value_form/base/autocomplete/single_value_input.rb+++ b/app/forms/projects/custom_fields/inputs/base/autocomplete/single_value_input.rb@@ -26,12 +26,12 @@# See COPYRIGHT and LICENSE files for more details.#++-class Project::CustomValueForm::Base::Autocomplete::SingleValueInput < ApplicationForm- include Project::CustomValueForm::Base::Utils+class Projects::CustomFields::Inputs::Base::Autocomplete::SingleValueInput < ApplicationForm+ include Projects::CustomFields::Inputs::Base::Utils- def initialize(custom_field:, custom_field_value:, project:)+ def initialize(custom_field:, custom_value:, project:)@custom_field = custom_field- @custom_field_value = custom_field_value+ @custom_value = custom_value@project = projectend@@ -58,10 +58,10 @@ class Project::CustomValueForm::Base::Autocomplete::SingleValueInput < Applicatienddef invalid?- @custom_field_value.errors.any?+ @custom_value.errors.any?enddef validation_message- @custom_field_value.errors.full_messages.join(', ') if invalid?+ @custom_value.errors.full_messages.join(', ') if invalid?endenddiff --git a/modules/overviews/app/forms/project/custom_value_form/base/autocomplete/user_query_utils.rb b/app/forms/projects/custom_fields/inputs/base/autocomplete/user_query_utils.rbsimilarity index 95%rename from modules/overviews/app/forms/project/custom_value_form/base/autocomplete/user_query_utils.rbrename to app/forms/projects/custom_fields/inputs/base/autocomplete/user_query_utils.rbindex 99c16b8799f..dfb07b027f3 100644--- a/modules/overviews/app/forms/project/custom_value_form/base/autocomplete/user_query_utils.rb+++ b/app/forms/projects/custom_fields/inputs/base/autocomplete/user_query_utils.rb@@ -26,7 +26,7 @@# See COPYRIGHT and LICENSE files for more details.#++-module Project::CustomValueForm::Base::Autocomplete::UserQueryUtils+module Projects::CustomFields::Inputs::Base::Autocomplete::UserQueryUtilsdef user_autocomplete_options{placeholder: I18n.t(:label_user_search),@@ -58,6 +58,7 @@ module Project::CustomValueForm::Base::Autocomplete::UserQueryUtilsenddef input_values_filter+ # TODO: not working yetuser_filter = { "type" => { "operator" => "=", "values" => ["User"] } }id_filter = { "id" => { "operator" => "=", "values" => init_user_ids } }diff --git a/modules/overviews/app/forms/project/custom_value_form/base/input.rb b/app/forms/projects/custom_fields/inputs/base/input.rbsimilarity index 78%rename from modules/overviews/app/forms/project/custom_value_form/base/input.rbrename to app/forms/projects/custom_fields/inputs/base/input.rbindex 3dc03b52b1e..5d2d5d0c450 100644--- a/modules/overviews/app/forms/project/custom_value_form/base/input.rb+++ b/app/forms/projects/custom_fields/inputs/base/input.rb@@ -26,12 +26,12 @@# See COPYRIGHT and LICENSE files for more details.#++-class Project::CustomValueForm::Base::Input < ApplicationForm- include Project::CustomValueForm::Base::Utils+class Projects::CustomFields::Inputs::Base::Input < ApplicationForm+ include Projects::CustomFields::Inputs::Base::Utils- def initialize(custom_field:, custom_field_value:, project:)+ def initialize(custom_field:, custom_value:, project:)@custom_field = custom_field- @custom_field_value = custom_field_value+ @custom_value = custom_value@project = projectend@@ -44,15 +44,11 @@ class Project::CustomValueForm::Base::Input < ApplicationForm)end- def value- @custom_field_value.value || @custom_field.default_value- end-def invalid?- @custom_field_value.errors.any?+ @custom_value.errors.any?enddef validation_message- @custom_field_value.errors.full_messages.join(', ') if invalid?+ @custom_value.errors.full_messages.join(', ') if invalid?endenddiff --git a/modules/overviews/app/forms/project/custom_value_form/base/utils.rb b/app/forms/projects/custom_fields/inputs/base/utils.rbsimilarity index 74%rename from modules/overviews/app/forms/project/custom_value_form/base/utils.rbrename to app/forms/projects/custom_fields/inputs/base/utils.rbindex df2c9fb3989..0c5dfac8909 100644--- a/modules/overviews/app/forms/project/custom_value_form/base/utils.rb+++ b/app/forms/projects/custom_fields/inputs/base/utils.rb@@ -26,31 +26,39 @@# See COPYRIGHT and LICENSE files for more details.#++-module Project::CustomValueForm::Base::Utils+module Projects::CustomFields::Inputs::Base::Utilsdef base_input_attributes{- name:,id:,- scope_name_to_model: false,- scope_id_to_model: false,- placeholder: @custom_field.name,- label: @custom_field.name,- required: @custom_field.is_required?,+ scope_name_to_model: false, # TODO: get rid of this, should work with scope_name_to_model: true+ name:,+ label:,+ value:,+ required: required?,invalid: invalid?,validation_message:}enddef id- name.gsub(/[\[\]]/, "_")+ "custom_field_#{@custom_field.id}"enddef name- if @custom_field_value.new_record?- "project[new_custom_field_values_attributes][#{@custom_field_value.custom_field_id}][value]"- else- "project[custom_field_values_attributes][#{@custom_field_value.id}][value]"- end+ # TODO: get rid of this, should work with scope_name_to_model: true+ "project[custom_field_values][#{@custom_field.id}]"+ end++ def label+ @custom_field.name+ end++ def value+ @custom_value+ end++ def required?+ @custom_field.is_required?enddef qa_field_namediff --git a/modules/overviews/app/forms/project/custom_value_form/bool.rb b/app/forms/projects/custom_fields/inputs/bool.rbsimilarity index 94%rename from modules/overviews/app/forms/project/custom_value_form/bool.rbrename to app/forms/projects/custom_fields/inputs/bool.rbindex 29cd0f79dc0..161b3a009de 100644--- a/modules/overviews/app/forms/project/custom_value_form/bool.rb+++ b/app/forms/projects/custom_fields/inputs/bool.rb@@ -26,7 +26,7 @@# See COPYRIGHT and LICENSE files for more details.#++-class Project::CustomValueForm::Bool < Project::CustomValueForm::Base::Input+class Projects::CustomFields::Inputs::Bool < Projects::CustomFields::Inputs::Base::Inputform do |custom_value_form|custom_value_form.check_box(**input_attributes)enddiff --git a/modules/overviews/app/forms/project/custom_value_form/date.rb b/app/forms/projects/custom_fields/inputs/date.rbsimilarity index 93%rename from modules/overviews/app/forms/project/custom_value_form/date.rbrename to app/forms/projects/custom_fields/inputs/date.rbindex 00b9cc8cc63..6eaa4297a02 100644--- a/modules/overviews/app/forms/project/custom_value_form/date.rb+++ b/app/forms/projects/custom_fields/inputs/date.rb@@ -26,7 +26,7 @@# See COPYRIGHT and LICENSE files for more details.#++-class Project::CustomValueForm::Date < Project::CustomValueForm::Base::Input+class Projects::CustomFields::Inputs::Date < Projects::CustomFields::Inputs::Base::Inputform do |custom_value_form|custom_value_form.text_field(**input_attributes)enddiff --git a/modules/overviews/app/forms/project/custom_value_form/float.rb b/app/forms/projects/custom_fields/inputs/float.rbsimilarity index 93%rename from modules/overviews/app/forms/project/custom_value_form/float.rbrename to app/forms/projects/custom_fields/inputs/float.rbindex 2d12a23b91d..9a4c796ff96 100644--- a/modules/overviews/app/forms/project/custom_value_form/float.rb+++ b/app/forms/projects/custom_fields/inputs/float.rb@@ -26,7 +26,7 @@# See COPYRIGHT and LICENSE files for more details.#++-class Project::CustomValueForm::Float < Project::CustomValueForm::Base::Input+class Projects::CustomFields::Inputs::Float < Projects::CustomFields::Inputs::Base::Inputform do |custom_value_form|custom_value_form.text_field(**input_attributes)enddiff --git a/modules/overviews/app/forms/project/custom_value_form/int.rb b/app/forms/projects/custom_fields/inputs/int.rbsimilarity index 94%rename from modules/overviews/app/forms/project/custom_value_form/int.rbrename to app/forms/projects/custom_fields/inputs/int.rbindex 2f6590c8675..e8987808890 100644--- a/modules/overviews/app/forms/project/custom_value_form/int.rb+++ b/app/forms/projects/custom_fields/inputs/int.rb@@ -26,7 +26,7 @@# See COPYRIGHT and LICENSE files for more details.#++-class Project::CustomValueForm::Int < Project::CustomValueForm::Base::Input+class Projects::CustomFields::Inputs::Int < Projects::CustomFields::Inputs::Base::Inputform do |custom_value_form|custom_value_form.text_field(**input_attributes)enddiff --git a/modules/overviews/app/forms/project/custom_value_form/multi_select_list.rb b/app/forms/projects/custom_fields/inputs/multi_select_list.rbsimilarity index 89%rename from modules/overviews/app/forms/project/custom_value_form/multi_select_list.rbrename to app/forms/projects/custom_fields/inputs/multi_select_list.rbindex ca9e26ecd5a..c117ad3df29 100644--- a/modules/overviews/app/forms/project/custom_value_form/multi_select_list.rb+++ b/app/forms/projects/custom_fields/inputs/multi_select_list.rb@@ -26,7 +26,7 @@# See COPYRIGHT and LICENSE files for more details.#++-class Project::CustomValueForm::MultiSelectList < Project::CustomValueForm::Base::Autocomplete::MultiValueInput+class Projects::CustomFields::Inputs::MultiSelectList < Projects::CustomFields::Inputs::Base::Autocomplete::MultiValueInputform do |custom_value_form|custom_value_form.autocompleter(**input_attributes) do |list|@custom_field.custom_options.each do |custom_option|@@ -45,7 +45,7 @@ class Project::CustomValueForm::MultiSelectList < Project::CustomValueForm::Baseenddef selected?(custom_option)- cf_values = @custom_field_values.reject { |custom_field_value| custom_field_value.id.nil? }+ cf_values = @custom_values.reject { |custom_value| custom_value.id.nil? }if cf_values.any?cf_values.pluck(:value).map { |value| value&.to_i }.include?(custom_option.id)diff --git a/modules/overviews/app/forms/project/custom_value_form/multi_user_select_list.rb b/app/forms/projects/custom_fields/inputs/multi_user_select_list.rbsimilarity index 80%rename from modules/overviews/app/forms/project/custom_value_form/multi_user_select_list.rbrename to app/forms/projects/custom_fields/inputs/multi_user_select_list.rbindex 360c22880ff..d75890f6604 100644--- a/modules/overviews/app/forms/project/custom_value_form/multi_user_select_list.rb+++ b/app/forms/projects/custom_fields/inputs/multi_user_select_list.rb@@ -26,10 +26,11 @@# See COPYRIGHT and LICENSE files for more details.#++-class Project::CustomValueForm::MultiUserSelectList < Project::CustomValueForm::Base::Autocomplete::MultiValueInput- include Project::CustomValueForm::Base::Autocomplete::UserQueryUtils+class Projects::CustomFields::Inputs::MultiUserSelectList < Projects::CustomFields::Inputs::Base::Autocomplete::MultiValueInput+ include Projects::CustomFields::Inputs::Base::Autocomplete::UserQueryUtilsform do |custom_value_form|+ # TODO: use user_autocompleter as seen on sharing form insteadcustom_value_form.autocompleter(**input_attributes)end@@ -44,10 +45,6 @@ class Project::CustomValueForm::MultiUserSelectList < Project::CustomValueForm::enddef init_user_ids- @custom_field_values.map(&:value)- end-- def name- "project[multi_user_custom_field_values_attributes][#{@custom_field.id}][comma_seperated_values][]"+ @custom_values.map(&:value)endenddiff --git a/modules/overviews/app/forms/project/custom_value_form/multi_version_select_list.rb b/app/forms/projects/custom_fields/inputs/multi_version_select_list.rbsimilarity index 88%rename from modules/overviews/app/forms/project/custom_value_form/multi_version_select_list.rbrename to app/forms/projects/custom_fields/inputs/multi_version_select_list.rbindex 716ff1749d0..979119e0dab 100644--- a/modules/overviews/app/forms/project/custom_value_form/multi_version_select_list.rb+++ b/app/forms/projects/custom_fields/inputs/multi_version_select_list.rb@@ -26,7 +26,7 @@# See COPYRIGHT and LICENSE files for more details.#++-class Project::CustomValueForm::MultiVersionSelectList < Project::CustomValueForm::Base::Autocomplete::MultiValueInput+class Projects::CustomFields::Inputs::MultiVersionSelectList < Projects::CustomFields::Inputs::Base::Autocomplete::MultiValueInputform do |custom_value_form|custom_value_form.autocompleter(**input_attributes) do |list|@project.versions.each do |version|@@ -45,6 +45,6 @@ class Project::CustomValueForm::MultiVersionSelectList < Project::CustomValueForenddef selected?(version)- @custom_field_values.pluck(:value).map { |value| value&.to_i }.include?(version.id)+ @custom_values.pluck(:value).map { |value| value&.to_i }.include?(version.id)endenddiff --git a/modules/overviews/app/forms/project/custom_value_form/single_select_list.rb b/app/forms/projects/custom_fields/inputs/single_select_list.rbsimilarity index 83%rename from modules/overviews/app/forms/project/custom_value_form/single_select_list.rbrename to app/forms/projects/custom_fields/inputs/single_select_list.rbindex 74d553784f4..304bfb8d223 100644--- a/modules/overviews/app/forms/project/custom_value_form/single_select_list.rb+++ b/app/forms/projects/custom_fields/inputs/single_select_list.rb@@ -26,10 +26,10 @@# See COPYRIGHT and LICENSE files for more details.#++-class Project::CustomValueForm::SingleSelectList < Project::CustomValueForm::Base::Autocomplete::SingleValueInput+class Projects::CustomFields::Inputs::SingleSelectList < Projects::CustomFields::Inputs::Base::Autocomplete::SingleValueInputform do |custom_value_form|custom_value_form.autocompleter(**input_attributes) do |list|- @custom_field_value.custom_field.custom_options.each do |custom_option|+ @custom_value.custom_field.custom_options.each do |custom_option|list.option(label: custom_option.value, value: custom_option.id,selected: selected?(custom_option)@@ -45,6 +45,6 @@ class Project::CustomValueForm::SingleSelectList < Project::CustomValueForm::Basenddef selected?(custom_option)- custom_option.id == @custom_field_value.value&.to_i || custom_option.id == @custom_field.default_value&.to_i+ custom_option.id == @custom_value.value&.to_i || custom_option.id == @custom_field.default_value&.to_iendenddiff --git a/modules/overviews/app/forms/project/custom_value_form/single_user_select_list.rb b/app/forms/projects/custom_fields/inputs/single_user_select_list.rbsimilarity index 82%rename from modules/overviews/app/forms/project/custom_value_form/single_user_select_list.rbrename to app/forms/projects/custom_fields/inputs/single_user_select_list.rbindex f7261a8ee0d..4dab874f24d 100644--- a/modules/overviews/app/forms/project/custom_value_form/single_user_select_list.rb+++ b/app/forms/projects/custom_fields/inputs/single_user_select_list.rb@@ -26,10 +26,11 @@# See COPYRIGHT and LICENSE files for more details.#++-class Project::CustomValueForm::SingleUserSelectList < Project::CustomValueForm::Base::Autocomplete::SingleValueInput- include Project::CustomValueForm::Base::Autocomplete::UserQueryUtils+class Projects::CustomFields::Inputs::SingleUserSelectList < Projects::CustomFields::Inputs::Base::Autocomplete::SingleValueInput+ include Projects::CustomFields::Inputs::Base::Autocomplete::UserQueryUtilsform do |custom_value_form|+ # TODO: use user_autocompleter as seen on sharing form insteadcustom_value_form.autocompleter(**input_attributes)end@@ -44,6 +45,6 @@ class Project::CustomValueForm::SingleUserSelectList < Project::CustomValueForm:enddef init_user_ids- [@custom_field_value.value]+ [@custom_value.value]endenddiff --git a/modules/overviews/app/forms/project/custom_value_form/single_version_select_list.rb b/app/forms/projects/custom_fields/inputs/single_version_select_list.rbsimilarity index 89%rename from modules/overviews/app/forms/project/custom_value_form/single_version_select_list.rbrename to app/forms/projects/custom_fields/inputs/single_version_select_list.rbindex 76999540966..dd99e504750 100644--- a/modules/overviews/app/forms/project/custom_value_form/single_version_select_list.rb+++ b/app/forms/projects/custom_fields/inputs/single_version_select_list.rb@@ -26,7 +26,7 @@# See COPYRIGHT and LICENSE files for more details.#++-class Project::CustomValueForm::SingleVersionSelectList < Project::CustomValueForm::Base::Autocomplete::SingleValueInput+class Projects::CustomFields::Inputs::SingleVersionSelectList < Projects::CustomFields::Inputs::Base::Autocomplete::SingleValueInputform do |custom_value_form|custom_value_form.autocompleter(**input_attributes) do |list|@project.versions.each do |version|@@ -45,6 +45,6 @@ class Project::CustomValueForm::SingleVersionSelectList < Project::CustomValueFoenddef selected?(version)- version.id == @custom_field_value.value&.to_i+ version.id == @custom_value.value&.to_iendenddiff --git a/modules/overviews/app/forms/project/custom_value_form/string.rb b/app/forms/projects/custom_fields/inputs/string.rbsimilarity index 93%rename from modules/overviews/app/forms/project/custom_value_form/string.rbrename to app/forms/projects/custom_fields/inputs/string.rbindex 25dfe7faa1e..0205cf602c9 100644--- a/modules/overviews/app/forms/project/custom_value_form/string.rb+++ b/app/forms/projects/custom_fields/inputs/string.rb@@ -26,7 +26,7 @@# See COPYRIGHT and LICENSE files for more details.#++-class Project::CustomValueForm::String < Project::CustomValueForm::Base::Input+class Projects::CustomFields::Inputs::String < Projects::CustomFields::Inputs::Base::Inputform do |custom_value_form|custom_value_form.text_field(**input_attributes)enddiff --git a/modules/overviews/app/forms/project/custom_value_form/text.rb b/app/forms/projects/custom_fields/inputs/text.rbsimilarity index 95%rename from modules/overviews/app/forms/project/custom_value_form/text.rbrename to app/forms/projects/custom_fields/inputs/text.rbindex 2e06d9f8a8e..c6683305fea 100644--- a/modules/overviews/app/forms/project/custom_value_form/text.rb+++ b/app/forms/projects/custom_fields/inputs/text.rb@@ -26,7 +26,7 @@# See COPYRIGHT and LICENSE files for more details.#++-class Project::CustomValueForm::Text < Project::CustomValueForm::Base::Input+class Projects::CustomFields::Inputs::Text < Projects::CustomFields::Inputs::Base::Inputform do |custom_value_form|# TODO: rich_text_area not working yet# Uncaught DOMException: Failed to execute 'querySelector' on 'Element': '#project_project[new_custom_field_values_attributes][xyz][value]' is not a valid selector.diff --git a/app/models/project.rb b/app/models/project.rbindex 3900ad4354f..0691b796701 100644--- a/app/models/project.rb+++ b/app/models/project.rb@@ -33,6 +33,7 @@ class Project < ApplicationRecordinclude Projects::Activityinclude Projects::Hierarchyinclude Projects::AncestorsFromRoot+ include Projects::ActsAsCustomizablePatchesinclude ::Scopes::Scoped# Maximum length for project identifiers@@ -41,8 +42,6 @@ class Project < ApplicationRecord# reserved identifiersRESERVED_IDENTIFIERS = %w(new menu).freeze- # belongs_to :project_type ---> mimic `work_package -> type -> custom_fields` structure ?-has_many :members, -> {# TODO: check whether this should# remain to be limited to User only@@ -89,7 +88,9 @@ class Project < ApplicationRecordhas_many :project_storages, dependent: :destroy, class_name: 'Storages::ProjectStorage'has_many :storages, through: :project_storages- acts_as_customizable+ acts_as_customizable # partially overridden via Projects::ActsAsCustomizablePatches in order to support sections and+ # project-leval activation of custom fields+acts_as_searchable columns: %W(#{table_name}.name #{table_name}.identifier #{table_name}.description),date_column: "#{table_name}.created_at",project_key: 'id',diff --git a/app/models/projects/acts_as_customizable_patches.rb b/app/models/projects/acts_as_customizable_patches.rbnew file mode 100644index 00000000000..d3cf2f0825e--- /dev/null+++ b/app/models/projects/acts_as_customizable_patches.rb@@ -0,0 +1,87 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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.+#++++module Projects::ActsAsCustomizablePatches+ extend ActiveSupport::Concern++ included do+ def active_custom_field_ids_of_project+ @active_custom_field_ids_of_project ||= ProjectCustomFieldProjectMapping+ .where(project_id: project.id)+ .pluck(:custom_field_id)+ end++ def available_custom_fields+ # overrides acts_as_customizable+ # in contrast to acts_as_customizable, custom_fields are enabled per project+ # thus we need to check the project_custom_field_project_mappings+ @available_custom_fields ||= ProjectCustomField+ .includes(:project_custom_field_section)+ .where(id: active_custom_field_ids_of_project)+ end++ def custom_field_section_ids+ # we need to check if a project custom field belongs to a specific section when validating+ # we need a mapping of custom_field_id => custom_field_section_id as we don't want to+ # change the code of acts_as_customizable for `custom_field_values` which does not include the custom_field_section_id+ # preloading a hash avoids n+1 queries while validating+ CustomField+ .where(id: custom_field_values.pluck(:custom_field_id))+ .pluck(:id, :custom_field_section_id)+ .to_h+ end++ def validate_custom_values+ # overrides acts_as_customizable+ # validate custom values only of the touched section+ # instead of validating ALL custom values like done in acts_as_customizable+ set_default_values! if new_record?++ custom_field_values+ .select { |custom_value| of_touched_custom_field_section?(custom_value) }+ .reject(&:marked_for_destruction?)+ .select(&:invalid?)+ .each { |custom_value| add_custom_value_errors! custom_value }+ end++ def of_touched_custom_field_section?(custom_value)+ if @touched_section_id.present?+ custom_field_section_ids[custom_value.custom_field_id] == @touched_section_id+ else+ true # validate all custom values if no specific section was marked as touched via `update_custom_field_values_of_section`+ end+ end++ def update_custom_field_values_of_section(section, params)+ # we need to set the touched section id to validate only the custom values of the touched section+ @touched_section_id = section.id++ update(params)+ end+ end+enddiff --git a/modules/overviews/app/components/project_custom_fields/sections/edit_dialog_component.html.erb b/modules/overviews/app/components/project_custom_fields/sections/edit_dialog_component.html.erbindex 83cb4ae632b..ea0e6c62072 100644--- a/modules/overviews/app/components/project_custom_fields/sections/edit_dialog_component.html.erb+++ b/modules/overviews/app/components/project_custom_fields/sections/edit_dialog_component.html.erb@@ -9,12 +9,8 @@) do |f|component_collection do |collection|collection.with_component(Primer::Alpha::Dialog::Body.new(style: "max-height: 500px;")) do- flex_layout(my: 3) do |flex|- @active_project_custom_fields_of_section.each do |project_custom_field|- flex.with_row(mb: 2, classes: 'op-project-custom-field-input-container', data: { qa_selector: "project-custom-field-input-container-#{project_custom_field.id}" }) do- render_custom_field_value_input(f, project_custom_field, project_custom_field_values_for(project_custom_field.id))- end- end+ f.fields_for(:custom_field_values) do |f|+ render(Projects::CustomFields::Form.new(f, project: @project, custom_field_section: @project_custom_field_section))endendcollection.with_component(Primer::Alpha::Dialog::Footer.new) dodiff --git a/modules/overviews/app/components/project_custom_fields/sections/edit_dialog_component.rb b/modules/overviews/app/components/project_custom_fields/sections/edit_dialog_component.rbindex 6e7fd1df73a..29d2f3d68fd 100644--- a/modules/overviews/app/components/project_custom_fields/sections/edit_dialog_component.rb+++ b/modules/overviews/app/components/project_custom_fields/sections/edit_dialog_component.rb@@ -34,77 +34,11 @@ module ProjectCustomFieldsinclude OpPrimer::ComponentHelpersdef initialize(project:,- project_custom_field_section:,- active_project_custom_fields_of_section:,- project_custom_field_values:)+ project_custom_field_section:)super@project = project@project_custom_field_section = project_custom_field_section- @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_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, custom_field_values)- if custom_field.multi_value?- render_multi_value_custom_field_input(form, custom_field, custom_field_values)- else- render_single_value_custom_field_input(form, custom_field, custom_field_values.first)- end- end-- def render_single_value_custom_field_input(form, custom_field, custom_field_value)- form_args = { custom_field:, custom_field_value:, project: @project }-- case custom_field.field_format- when "string"- render(Project::CustomValueForm::String.new(form, **form_args))- when "text"- render(Project::CustomValueForm::Text.new(form, **form_args))- when "int"- render(Project::CustomValueForm::Int.new(form, **form_args))- when "float"- render(Project::CustomValueForm::Float.new(form, **form_args))- when "list"- render(Project::CustomValueForm::SingleSelectList.new(form, **form_args))- when "date"- render(Project::CustomValueForm::Date.new(form, **form_args))- when "bool"- render(Project::CustomValueForm::Bool.new(form, **form_args))- when "user"- render(Project::CustomValueForm::SingleUserSelectList.new(form, **form_args))- when "version"- render(Project::CustomValueForm::SingleVersionSelectList.new(form, **form_args))- end- end-- def render_multi_value_custom_field_input(form, custom_field, custom_field_values)- form_args = { custom_field:, custom_field_values:, project: @project }-- case custom_field.field_format- when "list"- render(Project::CustomValueForm::MultiSelectList.new(form, **form_args))- when "user"- render(Project::CustomValueForm::MultiUserSelectList.new(form, **form_args))- when "version"- render(Project::CustomValueForm::MultiVersionSelectList.new(form, **form_args))- endendendenddiff --git a/modules/overviews/app/controllers/overviews/overviews_controller.rb b/modules/overviews/app/controllers/overviews/overviews_controller.rbindex 9d8bd5342fb..3613c5927c4 100644--- a/modules/overviews/app/controllers/overviews/overviews_controller.rb+++ b/modules/overviews/app/controllers/overviews/overviews_controller.rb@@ -9,6 +9,7 @@ module ::Overviewsmenu_item :overviewdef attributes_sidebar+ # TODO: check permissionsrender(ProjectCustomFields::SidebarComponent.new(project: @project,@@ -20,44 +21,30 @@ module ::Overviewsenddef attribute_section_dialog- section = ProjectCustomFieldSection.find(params[:section_id])-- eager_loaded_project_custom_field_values = CustomValue.where(- custom_field_id: active_project_custom_fields_of_section(section.id).pluck(:id),- customized_id: @project.id- ).to_a+ # TODO: check permissions+ @section = find_project_custom_field_sectionrender(ProjectCustomFields::Sections::EditDialogComponent.new(project: @project,- project_custom_field_section: section,- active_project_custom_fields_of_section: active_project_custom_fields_of_section(section.id),- project_custom_field_values: eager_loaded_project_custom_field_values+ project_custom_field_section: @section),layout: false)enddef update_attributes- section = find_project_custom_field_section-- has_errors = false-- ActiveRecord::Base.transaction do- modified_custom_field_values = modify_custom_field_values(section)- modified_custom_field_values = add_missing_required_custom_values(section, modified_custom_field_values)+ @section = find_project_custom_field_section- has_errors = modified_custom_field_values.any?(&:invalid?)+ # TODO: transform to contract/service-based approach with permission checks+ @project.update_custom_field_values_of_section(@section, project_attribute_params)- if has_errors- handle_errors(section, modified_custom_field_values)- else- save_custom_field_values(modified_custom_field_values)- delete_missing_custom_field_values(section, modified_custom_field_values)- delete_unused_multi_values(unused_multi_values(section))+ has_errors = @project.errors.any?- update_sidebar_component- end+ if has_errors+ handle_errors+ else+ update_sidebar_componentendrespond_to_with_turbo_streams(status: has_errors ? :unprocessable_entity : :ok)@@ -78,15 +65,12 @@ module ::Overviewsdef project_attribute_paramsparams.require(:project).permit(- custom_field_values_attributes: [:value],- new_custom_field_values_attributes: [:value],- multi_user_custom_field_values_attributes: [:custom_field_id, { comma_seperated_values: [] }],- multi_custom_field_values_attributes: [:custom_field_id, { values: [] }]+ custom_field_values: {})enddef active_project_custom_fields_grouped_by_section- # TODO: move to service+ # TODO: move to service/modelactive_custom_field_ids_of_project = ProjectCustomFieldProjectMapping.where(project_id: @project.id).pluck(:custom_field_id)@@ -107,183 +91,15 @@ module ::OverviewsProjectCustomFieldSection.find(params[:section_id])end- def modify_custom_field_values(section)- custom_field_values = []-- transaction_custom_field_values(section, :custom_field_values_attributes) do |custom_value_id, attributes|- custom_value = update_custom_value(custom_value_id, attributes)- custom_field_values << custom_value- end-- transaction_custom_field_values(section, :new_custom_field_values_attributes) do |custom_field_id, attributes|- custom_value = build_new_custom_value(custom_field_id, attributes)- custom_field_values << custom_value- end-- transaction_custom_field_values(section, :multi_custom_field_values_attributes) do |custom_field_id, attributes|- custom_field_values.concat(update_multi_custom_field_values(custom_field_id, attributes))- end-- transaction_custom_field_values(section, :multi_user_custom_field_values_attributes) do |custom_field_id, attributes|- custom_field_values.concat(update_multi_user_custom_field_values(custom_field_id, attributes))- end-- custom_field_values- end-- def unused_multi_values(section)- custom_field_values = []-- transaction_custom_field_values(section, :multi_custom_field_values_attributes) do |custom_field_id, attributes|- custom_field_values.concat(detect_unused_multi_values(custom_field_id, attributes))- end-- transaction_custom_field_values(section, :multi_user_custom_field_values_attributes) do |custom_field_id, attributes|- custom_field_values.concat(detect_unused_user_multi_values(custom_field_id, attributes))- end-- custom_field_values- end-- def transaction_custom_field_values(_section, attribute_key)- project_attribute_params[attribute_key]&.each do |custom_value_id, attributes|- yield(custom_value_id.to_i, attributes)- end- end-- def update_custom_value(custom_value_id, attributes)- custom_value = CustomValue.find(custom_value_id.to_i)- custom_value.value = attributes[:value]- custom_value- end-- def build_new_custom_value(custom_field_id, attributes)- CustomValue.new(- custom_field_id: custom_field_id.to_i,- value: attributes[:value],- customized_type: "Project",- customized_id: @project.id- )- end-- def update_multi_custom_field_values(custom_field_id, attributes)- custom_field_values = []-- existing_values_to_keep = attributes[:values] || []-- existing_values_to_keep.each do |value|- custom_value = find_or_initialize_custom_value(custom_field_id, value)- custom_field_values << custom_value- end-- custom_field_values- end-- def update_multi_user_custom_field_values(custom_field_id, attributes)- custom_field_values = []-- existing_values_to_keep = attributes[:comma_seperated_values][0]&.split(',') || []-- existing_values_to_keep.each do |value|- custom_value = find_or_initialize_custom_value(custom_field_id, value)- custom_field_values << custom_value- end-- custom_field_values- end-- def detect_unused_multi_values(custom_field_id, attributes)- existing_values_to_keep = attributes[:values] || []- unused_multi_values_to_be_deleted(custom_field_id, existing_values_to_keep)- end-- def detect_unused_user_multi_values(custom_field_id, attributes)- existing_values_to_keep = attributes[:comma_seperated_values][0]&.split(',') || []- unused_multi_values_to_be_deleted(custom_field_id, existing_values_to_keep)- end-- def unused_multi_values_to_be_deleted(custom_field_id, existing_values_to_keep)- @project.custom_values- .where(custom_field_id: custom_field_id.to_i)- .where.not(value: existing_values_to_keep)- .to_a- end-- def find_or_initialize_custom_value(custom_field_id, value)- CustomValue.find_or_initialize_by(- custom_field_id: custom_field_id.to_i,- value:,- customized_type: "Project",- customized_id: @project.id- )- end-- def handle_errors(section, modified_custom_field_values)+ def handle_errorsupdate_via_turbo_stream(component: ProjectCustomFields::Sections::EditDialogComponent.new(project: @project,- project_custom_field_section: section,- active_project_custom_fields_of_section: active_project_custom_fields_of_section(section.id),- project_custom_field_values: modified_custom_field_values+ project_custom_field_section: @section))end- def save_custom_field_values(modified_custom_field_values)- modified_custom_field_values.each(&:save!)- end-- def handle_missing_values(section, modified_custom_field_values)- mark_missing_values_as_required(section, modified_custom_field_values)- end-- def get_missing_custom_field_ids(section, modified_custom_field_values)- custom_field_ids_of_section = active_project_custom_fields_of_section(section.id).pluck(:id)- modified_custom_field_ids = modified_custom_field_values.pluck(:custom_field_id)-- custom_field_ids_of_section - modified_custom_field_ids- end-- def delete_missing_custom_field_values(section, modified_custom_field_values)- missing_custom_field_ids = get_missing_custom_field_ids(section, modified_custom_field_values)-- non_required_custom_field_ids = ProjectCustomField- .where(id: missing_custom_field_ids)- .where.not(is_required: true)- .pluck(:id)-- CustomValue- .where(custom_field_id: non_required_custom_field_ids, customized_id: @project.id)- .destroy_all- end-- def delete_unused_multi_values(custom_values_to_be_deleted)- custom_values_to_be_deleted.each(&:destroy!)- end-- def add_missing_required_custom_values(section, modified_custom_field_values)- missing_custom_field_ids = get_missing_custom_field_ids(section, modified_custom_field_values)-- required_custom_field_ids = ProjectCustomField- .where(id: missing_custom_field_ids)- .where(is_required: true)- .pluck(:id)-- required_custom_field_ids.each do |custom_field_id|- custom_value = CustomValue.find_or_initialize_by(- custom_field_id: custom_field_id.to_i,- customized_type: "Project",- customized_id: @project.id- )- custom_value.value = nil- custom_value.errors.add(:value, :blank)-- modified_custom_field_values << custom_value- end-- modified_custom_field_values- end-def update_sidebar_componentupdate_via_turbo_stream(component: ProjectCustomFields::SidebarComponent.new(@@ -293,5 +109,42 @@ module ::Overviews))end++ # resetting list values not working after refactoring, leave old code here for reference+ #+ # def unused_multi_values(section)+ # custom_field_values = []++ # transaction_custom_field_values(section, :multi_custom_field_values_attributes) do |custom_field_id, attributes|+ # custom_field_values.concat(detect_unused_multi_values(custom_field_id, attributes))+ # end++ # transaction_custom_field_values(section, :multi_user_custom_field_values_attributes) do |custom_field_id, attributes|+ # custom_field_values.concat(detect_unused_user_multi_values(custom_field_id, attributes))+ # end++ # custom_field_values+ # end++ # def detect_unused_multi_values(custom_field_id, attributes)+ # existing_values_to_keep = attributes[:values] || []+ # unused_multi_values_to_be_deleted(custom_field_id, existing_values_to_keep)+ # end++ # def detect_unused_user_multi_values(custom_field_id, attributes)+ # existing_values_to_keep = attributes[:comma_seperated_values][0]&.split(',') || []+ # unused_multi_values_to_be_deleted(custom_field_id, existing_values_to_keep)+ # end++ # def unused_multi_values_to_be_deleted(custom_field_id, existing_values_to_keep)+ # @project.custom_values+ # .where(custom_field_id: custom_field_id.to_i)+ # .where.not(value: existing_values_to_keep)+ # .to_a+ # end++ # def delete_unused_multi_values(custom_values_to_be_deleted)+ # custom_values_to_be_deleted.each(&:destroy!)+ # endendend