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

Base values on input so it's easier to pass custom options

Oliver Günther35c73795626c
diff --git a/app/forms/custom_fields/inputs/base/autocomplete/multi_value_input.rb b/app/forms/custom_fields/inputs/base/autocomplete/multi_value_input.rb
index fa46850be4c..37a2304fe83 100644
--- a/app/forms/custom_fields/inputs/base/autocomplete/multi_value_input.rb
+++ b/app/forms/custom_fields/inputs/base/autocomplete/multi_value_input.rb
@@ -26,15 +26,7 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class CustomFields::Inputs::Base::Autocomplete::MultiValueInput < ApplicationForm
- include CustomFields::Inputs::Base::Utils
-
- def initialize(custom_field:, custom_values:, object:)
- @custom_field = custom_field
- @custom_values = custom_values
- @object = object
- end
-
+class CustomFields::Inputs::Base::Autocomplete::MultiValueInput < CustomFields::Inputs::Base::Input
def input_attributes
base_input_attributes.merge(
autocomplete_options:,
@@ -55,11 +47,15 @@ class CustomFields::Inputs::Base::Autocomplete::MultiValueInput < ApplicationFor
raise NotImplementedError
end
+ def custom_values
+ @custom_values ||= @object.custom_values_for_custom_field(id: @custom_field.id)
+ end
+
def invalid?
- @custom_values.any? { |custom_value| custom_value.errors.any? }
+ custom_values.any? { |custom_value| custom_value.errors.any? }
end
def validation_message
- @custom_values.map { |custom_value| custom_value.errors.full_messages }.join(', ') if invalid?
+ custom_values.map { |custom_value| custom_value.errors.full_messages }.join(', ') if invalid?
end
end
diff --git a/app/forms/custom_fields/inputs/base/autocomplete/single_value_input.rb b/app/forms/custom_fields/inputs/base/autocomplete/single_value_input.rb
index 5472cecb9cc..7cc17044668 100644
--- a/app/forms/custom_fields/inputs/base/autocomplete/single_value_input.rb
+++ b/app/forms/custom_fields/inputs/base/autocomplete/single_value_input.rb
@@ -26,15 +26,7 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class CustomFields::Inputs::Base::Autocomplete::SingleValueInput < ApplicationForm
- include CustomFields::Inputs::Base::Utils
-
- def initialize(custom_field:, custom_value:, object:)
- @custom_field = custom_field
- @custom_value = custom_value
- @object = object
- end
-
+class CustomFields::Inputs::Base::Autocomplete::SingleValueInput < CustomFields::Inputs::Base::Input
def input_attributes
base_input_attributes.merge(
autocomplete_options:,
@@ -54,12 +46,4 @@ class CustomFields::Inputs::Base::Autocomplete::SingleValueInput < ApplicationFo
def decorated?
raise NotImplementedError
end
-
- def invalid?
- @custom_value.errors.any?
- end
-
- def validation_message
- @custom_value.errors.full_messages.join(', ') if invalid?
- 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 8312f70c59f..75bceebed24 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
@@ -36,7 +36,7 @@ module CustomFields::Inputs::Base::Autocomplete::UserQueryUtils
searchKey: search_key,
inputValue: custom_input_value,
focusDirectly: false,
- appendTo: 'body'
+ appendTo: append_to
}
end
@@ -48,6 +48,10 @@ module CustomFields::Inputs::Base::Autocomplete::UserQueryUtils
'any_name_attribute'
end
+ def append_to
+ options.fetch(:wrapper_id, 'body')
+ end
+
def filters
[
{ name: 'type', operator: '=', values: ['User', 'Group', 'PlaceholderUser'] },
diff --git a/app/forms/custom_fields/inputs/base/input.rb b/app/forms/custom_fields/inputs/base/input.rb
index e8b69c87657..a578a692ef9 100644
--- a/app/forms/custom_fields/inputs/base/input.rb
+++ b/app/forms/custom_fields/inputs/base/input.rb
@@ -29,10 +29,12 @@
class CustomFields::Inputs::Base::Input < ApplicationForm
include CustomFields::Inputs::Base::Utils
- def initialize(custom_field:, custom_value:, object:)
+ attr_reader :options
+
+ def initialize(custom_field:, object:, **options)
@custom_field = custom_field
- @custom_value = custom_value
@object = object
+ @options = options
end
def input_attributes
@@ -44,11 +46,15 @@ class CustomFields::Inputs::Base::Input < ApplicationForm
)
end
+ def custom_value
+ @custom_value ||= @object.custom_value_for(@custom_field.id)
+ end
+
def invalid?
- @custom_value.errors.any?
+ custom_value.errors.any?
end
def validation_message
- @custom_value.errors.full_messages.join(', ') if invalid?
+ custom_value.errors.full_messages.join(', ') if invalid?
end
end
diff --git a/app/forms/projects/custom_fields/form.rb b/app/forms/projects/custom_fields/form.rb
index ef19e8bc668..5d4c00542f7 100644
--- a/app/forms/projects/custom_fields/form.rb
+++ b/app/forms/projects/custom_fields/form.rb
@@ -35,11 +35,12 @@ module Projects::CustomFields
end
end
- def initialize(project:, custom_field_section: nil, custom_field: nil)
+ def initialize(project:, custom_field_section: nil, custom_field: nil, wrapper_id: nil)
super()
@project = project
@custom_field_section = custom_field_section
@custom_field = custom_field
+ @wrapper_id = wrapper_id
if @custom_field_section.present? && @custom_field.present?
raise ArgumentError,
@@ -76,8 +77,7 @@ module Projects::CustomFields
# - rich text editor is not yet supported
def single_value_custom_field_input(builder, custom_field)
- custom_value = @project.custom_value_for(custom_field.id)
- form_args = { custom_field:, custom_value:, object: @project }
+ form_args = { custom_field:, object: @project, wrapper_id: @wrapper_id }
case custom_field.field_format
when "string"
@@ -102,8 +102,7 @@ module Projects::CustomFields
end
def multi_value_custom_field_input(builder, custom_field)
- custom_values = @project.custom_values_for_custom_field(id: custom_field.id)
- form_args = { custom_field:, custom_values:, object: @project }
+ form_args = { custom_field:, object: @project, wrapper_id: @wrapper_id }
case custom_field.field_format
when "list"
diff --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.erb
index d71ff4e93ba..a73b0457c9f 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
@@ -1,4 +1,3 @@
-
<%=
content_tag("turbo-frame", id: "edit-project-custom-fields-dialog-#{@project_custom_field_section.id}-frame") do
component_wrapper(data: { qa_selector: 'async-dialog-content' }) do
@@ -10,7 +9,12 @@
component_collection do |collection|
# TODO: remove inline style
collection.with_component(Primer::Alpha::Dialog::Body.new(my: 3, style: "max-height: 500px;")) do
- render(Projects::CustomFields::Form.new(f, project: @project, custom_field_section: @project_custom_field_section))
+ render(Projects::CustomFields::Form.new(
+ f,
+ project: @project,
+ custom_field_section: @project_custom_field_section,
+ wrapper_id: "#edit-project-custom-fields-dialog-#{@project_custom_field_section.id}"
+ ))
end
collection.with_component(Primer::Alpha::Dialog::Footer.new) do
component_collection do |footer_collection|