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 form implementations

Jonas Jabari8ac7c64f90dd
diff --git a/modules/overviews/app/forms/project/custom_value_form/base/autocomplete/multi_value_input.rb b/modules/overviews/app/forms/project/custom_value_form/base/autocomplete/multi_value_input.rb
index 05caa715f6d..a9d165138ee 100644
--- a/modules/overviews/app/forms/project/custom_value_form/base/autocomplete/multi_value_input.rb
+++ b/modules/overviews/app/forms/project/custom_value_form/base/autocomplete/multi_value_input.rb
@@ -26,18 +26,18 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Project::CustomValueForm::Base::Autocomplete::MultiValueInput < Project::CustomValueForm::Base::Input
+class Project::CustomValueForm::Base::Autocomplete::MultiValueInput < ApplicationForm
+ include Project::CustomValueForm::Base::Utils
+
def initialize(custom_field:, custom_field_values:, project:)
@custom_field = custom_field
@custom_field_values = custom_field_values
@project = project
end
- def base_config
- super.merge(
- {
- autocomplete_options:
- },
+ def input_attributes
+ base_input_attributes.merge(
+ autocomplete_options:,
invalid: invalid?,
validation_message:,
wrapper_data_attributes: {
@@ -49,13 +49,13 @@ class Project::CustomValueForm::Base::Autocomplete::MultiValueInput < Project::C
def autocomplete_options
{
multiple: true,
- decorated:,
+ decorated: decorated?,
inputId: id,
inputName: name
}
end
- def decorated
+ def decorated?
raise NotImplementedError
end
@@ -63,10 +63,6 @@ class Project::CustomValueForm::Base::Autocomplete::MultiValueInput < Project::C
"project[multi_custom_field_values_attributes][#{@custom_field.id}][values]"
end
- def value
- nil
- end
-
def invalid?
@custom_field_values.any? { |custom_field_value| custom_field_value.errors.any? }
end
diff --git a/modules/overviews/app/forms/project/custom_value_form/base/autocomplete/single_value_input.rb b/modules/overviews/app/forms/project/custom_value_form/base/autocomplete/single_value_input.rb
index 2e1d1ed887f..83a1fe61828 100644
--- a/modules/overviews/app/forms/project/custom_value_form/base/autocomplete/single_value_input.rb
+++ b/modules/overviews/app/forms/project/custom_value_form/base/autocomplete/single_value_input.rb
@@ -26,12 +26,18 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Project::CustomValueForm::Base::Autocomplete::SingleValueInput < Project::CustomValueForm::Base::Input
- def base_config
- super.merge(
- {
- autocomplete_options:
- },
+class Project::CustomValueForm::Base::Autocomplete::SingleValueInput < ApplicationForm
+ include Project::CustomValueForm::Base::Utils
+
+ def initialize(custom_field:, custom_field_value:, project:)
+ @custom_field = custom_field
+ @custom_field_value = custom_field_value
+ @project = project
+ end
+
+ def input_attributes
+ base_input_attributes.merge(
+ autocomplete_options:,
invalid: invalid?,
validation_message:,
wrapper_data_attributes: {
@@ -43,20 +49,16 @@ class Project::CustomValueForm::Base::Autocomplete::SingleValueInput < Project::
def autocomplete_options
{
multiple: false,
- decorated:,
+ decorated: decorated?,
inputId: id,
inputName: name
}
end
- def decorated
+ def decorated?
raise NotImplementedError
end
- def value
- nil
- end
-
def invalid?
@custom_field_value.errors.any?
end
diff --git a/modules/overviews/app/forms/project/custom_value_form/base/autocomplete/user_query_utils.rb b/modules/overviews/app/forms/project/custom_value_form/base/autocomplete/user_query_utils.rb
new file mode 100644
index 00000000000..99c16b8799f
--- /dev/null
+++ b/modules/overviews/app/forms/project/custom_value_form/base/autocomplete/user_query_utils.rb
@@ -0,0 +1,67 @@
+#-- 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.
+#++
+
+module Project::CustomValueForm::Base::Autocomplete::UserQueryUtils
+ def user_autocomplete_options
+ {
+ placeholder: I18n.t(:label_user_search),
+ resource:,
+ filters:,
+ searchKey: search_key,
+ inputValue: input_value
+ }
+ end
+
+ def resource
+ 'principals'
+ end
+
+ def search_key
+ 'any_name_attribute'
+ end
+
+ def filters
+ [
+ { name: 'type', operator: '=', values: ['User', 'Group', 'PlaceholderUser'] },
+ { name: 'member', operator: '=', values: [@project.id.to_s] },
+ { name: 'status', operator: '!', values: [User.statuses["locked"].to_s] }
+ ]
+ end
+
+ def input_value
+ "?#{input_values_filter}"
+ end
+
+ def input_values_filter
+ user_filter = { "type" => { "operator" => "=", "values" => ["User"] } }
+ id_filter = { "id" => { "operator" => "=", "values" => init_user_ids } }
+
+ filters = [user_filter, id_filter]
+ URI.encode_www_form("filters" => filters.to_json)
+ end
+end
diff --git a/modules/overviews/app/forms/project/custom_value_form/base/input.rb b/modules/overviews/app/forms/project/custom_value_form/base/input.rb
index eb6cf8e4646..b5b9809ee29 100644
--- a/modules/overviews/app/forms/project/custom_value_form/base/input.rb
+++ b/modules/overviews/app/forms/project/custom_value_form/base/input.rb
@@ -27,45 +27,24 @@
#++
class Project::CustomValueForm::Base::Input < ApplicationForm
+ include Project::CustomValueForm::Base::Utils
+
def initialize(custom_field:, custom_field_value:, project:)
@custom_field = custom_field
@custom_field_value = custom_field_value
@project = project
end
- def base_config
- {
- name:,
- id:,
- scope_name_to_model: false,
- scope_id_to_model: false,
- placeholder: @custom_field.name,
- label: @custom_field.name,
- value:,
- required: @custom_field.is_required?,
- data: {
- 'qa-field-name': qa_field_name
+ def input_attributes
+ base_input_attributes.merge(
+ {
+ data: { 'qa-field-name': qa_field_name },
+ value:
}
- }
- end
-
- def 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
- end
-
- def id
- name.gsub(/[\[\]]/, "_")
+ )
end
def value
@custom_field_value.value || @custom_field.default_value
end
-
- def qa_field_name
- @custom_field.attribute_name(:kebab_case)
- end
end
diff --git a/modules/overviews/app/forms/project/custom_value_form/base/utils.rb b/modules/overviews/app/forms/project/custom_value_form/base/utils.rb
new file mode 100644
index 00000000000..16ab8e4b3a6
--- /dev/null
+++ b/modules/overviews/app/forms/project/custom_value_form/base/utils.rb
@@ -0,0 +1,57 @@
+#-- 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.
+#++
+
+module Project::CustomValueForm::Base::Utils
+ def 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?
+ }
+ end
+
+ def id
+ name.gsub(/[\[\]]/, "_")
+ end
+
+ def 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
+ end
+
+ def qa_field_name
+ @custom_field.attribute_name(:kebab_case)
+ end
+end
diff --git a/modules/overviews/app/forms/project/custom_value_form/bool.rb b/modules/overviews/app/forms/project/custom_value_form/bool.rb
index 9b7087fd893..29cd0f79dc0 100644
--- a/modules/overviews/app/forms/project/custom_value_form/bool.rb
+++ b/modules/overviews/app/forms/project/custom_value_form/bool.rb
@@ -28,10 +28,10 @@
class Project::CustomValueForm::Bool < Project::CustomValueForm::Base::Input
form do |custom_value_form|
- custom_value_form.check_box(**base_config)
+ custom_value_form.check_box(**input_attributes)
end
- def base_config
+ def input_attributes
super.merge({
value: "1",
unchecked_value: "0",
diff --git a/modules/overviews/app/forms/project/custom_value_form/date.rb b/modules/overviews/app/forms/project/custom_value_form/date.rb
index c0a925aa70c..00b9cc8cc63 100644
--- a/modules/overviews/app/forms/project/custom_value_form/date.rb
+++ b/modules/overviews/app/forms/project/custom_value_form/date.rb
@@ -28,10 +28,10 @@
class Project::CustomValueForm::Date < Project::CustomValueForm::Base::Input
form do |custom_value_form|
- custom_value_form.text_field(**base_config)
+ custom_value_form.text_field(**input_attributes)
end
- def base_config
+ def input_attributes
super.merge({ type: "date" })
end
end
diff --git a/modules/overviews/app/forms/project/custom_value_form/float.rb b/modules/overviews/app/forms/project/custom_value_form/float.rb
index a4ddbf6a7f0..2d12a23b91d 100644
--- a/modules/overviews/app/forms/project/custom_value_form/float.rb
+++ b/modules/overviews/app/forms/project/custom_value_form/float.rb
@@ -28,10 +28,10 @@
class Project::CustomValueForm::Float < Project::CustomValueForm::Base::Input
form do |custom_value_form|
- custom_value_form.text_field(**base_config)
+ custom_value_form.text_field(**input_attributes)
end
- def base_config
+ def input_attributes
super.merge({ type: "number", step: :any })
end
end
diff --git a/modules/overviews/app/forms/project/custom_value_form/int.rb b/modules/overviews/app/forms/project/custom_value_form/int.rb
index 0e336eb44df..2f6590c8675 100644
--- a/modules/overviews/app/forms/project/custom_value_form/int.rb
+++ b/modules/overviews/app/forms/project/custom_value_form/int.rb
@@ -28,10 +28,10 @@
class Project::CustomValueForm::Int < Project::CustomValueForm::Base::Input
form do |custom_value_form|
- custom_value_form.text_field(**base_config)
+ custom_value_form.text_field(**input_attributes)
end
- def base_config
+ def input_attributes
super.merge({ type: "number", step: 1 })
end
end
diff --git a/modules/overviews/app/forms/project/custom_value_form/multi_select_list.rb b/modules/overviews/app/forms/project/custom_value_form/multi_select_list.rb
index 90efc15d259..ca9e26ecd5a 100644
--- a/modules/overviews/app/forms/project/custom_value_form/multi_select_list.rb
+++ b/modules/overviews/app/forms/project/custom_value_form/multi_select_list.rb
@@ -28,7 +28,7 @@
class Project::CustomValueForm::MultiSelectList < Project::CustomValueForm::Base::Autocomplete::MultiValueInput
form do |custom_value_form|
- custom_value_form.autocompleter(**base_config) do |list|
+ 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,
@@ -40,7 +40,7 @@ class Project::CustomValueForm::MultiSelectList < Project::CustomValueForm::Base
private
- def decorated
+ def decorated?
true
end
diff --git a/modules/overviews/app/forms/project/custom_value_form/multi_user_select_list.rb b/modules/overviews/app/forms/project/custom_value_form/multi_user_select_list.rb
index 579f2fed1d2..360c22880ff 100644
--- a/modules/overviews/app/forms/project/custom_value_form/multi_user_select_list.rb
+++ b/modules/overviews/app/forms/project/custom_value_form/multi_user_select_list.rb
@@ -27,47 +27,27 @@
#++
class Project::CustomValueForm::MultiUserSelectList < Project::CustomValueForm::Base::Autocomplete::MultiValueInput
+ include Project::CustomValueForm::Base::Autocomplete::UserQueryUtils
+
form do |custom_value_form|
- custom_value_form.autocompleter(**base_config)
+ custom_value_form.autocompleter(**input_attributes)
end
private
- def decorated
+ def decorated?
false
end
def autocomplete_options
- super.merge({
- placeholder: "Search for users",
- resource: 'principals',
- filters:,
- searchKey: 'any_name_attribute',
- inputValue: input_value
- })
- end
-
- def name
- "project[multi_user_custom_field_values_attributes][#{@custom_field.id}][comma_seperated_values][]"
+ super.merge(user_autocomplete_options)
end
- def filters
- [
- { name: 'type', operator: '=', values: ['User', 'Group', 'PlaceholderUser'] },
- { name: 'member', operator: '=', values: [@project.id.to_s] },
- { name: 'status', operator: '!', values: [User.statuses["locked"].to_s] }
- ]
+ def init_user_ids
+ @custom_field_values.map(&:value)
end
- def input_value
- "?#{input_values_filter}"
- end
-
- def input_values_filter
- user_filter = { "type" => { "operator" => "=", "values" => ["User"] } }
- id_filter = { "id" => { "operator" => "=", "values" => @custom_field_values.map(&:value) } }
-
- filters = [user_filter, id_filter]
- URI.encode_www_form("filters" => filters.to_json)
+ def name
+ "project[multi_user_custom_field_values_attributes][#{@custom_field.id}][comma_seperated_values][]"
end
end
diff --git a/modules/overviews/app/forms/project/custom_value_form/multi_version_select_list.rb b/modules/overviews/app/forms/project/custom_value_form/multi_version_select_list.rb
index ec773aecbc1..716ff1749d0 100644
--- a/modules/overviews/app/forms/project/custom_value_form/multi_version_select_list.rb
+++ b/modules/overviews/app/forms/project/custom_value_form/multi_version_select_list.rb
@@ -28,7 +28,7 @@
class Project::CustomValueForm::MultiVersionSelectList < Project::CustomValueForm::Base::Autocomplete::MultiValueInput
form do |custom_value_form|
- custom_value_form.autocompleter(**base_config) do |list|
+ custom_value_form.autocompleter(**input_attributes) do |list|
@project.versions.each do |version|
list.option(
label: version.name, value: version.id,
@@ -40,7 +40,7 @@ class Project::CustomValueForm::MultiVersionSelectList < Project::CustomValueFor
private
- def decorated
+ def decorated?
true
end
diff --git a/modules/overviews/app/forms/project/custom_value_form/single_select_list.rb b/modules/overviews/app/forms/project/custom_value_form/single_select_list.rb
index d8d70d73182..74d553784f4 100644
--- a/modules/overviews/app/forms/project/custom_value_form/single_select_list.rb
+++ b/modules/overviews/app/forms/project/custom_value_form/single_select_list.rb
@@ -28,7 +28,7 @@
class Project::CustomValueForm::SingleSelectList < Project::CustomValueForm::Base::Autocomplete::SingleValueInput
form do |custom_value_form|
- custom_value_form.autocompleter(**base_config) do |list|
+ custom_value_form.autocompleter(**input_attributes) do |list|
@custom_field_value.custom_field.custom_options.each do |custom_option|
list.option(
label: custom_option.value, value: custom_option.id,
@@ -40,7 +40,7 @@ class Project::CustomValueForm::SingleSelectList < Project::CustomValueForm::Bas
private
- def decorated
+ def decorated?
true
end
diff --git a/modules/overviews/app/forms/project/custom_value_form/single_user_select_list.rb b/modules/overviews/app/forms/project/custom_value_form/single_user_select_list.rb
index ba293ba04bc..f7261a8ee0d 100644
--- a/modules/overviews/app/forms/project/custom_value_form/single_user_select_list.rb
+++ b/modules/overviews/app/forms/project/custom_value_form/single_user_select_list.rb
@@ -27,43 +27,23 @@
#++
class Project::CustomValueForm::SingleUserSelectList < Project::CustomValueForm::Base::Autocomplete::SingleValueInput
+ include Project::CustomValueForm::Base::Autocomplete::UserQueryUtils
+
form do |custom_value_form|
- custom_value_form.autocompleter(**base_config)
+ custom_value_form.autocompleter(**input_attributes)
end
private
- def decorated
+ def decorated?
false
end
def autocomplete_options
- super.merge({
- placeholder: "Search for a user",
- resource: 'principals',
- filters:,
- searchKey: 'any_name_attribute',
- inputValue: input_value
- })
- end
-
- def filters
- [
- { name: 'type', operator: '=', values: ['User', 'Group', 'PlaceholderUser'] },
- { name: 'member', operator: '=', values: [@project.id.to_s] },
- { name: 'status', operator: '!', values: [User.statuses["locked"].to_s] }
- ]
+ super.merge(user_autocomplete_options)
end
- def input_value
- "?#{input_values_filter}"
- end
-
- def input_values_filter
- user_filter = { "type" => { "operator" => "=", "values" => ['User', 'Group', 'PlaceholderUser'] } }
- id_filter = { "id" => { "operator" => "=", "values" => @custom_field_value.value } }
-
- filters = [user_filter, id_filter]
- URI.encode_www_form("filters" => filters.to_json)
+ def init_user_ids
+ [@custom_field_value.value]
end
end
diff --git a/modules/overviews/app/forms/project/custom_value_form/single_version_select_list.rb b/modules/overviews/app/forms/project/custom_value_form/single_version_select_list.rb
index 72f1d1a1b4a..76999540966 100644
--- a/modules/overviews/app/forms/project/custom_value_form/single_version_select_list.rb
+++ b/modules/overviews/app/forms/project/custom_value_form/single_version_select_list.rb
@@ -28,7 +28,7 @@
class Project::CustomValueForm::SingleVersionSelectList < Project::CustomValueForm::Base::Autocomplete::SingleValueInput
form do |custom_value_form|
- custom_value_form.autocompleter(**base_config) do |list|
+ custom_value_form.autocompleter(**input_attributes) do |list|
@project.versions.each do |version|
list.option(
label: version.name, value: version.id,
@@ -40,7 +40,7 @@ class Project::CustomValueForm::SingleVersionSelectList < Project::CustomValueFo
private
- def decorated
+ def decorated?
true
end
diff --git a/modules/overviews/app/forms/project/custom_value_form/string.rb b/modules/overviews/app/forms/project/custom_value_form/string.rb
index 5b535eb950a..25dfe7faa1e 100644
--- a/modules/overviews/app/forms/project/custom_value_form/string.rb
+++ b/modules/overviews/app/forms/project/custom_value_form/string.rb
@@ -28,6 +28,6 @@
class Project::CustomValueForm::String < Project::CustomValueForm::Base::Input
form do |custom_value_form|
- custom_value_form.text_field(**base_config)
+ custom_value_form.text_field(**input_attributes)
end
end
diff --git a/modules/overviews/app/forms/project/custom_value_form/text.rb b/modules/overviews/app/forms/project/custom_value_form/text.rb
index 5c4758b3f74..2e06d9f8a8e 100644
--- a/modules/overviews/app/forms/project/custom_value_form/text.rb
+++ b/modules/overviews/app/forms/project/custom_value_form/text.rb
@@ -33,6 +33,6 @@ class Project::CustomValueForm::Text < Project::CustomValueForm::Base::Input
# --> rich_text_area is not using the configured id, which is not scoped to model via base_config
# --> ids with '[' ']' are not valid selectors
# using simple text area for now
- custom_value_form.text_area(**base_config)
+ custom_value_form.text_area(**input_attributes)
end
end