RubyMoney/money — Issue #118: Updating currency alongside money value in form?
Author: johnrees
Created: 2011-11-15 18:59:40+00:00
State: open

I can't figure out how to update a record's currency value using a form when I have a text input linked to the composed_of accessor.

If I have the `currency` and `total_cents` as text inputs then I can manually change both of them fine, however, if the text input represents the `total` (composed_of attribute) instead, the record's currency value gets set/overwritten by the Money.default_currency value instead of the submitted `currency` value.

I have the following fields in my model -

t.string :currency, :limit => 3
t.integer :total_cents, :default => 0, :null => false

And the composed_of attributes -

composed_of :total,
:class_name => "Money",
:mapping => [%w(total_cents cents), %w(currency currency_as_string)],
:constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) },
:converter => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") }

composed_of :gbp_total,
:class_name => "Money",
:mapping => [%w(gbp_total_cents cents)],
:constructor => Proc.new { |cents| Money.new(cents || 0, 'GBP') },
:converter => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") }

Am I missing something? Thanks