Permalink
Browse files

raise an exception when replacing frozen association

  • Loading branch information...
1 parent c340141 commit c405d2c72bc471ae3f2951d7f95bac11ebd8ea2b @ryanb committed Jul 14, 2008
View
@@ -1,3 +1,5 @@
+* raise an exception when trying to replace frozen association
+
*0.1.0* (July 14th, 2008)
* initial release
@@ -18,10 +18,14 @@ def fetch(*args)
frozen || nonfrozen(*args)
end
+ def frozen?
+ frozen_data
+ end
+
private
def frozen
- @frozen ||= load_frozen if frozen_data
+ @frozen ||= load_frozen if frozen?
end
def load_frozen
@@ -40,10 +44,6 @@ def frozen_data
@owner.read_attribute("frozen_#{name}")
end
- def attribute_name
- "frozen_#{name}"
- end
-
def target_class
@reflection.klass
end
@@ -29,6 +29,16 @@ def generate
send(freezer).fetch(*args)
end
model_class.alias_method_chain reflection.name, :frozen_check
+
+ generate_method "#{reflection.name}_with_frozen_check=" do |*args|
+ if send(freezer).frozen?
+ # TODO make this a custom exception
+ raise "Unable to set #{reflection.name} because association is frozen."
+ else
+ send("#{reflection.name}_without_frozen_check=", *args)
+ end
+ end
+ model_class.alias_method_chain "#{reflection.name}=", :frozen_check
end
private
@@ -78,6 +78,15 @@
@order.unfreeze_ship_method
@order.ship_method.price.should == 8
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)
+ end
+
+ it "should raise an exception when attempting to replace association" do
+ lambda { @order.ship_method = ShipMethod.new }.should raise_error(Exception)
+ end
end
end
end

0 comments on commit c405d2c

Please sign in to comment.