avo
avo copied to clipboard
[RFC] `format_(edit|new|show|index|forms|display)_using` field options
Context
We usually need to format values in different ways and most of the time on Index and Show views.
One example of that is currency. On the edit view we'll want to display the raw value but on the display views (Index, Show) you need the embellished view.
Example:
# Before
field :amount, as: number, format_using: -> do
if view == :index || view == :show
number_to_currency value
else
value
end
end
# After
field :amount, as: number, format_index_using: -> { number_to_currency value }
# or using display (index and show views) after we merge it
field :amount, as: number, format_display_using: -> { number_to_currency value }
Another example are computed links. For example have an id in the database represented as a link on display views.
# Before
field :stripe_id, as: :text, format_using: -> do
if view == :index || view == :show
link_to id, "https://dashboard.stripe.com/plans/#{value}"
else
value
end
end
# After
field :stripe_id, as: :text, format_display_using: -> { link_to id, "https://dashboard.stripe.com/plans/#{value}" }
The format_using option will still be available and it will take precedence over all others.
# this will yield "general" on all views
field :stripe_id, as: :text, format_using: -> { "general" }, format_index_using: -> { "index" }
Avo version: 3 and up
This will deprecate the decorate option which supported only display views.
We are doing this change so we can support a more granular per-view formatting.
@Paul-Bob Let me give this a shot
@Paul-Bob I think this should be closed as its PR was merged.
@Paul-Bob I think this should be closed as its PR was merged.
Right, missed that, thanks!