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

Take over custom fields

Oliver Günther74c8527685b0
diff --git a/app/forms/custom_fields/custom_field_rendering.rb b/app/forms/custom_fields/custom_field_rendering.rb
new file mode 100644
index 00000000000..3e8d16af729
--- /dev/null
+++ b/app/forms/custom_fields/custom_field_rendering.rb
@@ -0,0 +1,107 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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 CustomFields::CustomFieldRendering
+ def render_custom_fields(form:)
+ custom_fields.each do |custom_field|
+ form.fields_for(:custom_field_values) do |builder|
+ custom_field_input(builder, custom_field)
+ end
+ end
+ end
+
+ # override if you want to pass more attributes
+ def additional_custom_field_input_arguments
+ {}
+ end
+
+ def custom_fields
+ raise NotImplementedError, "#custom_fields method needs to be overwritten and provide all custom fields we want to show"
+ end
+
+ private
+
+ def custom_field_input(builder, custom_field)
+ if custom_field.multi_value?
+ multi_value_custom_field_input(builder, custom_field)
+ else
+ single_value_custom_field_input(builder, custom_field)
+ end
+ end
+
+ def form_arguments(custom_field)
+ {
+ custom_field: custom_field,
+ object: model
+ }.merge(additional_custom_field_input_arguments)
+ end
+
+ # TBD: transform inputs called below to primer form dsl instead of form classes?
+ # TODOS:
+ # - initial values for user inputs are not displayed
+ # - allow/disallow-non-open version setting is not yet respected in the version selector
+ # - rich text editor is not yet supported
+
+ def single_value_custom_field_input(builder, custom_field)
+ form_args = form_arguments(custom_field)
+
+ case custom_field.field_format
+ when "string", "link"
+ CustomFields::Inputs::String.new(builder, **form_args)
+ when "text"
+ CustomFields::Inputs::Text.new(builder, **form_args)
+ when "int"
+ CustomFields::Inputs::Int.new(builder, **form_args)
+ when "float"
+ CustomFields::Inputs::Float.new(builder, **form_args)
+ when "list"
+ CustomFields::Inputs::SingleSelectList.new(builder, **form_args)
+ when "date"
+ CustomFields::Inputs::Date.new(builder, **form_args)
+ when "bool"
+ CustomFields::Inputs::Bool.new(builder, **form_args)
+ when "user"
+ CustomFields::Inputs::SingleUserSelectList.new(builder, **form_args)
+ when "version"
+ CustomFields::Inputs::SingleVersionSelectList.new(builder, **form_args)
+ end
+ end
+
+ def multi_value_custom_field_input(builder, custom_field)
+ form_args = form_arguments(custom_field)
+
+ case custom_field.field_format
+ when "list"
+ CustomFields::Inputs::MultiSelectList.new(builder, **form_args)
+ when "user"
+ CustomFields::Inputs::MultiUserSelectList.new(builder, **form_args)
+ when "version"
+ CustomFields::Inputs::MultiVersionSelectList.new(builder, **form_args)
+ end
+ end
+end
diff --git a/app/forms/custom_fields/inputs/base/autocomplete/user_query_utils.rb b/app/forms/custom_fields/inputs/base/autocomplete/user_query_utils.rb
index e2d17241aed..72f91a4d873 100644
--- a/app/forms/custom_fields/inputs/base/autocomplete/user_query_utils.rb
+++ b/app/forms/custom_fields/inputs/base/autocomplete/user_query_utils.rb
@@ -51,10 +51,17 @@ module CustomFields::Inputs::Base::Autocomplete::UserQueryUtils
end
def filters
- [
+ filters = [
{ name: "type", operator: "=", values: ["User", "Group", "PlaceholderUser"] },
- { name: "member", operator: "=", values: [@object.id.to_s] },
{ name: "status", operator: "!", values: [Principal.statuses["locked"].to_s] }
]
+
+ if @object.is_a?(Project)
+ filters << { name: "member", operator: "=", values: [@object.id.to_s] }
+ elsif @object.respond_to?(:project_id)
+ filters << { name: "member", operator: "=", values: [@object.project_id.to_s] }
+ end
+
+ filters
end
end
diff --git a/app/forms/custom_fields/inputs/base/input.rb b/app/forms/custom_fields/inputs/base/input.rb
index 6e30709cc0f..22e3b8a56ac 100644
--- a/app/forms/custom_fields/inputs/base/input.rb
+++ b/app/forms/custom_fields/inputs/base/input.rb
@@ -32,6 +32,8 @@ class CustomFields::Inputs::Base::Input < ApplicationForm
attr_reader :options
def initialize(custom_field:, object:, **options)
+ super()
+
@custom_field = custom_field
@object = object
@options = options
diff --git a/app/forms/custom_fields/inputs/multi_select_list.rb b/app/forms/custom_fields/inputs/multi_select_list.rb
index 832ddb524b4..85434337830 100644
--- a/app/forms/custom_fields/inputs/multi_select_list.rb
+++ b/app/forms/custom_fields/inputs/multi_select_list.rb
@@ -31,16 +31,18 @@ class CustomFields::Inputs::MultiSelectList < CustomFields::Inputs::Base::Autoco
# autocompleter does not set key with blank value if nothing is selected or input is cleared
# in order to let acts_as_customizable handle the clearing of the value, we need to set the value to blank via a hidden field
# which sends blank if autocompleter is cleared
- custom_value_form.hidden(**input_attributes.merge(
+ custom_value_form.hidden(
+ **input_attributes,
scope_name_to_model: false,
- name: "#{@object.class.name.downcase}[custom_field_values][#{input_attributes[:name]}][]",
+ name: "#{@object.model_name.element}[custom_field_values][#{input_attributes[:name]}][]",
value:
- ))
+ )
custom_value_form.autocompleter(**input_attributes) do |list|
@custom_field.custom_options.each do |custom_option|
list.option(
- label: custom_option.value, value: custom_option.id,
+ label: custom_option.value,
+ value: custom_option.id,
selected: selected?(custom_option)
)
end
diff --git a/app/forms/custom_fields/inputs/multi_user_select_list.rb b/app/forms/custom_fields/inputs/multi_user_select_list.rb
index f086b8b8948..dfc7e691dd8 100644
--- a/app/forms/custom_fields/inputs/multi_user_select_list.rb
+++ b/app/forms/custom_fields/inputs/multi_user_select_list.rb
@@ -33,11 +33,12 @@ class CustomFields::Inputs::MultiUserSelectList < CustomFields::Inputs::Base::Au
# autocompleter does not set key with blank value if nothing is selected or input is cleared
# in order to let acts_as_customizable handle the clearing of the value, we need to set the value to blank via a hidden field
# which sends blank if autocompleter is cleared
- custom_value_form.hidden(**input_attributes.merge(
+ custom_value_form.hidden(
+ **input_attributes,
scope_name_to_model: false,
- name: "#{@object.class.name.downcase}[custom_field_values][#{input_attributes[:name]}][]",
+ name: "#{@object.model_name.element}[custom_field_values][#{input_attributes[:name]}][]",
value:
- ))
+ )
custom_value_form.autocompleter(**input_attributes)
end
diff --git a/app/forms/custom_fields/inputs/multi_version_select_list.rb b/app/forms/custom_fields/inputs/multi_version_select_list.rb
index f03ce7672f6..2740a117e84 100644
--- a/app/forms/custom_fields/inputs/multi_version_select_list.rb
+++ b/app/forms/custom_fields/inputs/multi_version_select_list.rb
@@ -35,16 +35,18 @@ class CustomFields::Inputs::MultiVersionSelectList < CustomFields::Inputs::Base:
# autocompleter does not set key with blank value if nothing is selected or input is cleared
# in order to let acts_as_customizable handle the clearing of the value, we need to set the value to blank via a hidden field
# which sends blank if autocompleter is cleared
- custom_value_form.hidden(**input_attributes.merge(
+ custom_value_form.hidden(
+ **input_attributes,
scope_name_to_model: false,
- name: "#{@object.class.name.downcase}[custom_field_values][#{input_attributes[:name]}][]",
+ name: "#{@object.model_name.element}[custom_field_values][#{input_attributes[:name]}][]",
value:
- ))
+ )
custom_value_form.autocompleter(**input_attributes) do |list|
assignable_custom_field_values(@custom_field).each do |version|
list.option(
- label: version.name, value: version.id,
+ label: version.name,
+ value: version.id,
selected: selected?(version)
)
end