Skip to content

Commit b1029b3

Browse files
committed
Basic userSelect field for inplaceEditComponent
1 parent cac9bb8 commit b1029b3

File tree

5 files changed

+153
-13
lines changed

5 files changed

+153
-13
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ def boolean_display_value(value)
107107
def writable?
108108
writable && (@system_arguments[:readonly].nil? || @system_arguments[:readonly] == false)
109109
end
110+
111+
def custom_field_values
112+
CustomValue
113+
.includes(custom_field: :custom_options)
114+
.where(
115+
custom_field_id: custom_field&.id,
116+
customized_id: model.id
117+
)
118+
.to_a
119+
end
110120
end
111121
end
112122
end

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def render_display_value
4242

4343
if value.present? && value != [nil]
4444
if custom_field?
45-
custom_field_values
45+
formatted_custom_field_values
4646
else
4747
value.is_a?(Array) ? value.map(&:to_s).join(", ") : value.to_s
4848
end
@@ -51,19 +51,12 @@ def render_display_value
5151
end
5252
end
5353

54-
def custom_field_values
55-
return @custom_field_values if defined?(@custom_field_values)
54+
def formatted_custom_field_values
55+
return @formatted_custom_field_values if defined?(@formatted_custom_field_values)
5656

57-
values = CustomValue
58-
.includes(custom_field: :custom_options)
59-
.where(
60-
custom_field_id: custom_field&.id,
61-
customized_id: model.id
62-
)
63-
.to_a
64-
.map { |v| format_value(v.value, custom_field) }
57+
values = custom_field_values.map { |v| format_value(v.value, custom_field) }
6558

66-
@custom_field_values = custom_field&.multi_value? ? values.join(", ") : values.first
59+
@formatted_custom_field_values = custom_field&.multi_value? ? values.join(", ") : values.first
6760
end
6861
end
6962
end
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
module DisplayFields
35+
class UserSelectListComponent < SelectListComponent
36+
include CustomFieldsHelper
37+
38+
attr_reader :model, :attribute, :writable
39+
40+
def formatted_custom_field_values
41+
return @formatted_custom_field_values if defined?(@formatted_custom_field_values)
42+
43+
cf_values = custom_field_values
44+
45+
users = cf_values.filter_map(&:typed_value)
46+
47+
@formatted_custom_field_values = if custom_field.multi_value?
48+
flex_layout do |avatar_container|
49+
users.each do |user|
50+
avatar_container.with_row do
51+
render_avatar(user)
52+
end
53+
end
54+
end
55+
else
56+
render_avatar(users.first)
57+
end
58+
end
59+
60+
private
61+
62+
def render_avatar(user)
63+
return unless user
64+
65+
render(::Users::AvatarComponent.new(user:, size: :mini))
66+
end
67+
end
68+
end
69+
end
70+
end
71+
end
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 UserSelectListComponent < SelectListComponent
35+
attr_reader :form, :attribute, :model
36+
37+
def self.display_class
38+
DisplayFields::UserSelectListComponent
39+
end
40+
41+
def initialize(form:, attribute:, model:, **system_arguments)
42+
super
43+
44+
unless custom_field?
45+
assign_defaults!
46+
end
47+
end
48+
49+
private
50+
51+
def render_custom_field_input
52+
input_class = if custom_field.multi_value?
53+
CustomFields::Inputs::MultiUserSelectList
54+
else
55+
CustomFields::Inputs::SingleUserSelectList
56+
end
57+
58+
# Use fields_for to create the proper context for custom field inputs
59+
form.fields_for(:custom_field_values) do |builder|
60+
input_class.new(builder, custom_field:, object: model)
61+
end
62+
end
63+
end
64+
end
65+
end
66+
end

config/initializers/inplace_edit_fields.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"hierarchy" => OpenProject::Common::InplaceEditFields::TextInputComponent, # TODO
4747
"weighted_item_list" => OpenProject::Common::InplaceEditFields::TextInputComponent, # TODO
4848
"list" => OpenProject::Common::InplaceEditFields::SelectListComponent,
49-
"user" => OpenProject::Common::InplaceEditFields::TextInputComponent, # TODO
49+
"user" => OpenProject::Common::InplaceEditFields::UserSelectListComponent,
5050
"version" => OpenProject::Common::InplaceEditFields::VersionSelectListComponent,
5151
"calculated_value" => OpenProject::Common::InplaceEditFields::CalculatedValueInputComponent
5252
}

0 commit comments

Comments
 (0)