Permalink
Browse files

don't raise exception when trying to freeze nil association

  • Loading branch information...
1 parent c405d2c commit 26c2ceacf174c16c452c229c58fd503466681be0 @ryanb committed Jul 14, 2008
Showing with 15 additions and 7 deletions.
  1. +2 −0 CHANGELOG
  2. +1 −1 lib/association_freezer/belongs_to_freezer.rb
  3. +12 −6 spec/freezer_spec.rb
View
@@ -1,3 +1,5 @@
+* don't raise exception when trying to freeze nil association
+
* raise an exception when trying to replace frozen association
*0.1.0* (July 14th, 2008)
@@ -6,7 +6,7 @@ def initialize(owner, reflection)
end
def freeze
- self.frozen_data = Marshal.dump(nonfrozen.attributes)
+ self.frozen_data = Marshal.dump(nonfrozen.attributes) if nonfrozen
end
def unfreeze
View
@@ -30,13 +30,19 @@
end
it "should freeze association in its current state" do
- @ship_method = ShipMethod.create!(:price => 2)
- @order.ship_method = @ship_method
- @ship_method.price = 3
+ ship_method = ShipMethod.create!(:price => 2)
+ @order.ship_method = ship_method
+ ship_method.price = 3
@order.freeze_ship_method
@order.ship_method.price.should == 3
end
+ it "should do nothing when attempting to freeze a nil association" do
+ @order.ship_method.should be_nil
+ lambda { @order.freeze_ship_method }.should_not raise_error
+ @order.ship_method.should be_nil
+ end
+
describe "when freezing association" do
before(:each) do
@ship_method = ShipMethod.create!(:price => 5)
@@ -80,12 +86,12 @@
end
it "should raise an exception when attempting to save associated model" do
- lambda { @order.ship_method.save }.should raise_error(Exception)
- lambda { @order.ship_method.save! }.should raise_error(Exception)
+ lambda { @order.ship_method.save }.should raise_error
+ lambda { @order.ship_method.save! }.should raise_error
end
it "should raise an exception when attempting to replace association" do
- lambda { @order.ship_method = ShipMethod.new }.should raise_error(Exception)
+ lambda { @order.ship_method = ShipMethod.new }.should raise_error
end
end
end

0 comments on commit 26c2cea

Please sign in to comment.