Skip to content

Commit 2d92b5d

Browse files
committed
Use CustomValue.formatted_value instead of formatting the values manually
1 parent c7afb49 commit 2d92b5d

File tree

7 files changed

+34
-201
lines changed

7 files changed

+34
-201
lines changed

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,10 @@ def initialize(model:, attribute:, writable:, truncated:, has_comment: false, sh
4949
end
5050

5151
def render_display_value
52-
value = model.public_send(attribute)
53-
54-
if value.is_a?(TrueClass) || value.is_a?(FalseClass)
55-
boolean_display_value(value)
56-
elsif value.is_a?(Date) || value.is_a?(Time)
57-
helpers.format_date(value)
58-
elsif value.present? && value != [nil]
59-
format_present_value(value)
52+
if custom_field?
53+
render_custom_field_display_value
6054
else
61-
t("placeholders.default")
55+
render_attribute_display_value
6256
end
6357
end
6458

@@ -151,11 +145,22 @@ def display_field_classes
151145
"op-inplace-edit--display-field#{' op-inplace-edit--display-field_clickable' if clickable}"
152146
end
153147

154-
def format_present_value(value)
155-
if custom_field?
156-
helpers.format_value(value, custom_field)
157-
else
148+
def render_custom_field_display_value
149+
values = custom_field_values.reject { |v| v.value.blank? }
150+
values.present? ? values.map(&:formatted_value).join(", ") : t("placeholders.default")
151+
end
152+
153+
def render_attribute_display_value
154+
value = model.public_send(attribute)
155+
156+
if value.is_a?(TrueClass) || value.is_a?(FalseClass)
157+
boolean_display_value(value)
158+
elsif value.is_a?(Date) || value.is_a?(Time)
159+
helpers.format_date(value)
160+
elsif value.present?
158161
value.to_s
162+
else
163+
t("placeholders.default")
159164
end
160165
end
161166

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

Lines changed: 0 additions & 59 deletions
This file was deleted.

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

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,13 @@ module Common
3333
module InplaceEditFields
3434
module DisplayFields
3535
class SelectListComponent < DisplayFieldComponent
36-
include CustomFieldsHelper
37-
38-
attr_reader :model, :attribute, :writable
39-
40-
def render_display_value
41-
value = model.public_send(attribute)
42-
43-
if value.present? && value != [nil]
44-
render_value(value)
45-
else
46-
t("placeholders.default")
47-
end
48-
end
49-
5036
private
5137

52-
def render_value(value)
53-
if custom_field?
54-
formatted_custom_field_values.presence || t("placeholders.default")
55-
else
56-
value.is_a?(Array) ? value.join(", ") : value.to_s
57-
end
58-
end
59-
60-
def formatted_custom_field_values
61-
return @formatted_custom_field_values if defined?(@formatted_custom_field_values)
62-
63-
values = custom_field_values.map { |v| format_value(v.value, custom_field) }
38+
def render_attribute_display_value
39+
value = model.public_send(attribute)
40+
return t("placeholders.default") unless value.present? && value != [nil]
6441

65-
@formatted_custom_field_values = custom_field&.multi_value? ? values.join(", ") : values.first
42+
value.is_a?(Array) ? value.join(", ") : value.to_s
6643
end
6744
end
6845
end

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

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,35 +33,22 @@ module Common
3333
module InplaceEditFields
3434
module DisplayFields
3535
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)
36+
private
4637

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
38+
def render_custom_field_display_value
39+
users = custom_field_values.filter_map(&:typed_value)
40+
return t("placeholders.default") if users.empty?
41+
42+
if custom_field.multi_value?
43+
flex_layout do |avatar_container|
44+
users.each { |user| avatar_container.with_row { render_avatar(user) } }
45+
end
46+
else
47+
render_avatar(users.first)
48+
end
5849
end
5950

60-
private
61-
6251
def render_avatar(user)
63-
return unless user
64-
6552
render(::Users::AvatarComponent.new(user:, size: :mini))
6653
end
6754
end

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ module InplaceEditFields
3434
class HierarchyListComponent < BaseFieldComponent
3535
include CustomFieldHierarchyTreeViewHelper
3636

37-
def self.display_class
38-
DisplayFields::HierarchyListComponent
39-
end
40-
4137
def self.open_in_dialog?
4238
true
4339
end

lookbook/docs/patterns/06-inplace-edit-fields.md.erb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,6 @@ end
201201
class HierarchyListComponent < BaseFieldComponent
202202
include CustomFieldHierarchyTreeViewHelper
203203

204-
def self.display_class
205-
DisplayFields::HierarchyListComponent
206-
end
207-
208204
def self.open_in_dialog?
209205
true
210206
end

spec/components/open_project/common/inplace_edit_fields/display_fields/hierarchy_list_component_spec.rb

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)