https://avo.cool logo
Hello! Greetings everyone. I need a little help...
# fields
g
Hello! Greetings everyone. I need a little help. I am trying to set the default value of a Money field. I use the
default
option on the field and set it using a Money object but it doesn't work. The form only displays
0.00
in the field. I've also tried setting the default value with the
_cents
field or with a static value such as
1000.00
. Nothing works, the value loaded in the form is always
0.00
. This is the code:
Copy code
field :labor, as: :money, currencies: [ "EUR" ], name: "Labor", required: true, default: project&.default_phase_labor
Any idea what is wrong here? Thanks for the support!
m
Thread automatically created by Guillaume in #fields
l
Hey @glamorous-napkin-23111 Try to wrap that default value in a block so it can be evaluated on runtime.
Default: → {project….}
g
Trying
l
I’m AFK right now
g
No change.
l
Try record instead of project
g
record would refer to an instance of the "current" resource, correct? In this scenario, project refers to an instance of another resource. project is loaded at the top of the fields method.
Copy code
def fields
    project = params[:via_record_id] ? Project.find(params[:via_record_id]) : nil

    field :id, as: :id
    field :company_id, as: :hidden, default: Avo::Current.tenant_id
    field :project, as: :belongs_to
    field :name, as: :text, hide_on: :show

    field :h_phase_timeline, as: :heading, label: "Phase Timeline"
    field :start_date, as: :date, default: project&.default_phase_start_date
    field :end_date, as: :date, default: project&.default_phase_end_date
    field :duration, as: :number, format_show_using: -> { "#{value} months" }
    field :status, as: :select, enum: ::Phase.statuses, default: 0, required: true

    panel name: "Phase Financials" do
      field :labor, as: :money, currencies: [ "EUR" ], name: "Labor", required: true, help: "Remaining labor budget: #{project&.remaining_labor} #{project&.remaining_labor&.currency}"
      field :direct_costs, as: :money, currencies: [ "EUR" ], required: true, help: "Remaining direct costs budget: #{project&.remaining_direct_costs} #{project&.remaining_direct_costs&.currency}", default: -> { project&.default_phase_direct_costs }
    end

    panel name: "Financial Objectives" do
      field :labor_margin_objective_pc, as: :number, format_show_using: -> { "#{value}%" }, name: "Labor Margin (%)", help: -> { "Contract labor objective: #{project&.default_phase_labor_margin_objective_pc}%" }
      field :labor_margin_objective_val, as: :money, currencies: [ "EUR" ], name: "Labor Margin", only_on: :show
      field :labor_execution_budget, as: :money, currencies: [ "EUR" ], name: "Labor Execution Budget", only_on: :show
    end

    field :project_reports, as: :has_many
    field :time_entries, as: :has_many
  end
Setting the default value for other non Money fields works fine (ex: start_date).
l
Does it work if you set it to a set value? (For example 3)
Setting the project like that might not work for that use case
The fields method might be ran in different circumstances.
It’s difficult to read the source code here
Can you open a GitHub issue please?
Btw, I’m boarding a plane so I will be away for a while
l
What is
project&.default_phase_labor
returning ?
g
It returns an instance of the Money class
l
I think default is never applied
Because even on a new model, the monetized attribute is always an object with fractional 0
Try this on the console:
YourModelName.new.labor
It will probably return
#<Money fractional:0 currency:USD>
which is not
nil
so default is not applied
You need to allow
nil
values on the monetized attribute
I tried it on my end and that was the issue, let me know how it goes
BTW this is valid for all fields, Avo will only apply the default value on values that are
nil
g
Afk now but will try in a bit
Thx
l
Sure, I'll also be out for a while, no pressure =))
g
Confirming that it works now! Thanks a lot.
m
Thread was archived by Guillaume. Anyone can send a message to unarchive it.