money-rails icon indicating copy to clipboard operation
money-rails copied to clipboard

Define methods on dynamic module

Open jbennett opened this issue 7 months ago • 1 comments

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.

jbennett avatar Jul 03 '24 13:07 jbennett