money-rails
money-rails copied to clipboard
Define methods on dynamic module
Currently you cannot override the generated methods and access super ie:
class Transaction < ApplicationRecord
monetize :price_cents
def price=(value)
# stuff
super # NoMethodError
end
end
This happens because the method is defined on the class directly so their is no super method to call. A solution today is to create an alias (alias :old_price= :price=
) before overriding the method as is recommending in issue #507.
Instead, a new module can be created, included into the class, and the methods defined on it allowing super
to work as expected.