Skip to content

Commit

Permalink
tests for updatability in before_update method
Browse files Browse the repository at this point in the history
* removed commented code
* removed attempted attribute update in Component::after_destroy (it was
  breaking some tests when in fact I believe this is correct behaviour
* added specific tests for updatability in before/after destroy methods
  • Loading branch information
tardate committed Dec 18, 2010
1 parent a8b3273 commit e42bf31
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
12 changes: 11 additions & 1 deletion spec/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Component < ActiveRecord::Base #:nodoc:
has_many :sub_components, :dependent => :destroy
NEW_NAME = 'Something Else!'

after_destroy :change_name
before_destroy :change_name
def change_name
self.update_attribute(:name, NEW_NAME)
end
Expand Down Expand Up @@ -147,6 +147,16 @@ def ret_false
end
end

class ZombiePirate < ActiveRecord::Base #:nodoc:
set_table_name :pirates
NEW_NAME = 'Undead rising!'

after_destroy :change_name
def change_name
self.update_attribute(:name, NEW_NAME)
end
end

class Uuid < ActiveRecord::Base #:nodoc:
set_primary_key "uuid"

Expand Down
29 changes: 11 additions & 18 deletions spec/paranoid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,6 @@
LUKE = 'Luke Skywalker'

describe Paranoid do
before(:each) do
# Sticker.delete_all
# Place.delete_all
# Android.delete_all
# Person.delete_all
# Component.delete_all
#
# @luke = Person.create(:name => LUKE)
# @r2d2 = Android.create(:name => 'R2D2', :owner_id => @luke.id)
# @c3p0 = Android.create(:name => 'C3P0', :owner_id => @luke.id)
#
# @r2d2.components.create(:name => 'Rotors')
#
# @r2d2.memories.create(:name => 'A pretty sunset')
# @c3p0.sticker = Sticker.create(:name => 'OMG, PONIES!')
# @tatooine = Place.create(:name => "Tatooine")
# @r2d2.places << @tatooine
end

describe 'basic functionality' do
before(:each) do
Expand Down Expand Up @@ -268,6 +250,17 @@
pirate = RandomPirate.create!(:name => 'Roberts')
lambda { pirate.destroy rescue nil }.should_not change(RandomPirate, :count)
end

it 'should allow updates in before_destroy callbacks' do
Component.create!(:name => 'bolt').destroy
Component.with_destroyed_only.first.name.should eql(Component::NEW_NAME)
end

it 'should not allow updates in after_destroy callbacks' do
soon_to_be_undead = ZombiePirate.create!(:name => 'But I feel fine!')
lambda { soon_to_be_undead.destroy }.should raise_error
end

end

describe 'association destroy' do
Expand Down

0 comments on commit e42bf31

Please sign in to comment.