Sha256: 855d29661e6e34c5b74e97fbed007a169d99873c609ed3fceb7c55413484875a

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

include OldApiResource

describe "Should put callbacks around save, create, update, and destroy by default" do
  
  before(:all) do
    # This defines all the callbacks to check and see if they are fired
    TestResource.class_eval <<-EOE, __FILE__, __LINE__ + 1
      attr_accessor :s_val, :c_val, :u_val, :d_val
      before_save :bs_cb; after_save :as_cb
      before_create :bc_cb; after_create :ac_cb
      before_update :bu_cb; after_update :au_cb
      before_destroy :bd_cb; after_destroy :ad_cb
      
      def bs_cb
        @s_val = 1
      end
      def as_cb
        @s_val += 1
      end
      def bc_cb
        @c_val = 1
      end
      def ac_cb
       @c_val += 1
      end
      def bu_cb
        @u_val = 1
      end
      def au_cb
        @u_val += 1
      end
      def bd_cb
        @d_val = 1
      end
      def ad_cb
        @d_val += 1
      end
    EOE
  end
  
  it "should fire save and create callbacks when saving a new record" do
    tr = TestResource.new(:name => "Ethan", :age => 20)
    tr.save.should be_true
    tr.s_val.should eql(2)
    tr.c_val.should eql(2)
    tr.u_val.should be_nil
  end
  
  it "should fire save and update callbacks when updating a record" do
    tr = TestResource.new(:id => 1, :name => "Ethan", :age => 20)
    tr.name = "Test"
    tr.age = 21
    tr.save.should be_true
    tr.s_val.should eql(2)
    tr.c_val.should be_nil
    tr.u_val.should eql(2)
  end
  
  it "should only fire destroy callbacks when destroying a record" do
    tr = TestResource.new(:id => 1, :name => "Ethan", :age => 20)
    tr.destroy.should be_true
    tr.d_val.should eql(2)
    tr.s_val.should be_nil   
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
old_api_resource-0.3.0 spec/lib/callbacks_spec.rb