A
Alfredo Garcia lopez
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 ld_price, rice
alias_method rice, ld_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.
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 ld_price, rice
alias_method rice, ld_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.