Skip to content

Commit cac9bb8

Browse files
committed
VersionListComponent for inplaceEditComponent
1 parent f894f84 commit cac9bb8

File tree

4 files changed

+109
-7
lines changed

4 files changed

+109
-7
lines changed

app/components/open_project/common/inplace_edit_fields/display_fields/select_list_component.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def render_display_value
4444
if custom_field?
4545
custom_field_values
4646
else
47-
value.is_a?(Array) ? value.join(", ") : value
47+
value.is_a?(Array) ? value.map(&:to_s).join(", ") : value.to_s
4848
end
4949
else
5050
t("placeholders.default")

app/components/open_project/common/inplace_edit_fields/select_list_component.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,23 @@ def initialize(form:, attribute:, model:, **system_arguments)
5151
@system_arguments[:label] ||= model.class.human_attribute_name(attribute)
5252

5353
@system_arguments[:autocomplete_options] ||= {}
54-
@system_arguments[:autocomplete_options][:model] = { id: model.id, name: model.name }
55-
@system_arguments[:autocomplete_options][:inputName] = attribute
56-
@system_arguments[:autocomplete_options][:focusDirectly] = true
57-
@system_arguments[:autocomplete_options][:closeOnSelect] = false
54+
@system_arguments[:autocomplete_options][:model] ||= { id: model.id, name: model.name }
55+
@system_arguments[:autocomplete_options][:inputName] ||= attribute
56+
if @system_arguments[:autocomplete_options][:focusDirectly].nil?
57+
@system_arguments[:autocomplete_options][:focusDirectly] =
58+
true
59+
end
60+
if @system_arguments[:autocomplete_options][:closeOnSelect].nil?
61+
@system_arguments[:autocomplete_options][:closeOnSelect] =
62+
false
63+
end
5864
end
5965

6066
def call
6167
if custom_field?
6268
render_custom_field_input
6369
else
64-
form.autocompleter(name: attribute, **@system_arguments)
70+
render_autocompleter
6571
end
6672

6773
form.group(layout: :horizontal, justify_content: :flex_end) do |button_group|
@@ -92,6 +98,10 @@ def render_custom_field_input
9298
end
9399
end
94100

101+
def render_autocompleter
102+
form.autocompleter(name: attribute, **@system_arguments)
103+
end
104+
95105
def custom_field?
96106
attribute.to_s.start_with?("custom_field_")
97107
end
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# frozen_string_literal: true
2+
3+
#-- copyright
4+
# OpenProject is an open source project management software.
5+
# Copyright (C) the OpenProject GmbH
6+
#
7+
# This program is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU General Public License version 3.
9+
#
10+
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
11+
# Copyright (C) 2006-2013 Jean-Philippe Lang
12+
# Copyright (C) 2010-2013 the ChiliProject Team
13+
#
14+
# This program is free software; you can redistribute it and/or
15+
# modify it under the terms of the GNU General Public License
16+
# as published by the Free Software Foundation; either version 2
17+
# of the License, or (at your option) any later version.
18+
#
19+
# This program is distributed in the hope that it will be useful,
20+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
# GNU General Public License for more details.
23+
#
24+
# You should have received a copy of the GNU General Public License
25+
# along with this program; if not, write to the Free Software
26+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27+
#
28+
# See COPYRIGHT and LICENSE files for more details.
29+
#++
30+
31+
module OpenProject
32+
module Common
33+
module InplaceEditFields
34+
class VersionSelectListComponent < SelectListComponent
35+
attr_reader :form, :attribute, :model
36+
37+
def initialize(form:, attribute:, model:, **system_arguments)
38+
super
39+
40+
unless custom_field?
41+
assign_defaults!
42+
end
43+
end
44+
45+
private
46+
47+
def render_custom_field_input
48+
input_class = if custom_field.multi_value?
49+
CustomFields::Inputs::MultiVersionSelectList
50+
else
51+
CustomFields::Inputs::SingleVersionSelectList
52+
end
53+
54+
# Use fields_for to create the proper context for custom field inputs
55+
form.fields_for(:custom_field_values) do |builder|
56+
input_class.new(builder, custom_field:, object: model)
57+
end
58+
end
59+
60+
def render_autocompleter
61+
form.autocompleter(name: attribute, **@system_arguments) do |list|
62+
model.assignable_versions.each do |version|
63+
list.option(
64+
label: version.name,
65+
value: version.id,
66+
selected: version.id == model.version&.id
67+
)
68+
end
69+
end
70+
end
71+
72+
def assign_defaults!
73+
version = model.version
74+
@system_arguments[:autocomplete_options][:inputValue] = version&.id
75+
@system_arguments[:autocomplete_options][:model] = version_model
76+
@system_arguments[:autocomplete_options][:decorated] = true
77+
@system_arguments[:autocomplete_options][:closeOnSelect] = true
78+
# Override inputName to use Rails form builder naming convention
79+
@system_arguments[:autocomplete_options][:inputName] = input_name
80+
end
81+
82+
def version_model
83+
version ? { id: version.id, name: version.name } : nil
84+
end
85+
86+
def input_name
87+
"#{model.class.model_name.param_key}[#{attribute}]"
88+
end
89+
end
90+
end
91+
end
92+
end

config/initializers/inplace_edit_fields.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"weighted_item_list" => OpenProject::Common::InplaceEditFields::TextInputComponent, # TODO
4848
"list" => OpenProject::Common::InplaceEditFields::SelectListComponent,
4949
"user" => OpenProject::Common::InplaceEditFields::TextInputComponent, # TODO
50-
"version" => OpenProject::Common::InplaceEditFields::TextInputComponent, # TODO
50+
"version" => OpenProject::Common::InplaceEditFields::VersionSelectListComponent,
5151
"calculated_value" => OpenProject::Common::InplaceEditFields::CalculatedValueInputComponent
5252
}
5353

0 commit comments

Comments
 (0)