money gem:从父模型继承货币

时间:2011-07-13 16:29:11

标签: ruby-on-rails ruby ruby-on-rails-3 rubygems currency

有人可以告诉我如何从父模型继承货币吗?

我使用money gem(https://github.com/RubyMoney/money)并拥有2个模型(market和tradelimit)。

市场价格会定期更新,如果有一个具有相同价格的交易限制,则会通知用户。

因为市场已经存储了货币,我不想再将它存储在tradelimit模型中(不干):

class Market << 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

#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

我如何从父(市场)模型继承货币? 我试图在tradelimit模型中覆盖货币方法,但这是最好的工作..

有什么建议吗?谢谢!

修改

或者有没有办法(没有composition_of功能)来提供这个功能?

1 个答案:

答案 0 :(得分:0)

你试过了吗?

class Market < AR
  #use Money as a mixin
  include Money
...

end

#Now Tradelimit should inherit all from Market
class Tradelimit <  Market