https://avo.cool logo
I'm having trouble getting `format_using` to wo...
# avo-2
m
Thread automatically created by andrew.walker.testdouble.com in #740893011994738751
l
Cc: @loud-jewelry-99127
l
hello @late-noon-56486 it should be working can you provide the custom field files?
Did you overridden the
value
method on your custom field?
l
So I am doing:
Copy code
bash
bin/rails generate avo:field currency
Avo pro 2.44.0
      create  app/components/avo/fields/currency_field
      create  app/components/avo/fields/currency_field/edit_component.html.erb
      create  app/components/avo/fields/currency_field/edit_component.rb
      create  app/components/avo/fields/currency_field/index_component.html.erb
      create  app/components/avo/fields/currency_field/index_component.rb
      create  app/components/avo/fields/currency_field/show_component.html.erb
      create  app/components/avo/fields/currency_field/show_component.rb
      create  app/avo/fields/currency_field.rb
^ I am not changing anything in these files. And then in
client_resource.rb
I am setting:
Copy code
field :meal_budget_consumption_adjustment,
  as: :currency,
  name: "Budget spent on client?",
  format_using: -> { "20.00" }
If go to the edit view, the field works but does not utilize the
format_using
block. If I set this to
as: :text
or
as: :number
then it utilizes
format_using
.
currency_field.rb
Copy code
ruby
class CurrencyField < Avo::Fields::BaseField
  def initialize(name, **args, &block)
    super(name, **args, &block)
  end
end
edit_component.html.erb
Copy code
<%= field_wrapper **field_wrapper_args do %>
  <%= @form.text_field @field.id,
    class: classes("w-full"),
    placeholder: @field.placeholder,
    disabled: disabled? %>
<% end %>
format_using
works on the index and show pages but not on the edit page.
I got it to work on the edit page by explicitly referencing
@field.value
like so:
edit_component.html.erb
Copy code
<%= field_wrapper **field_wrapper_args do %>
  <%= @form.text_field @field.id,
    class: classes("w-full"),
    placeholder: @field.placeholder,
    disabled: disabled?,
    value: @field.value %>
<% end %>
l
great! That's the way
I tested it on show view and didn't realized that value is missing on edit
l
Let's say I wanted to always apply formatting to the value when it's being shown or edited and don't want to have to specify
format_using
for each instance of the field. Where would be the ideal place to do that from? I know I can specify a default value for
format_using
from the currency class in
currency_field.rb
but I'm not sure that would be considered best practice.
l
Add it to the initializer args 😉
When you call super on the initializer
Merge whatever you want as default on that args
Makes sense?
l
Copy code
class CurrencyField < Avo::Fields::BaseField
  def initialize(name, **args, &block)
    super(name, **args, &block)

    @format_using = args[:format_using] || -> { "20.00" }
  end
end
l
Before super
The args that you pass to super should include
format_using:...
Copy code
class CurrencyField < Avo::Fields::BaseField
  def initialize(name, **args, &block)
    super(name, **args, &block)

    @format_using = -> { "20.00" }
  end
end
l
Copy code
class CurrencyField < Avo::Fields::BaseField
  def initialize(name, **args, &block)
    args[:format_using] ||= -> { "20.00" }
    super(name, **args, &block)
  end
end
?
l
Perfect
It should work
Sry I'm on phone and it's hard to make good snippets 😅
l
No worries
It works. It also worked with
Copy code
class CurrencyField < Avo::Fields::BaseField
  def initialize(name, **args, &block)
    super(name, **args, &block)

    @format_using = args[:format_using] || -> { "20.00" }
  end
end
but I will go with your suggestion
l
Ah right, both works 😅 on custom fields you can even add your own arguments