Skip to content

Commit

Permalink
Merge pull request #104 from avo-hq/add/format_using_examples
Browse files Browse the repository at this point in the history
add: format using view example
  • Loading branch information
Paul-Bob authored Sep 1, 2023
2 parents 11e0011 + ac5d2a2 commit 99cd41c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
37 changes: 22 additions & 15 deletions docs/2.0/field-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,33 +130,40 @@ This example will display a boolean field with the value computed from your cust

Sometimes you will want to process the database value before showing it to the user. You may do that using `format_using` block.

Notice that this block will have effect on **all** views.

You have access to a bunch of variables inside this block, all the defaults that [`Avo::ExecutionContext`](../3.0/execution-context.html) provides plus `value`, `model`, `key`, `resource`, `view` and `field`.

:::warning
We removed the `value` argument from `format_using` since version `2.36`.
:::

```ruby
field :is_writer, as: :text, format_using: -> {
# You have access to the following variables:
# - value
# - resource
# - record
# - view
# - view_context
# - context
# - params
# - request
value.present? ? 'πŸ‘' : 'πŸ‘Ž'
if view == :new || view == :edit
value
else
value.present? ? 'πŸ‘' : 'πŸ‘Ž'
end
}
# or
field :company_url, as: :text, format_using: -> { link_to(value, value, target: "_blank") } do |model, *args|
main_app.companies_url(model)
end
```

This example snippet will make the `:is_writer` field generate emojis instead of 1/0 values.
This example snippet will make the `:is_writer` field generate `πŸ‘` or `πŸ‘Ž` emojis instead of `1` or `0` values on display views and the values `1` or `0` on form views.

<img :src="('/assets/img/fields-reference/fields-formatter.png')" alt="Fields formatter" class="border mb-4" />

Another example:

```ruby
field :company_url,
as: :text,
format_using: -> {
link_to(value, value, target: "_blank")
} do |model, *args|
main_app.companies_url(model)
end
```

### Formatting with Rails helpers

You can also format using Rails helpers like `number_to_currency` (note that `view_context` is used to access the helper):
Expand Down
36 changes: 29 additions & 7 deletions docs/3.0/field-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,42 @@ This example will display a boolean field with the value computed from your cust

## Fields Formatter

Sometimes you will want to process the database value before showing it to the user. You may do that using `format_using` block that receives the `value` of that field as a parameter.
Sometimes you will want to process the database value before showing it to the user. You may do that using `format_using` block.

Notice that this block will have effect on **all** views.

You have access to a bunch of variables inside this block, all the defaults that [`Avo::ExecutionContext`](./execution-context.html) provides plus `value`, `record`, `resource`, `view` and `field`.

```ruby
field :is_writer, as: :text, format_using: -> { value.present? ? 'πŸ‘' : 'πŸ‘Ž' }
# or
field :company_url, as: :text, format_using: -> { link_to(value, value, target: "_blank") } do
main_app.companies_url(record)
end
field :is_writer, as: :text, format_using: -> {
if view.form?
value
else
value.present? ? 'πŸ‘' : 'πŸ‘Ž'
end
}
```

This example snippet will make the `:is_writer` field generate emojis instead of 1/0 values.
This example snippet will make the `:is_writer` field generate `πŸ‘` or `πŸ‘Ž` emojis instead of `1` or `0` values on display views and the values `1` or `0` on form views.

<img :src="('/assets/img/fields-reference/fields-formatter.png')" alt="Fields formatter" class="border mb-4" />

Another example:

```ruby
field :company_url,
as: :text,
format_using: -> {
if view == :new || view == :edit
value
else
link_to(value, value, target: "_blank")
end
} do
main_app.companies_url(record)
end
```

## Formatting with Rails helpers

You can also format using Rails helpers like `number_to_currency` (note that `view_context` is used to access the helper):
Expand Down

0 comments on commit 99cd41c

Please sign in to comment.