Return to Snippet

Revision: 30489
at August 15, 2010 10:06 by viatropos


Initial Code
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

Initial URL
ruby-metaprogramming-subclass-a-method-with-before-after-callbacks-and-alias-method

Initial Description

                                

Initial Title
Ruby Metaprogramming - Subclass a Method with Before/After Callbacks and alias_method

Initial Tags
ruby

Initial Language
Ruby