/ Published in: Ruby
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
require 'rubygems' require 'active_model' class BaseModel extend ActiveModel::Callbacks define_model_callbacks :save before_save { puts "[save:before]"} after_save { puts "[save:after]"} def do_save; end def save _run_save_callbacks do do_save end end alias_method :superclass_save, :save end class SomeModel < BaseModel def save puts "[saving...]" true end end SomeModel.class_eval do alias_method :subclass_save, :save alias_method :do_save, :subclass_save alias_method :save, :superclass_save end model = SomeModel.new model.save
URL: ruby-metaprogramming-subclass-a-method-with-before-after-callbacks-and-alias-method