avo icon indicating copy to clipboard operation
avo copied to clipboard

[RFC] `format_(edit|new|show|index|forms|display)_using` field options

Open adrianthedev opened this issue 2 years ago • 1 comments

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.

adrianthedev avatar Aug 12 '23 11:08 adrianthedev

@Paul-Bob Let me give this a shot

zhephyn avatar Apr 21 '25 06:04 zhephyn

@Paul-Bob I think this should be closed as its PR was merged.

zhephyn avatar Jul 08 '25 13:07 zhephyn

@Paul-Bob I think this should be closed as its PR was merged.

Right, missed that, thanks!

Paul-Bob avatar Jul 08 '25 13:07 Paul-Bob