Skip to content

Commit 9ab80e8

Browse files
Add support for decoration in base field handling (#3765)
* Add support for decoration in base field handling * tweaks * performance tweak * fix * Refactor tag-related specs for consistency and readability * Revert "Refactor tag-related specs for consistency and readability" This reverts commit 060466b. * add test for decorate * lint * fix multiple actions flux spec * fix multiple actions flux spec --------- Co-authored-by: Paul Bob <[email protected]> Co-authored-by: Paul Bob <[email protected]>
1 parent 8f0c5c4 commit 9ab80e8

File tree

5 files changed

+49
-36
lines changed

5 files changed

+49
-36
lines changed

lib/avo/fields/base_field.rb

+28-30
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,19 @@ def initialize(id, **args, &block)
7070
@summarizable = args[:summarizable] || false
7171
@nullable = args[:nullable] || false
7272
@null_values = args[:null_values] || [nil, ""]
73-
@format_using = args[:format_using] || nil
74-
@update_using = args[:update_using] || nil
73+
@format_using = args[:format_using]
74+
@update_using = args[:update_using]
75+
@decorate = args[:decorate]
7576
@placeholder = args[:placeholder]
76-
@autocomplete = args[:autocomplete] || nil
77-
@help = args[:help] || nil
78-
@default = args[:default] || nil
77+
@autocomplete = args[:autocomplete]
78+
@help = args[:help]
79+
@default = args[:default]
7980
@visible = args[:visible]
8081
@as_avatar = args[:as_avatar] || false
81-
@html = args[:html] || nil
82-
@view = Avo::ViewInquirer.new(args[:view]) || nil
83-
@value = args[:value] || nil
84-
@stacked = args[:stacked] || nil
82+
@html = args[:html]
83+
@view = Avo::ViewInquirer.new(args[:view])
84+
@value = args[:value]
85+
@stacked = args[:stacked]
8586
@for_presentation_only = args[:for_presentation_only] || false
8687
@resource = args[:resource]
8788
@action = args[:action]
@@ -154,45 +155,42 @@ def placeholder
154155
def value(property = nil)
155156
return @value if @value.present?
156157

157-
property ||= @for_attribute || id
158+
property ||= @for_attribute || @id
158159

159160
# Get record value
160-
final_value = record.send(property) if is_model?(record) && record.respond_to?(property)
161+
final_value = @record.send(property) if is_model?(@record) && @record.respond_to?(property)
161162

162163
# On new views and actions modals we need to prefill the fields with the default value if value is nil
163-
if final_value.nil? && should_fill_with_default_value? && default.present?
164+
if final_value.nil? && should_fill_with_default_value? && @default.present?
164165
final_value = computed_default_value
165166
end
166167

167168
# Run computable callback block if present
168-
if computable && block.present?
169-
final_value = execute_block
169+
if computable && @block.present?
170+
final_value = execute_context(@block)
170171
end
171172

172173
# Run the value through resolver if present
173-
if format_using.present?
174-
final_value = Avo::ExecutionContext.new(
175-
target: format_using,
176-
value: final_value,
177-
record: record,
178-
resource: resource,
179-
view: view,
180-
field: self,
181-
include: self.class.included_modules
182-
).handle
174+
if @format_using.present?
175+
final_value = execute_context(@format_using, value: final_value)
176+
end
177+
178+
if @decorate.present? && @view.display?
179+
final_value = execute_context(@decorate, value: final_value)
183180
end
184181

185182
final_value
186183
end
187184

188-
def execute_block
185+
def execute_context(target, **extra_args)
189186
Avo::ExecutionContext.new(
190-
target: block,
191-
record: record,
192-
resource: resource,
193-
view: view,
187+
target:,
188+
record: @record,
189+
resource: @resource,
190+
view: @view,
194191
field: self,
195-
include: self.class.included_modules
192+
include: self.class.included_modules,
193+
**extra_args
196194
).handle
197195
end
198196

lib/avo/fields/heading_field.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def id
2424
end
2525

2626
def value
27-
block.present? ? execute_block : @label
27+
@block.present? ? execute_context(@block) : @label
2828
end
2929
end
3030
end

spec/dummy/app/avo/resources/city.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def tool_fields
9595

9696
link_to resource.record.name, path, data: data
9797
end
98-
field :population, as: :number, filterable: true
98+
field :population, as: :number, filterable: true, decorate: -> { number_with_delimiter(value, delimiter: ".") }
9999
field :is_capital, as: :boolean, filterable: true
100100
field :features, as: :key_value
101101
field :image_url, as: :external_image

spec/features/avo/text_field_spec.rb

+15
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,19 @@
6060
end
6161
end
6262
end
63+
64+
describe "decorate" do
65+
let!(:city) { create :city, population: 18000 }
66+
67+
it "only on display" do
68+
visit avo.edit_resources_city_path(city)
69+
expect(find_by_id("city_population").value).to eq("18000")
70+
71+
visit avo.resources_city_path(city)
72+
expect(show_field_value(id: :population)).to eq "18.000"
73+
74+
visit avo.resources_cities_path
75+
expect(index_field_value(id: :population, record_id: city.to_param)).to eq "18.000"
76+
end
77+
end
6378
end

spec/system/avo/multiple_actions_flux_spec.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
visit "/admin/resources/cities"
1010

1111
expect(page).to have_text "Spec City"
12-
expect(page).to have_text "123456"
12+
expect(page).to have_text "123.456"
1313

1414
find(:css, 'input[type="checkbox"][data-action="input->item-selector#toggle input->item-select-all#selectRow"]', match: :first).set(true)
1515

@@ -31,16 +31,16 @@
3131

3232
expect(page).to have_text "IS CAPITAL"
3333
expect(page).to have_text "Spec City"
34-
expect(page).to have_text "123456"
34+
expect(page).to have_text "123.456"
3535

3636
fill_in "fields_population", with: "654321"
3737

3838
run_action
3939

4040
expect(page).to have_text "City updated!"
4141
expect(page).to have_text "Spec City"
42-
expect(page).not_to have_text "123456"
43-
expect(page).to have_text "654321"
42+
expect(page).not_to have_text "123.456"
43+
expect(page).to have_text "654.321"
4444
end
4545
end
4646
end

0 commit comments

Comments
 (0)