money gem: inherit currency from parent model
Can someone tell me how to inherit the currency from a parent model?
I use the money gem (https://github.com/RubyMoney/money) and have 2 models (market and tradelimit).
The price of the market becomes updated periodical and if there is a tradelimit which has the same price, the user becomes informed.
Because the market has already the currency stored, i dont want to store it again in the tradelimit model (not dry):
class Market << AR
composed_of :price,
:class_name => "Money"开发者_Python百科,
:mapping => [%w(price_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") }
...
end
#tradelimit.rb
class Tradelimit << AR
composed_of :price,
:class_name => "Money",
:mapping => [%w(price_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") }
...
end
How can I inherit now the currency from the parent (market) model? I tried to override the currency method in the tradelimit model, but this doenst work..
Any suggestions? Thanks!
EDIT:
Or is there a way (without the composed_of feature) to provide this functionality?
Have you tried:
class Market < AR
#use Money as a mixin
include Money
...
end
#Now Tradelimit should inherit all from Market
class Tradelimit < Market
精彩评论