Hi. Is there a way to include code in a class and all its subclasses and get the behaviour as if it was included at the end of the code in each of them? Example: class Product<ActiveRecord::Base def price 100 end end module ProductExtension def price 120 end end I would wish find a way to include this module to the superclass(ActiveRecord::Base) and get the following result: @product=Product.new @product.price =>120 or even module ProductExtension def precio old_price+30 end alias_method :old_price, :price alias_method :price, :old_price end @product=Product.new @product.price =>130 The module to include would be conditional. Ex: If the current class is Product I would include ProductExtension. If the current class is Category I would include CategoryExtension. Perhaps thereÂ´s a better way to chieve this.Accept suggestions. Thanks for reading.
on 2008-10-22 17:20
on 2008-10-22 17:24
Sorry,this is wrong > > module ProductExtension > def precio > old_price+30 > end > alias_method :old_price, :price > alias_method :price, :old_price > end It would be ... module ProductExtension def new_price old_price+30 end alias_method :old_price, :price alias_method :price, :new_price end