Sha256: b7dd7a5ba5840e18fb468a4e2242ee504d343624b383b5db92d1f441d7e73274

Contents?: true

Size: 1.15 KB

Versions: 7

Compression:

Stored size: 1.15 KB

Contents

require 'helper'

module CallbackHelper
  extend ActiveSupport::Concern

  included do
    [ :before_create,  :after_create,
      :before_update,  :after_update,
      :before_save,    :after_save,
      :before_destroy, :after_destroy].each do |callback|
      callback_method = "#{callback}_callback"
      send(callback, callback_method)
      define_method(callback_method) { history << callback.to_sym }
    end
  end

  def history
    @history ||= []
  end

  def clear_history
    @history = nil
  end
end

describe Toy::Callbacks do
  uses_constants('Game')

  before do
    Game.send(:include, CallbackHelper)
  end

  it "runs callbacks in correct order for create" do
    doc = Game.create
    doc.history.should == [:before_save, :before_create, :after_create, :after_save]
  end

  it "runs callbacks in correct order for update" do
    doc = Game.create
    doc.clear_history
    doc.save
    doc.history.should == [:before_save, :before_update, :after_update, :after_save]
  end

  it "runs callbacks in correct order for destroy" do
    doc = Game.create
    doc.clear_history
    doc.destroy
    doc.history.should == [:before_destroy, :after_destroy]
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
toystore-0.13.2 spec/toy/callbacks_spec.rb
toystore-0.13.1 spec/toy/callbacks_spec.rb
toystore-0.13.0 spec/toy/callbacks_spec.rb
toystore-0.12.0 spec/toy/callbacks_spec.rb
toystore-0.11.0 spec/toy/callbacks_spec.rb
toystore-0.10.4 spec/toy/callbacks_spec.rb
toystore-0.10.3 spec/toy/callbacks_spec.rb