Skip to content
Browse files

ensuring association is frozen at the current state and not always re…

…loaded from the db
  • Loading branch information...
1 parent be01afc commit 8d7e3d1ac0aabb56e3ae7b577ef7b2f46c6e029b @ryanb committed
View
11 lib/association_freezer/belongs_to_freezer.rb
@@ -6,14 +6,17 @@ def initialize(owner, reflection)
end
def freeze
- @owner.send(name, true).freeze
- freeze_to_db(Marshal.dump(@owner.send(name).attributes))
+ freeze_to_db(Marshal.dump(nonfrozen.attributes))
end
def unfreeze
# TODO
end
+ def fetch(*args)
+ frozen(*args) || nonfrozen(*args)
+ end
+
def frozen(force_reload = false)
if @frozen && !force_reload
@frozen
@@ -24,6 +27,10 @@ def frozen(force_reload = false)
private
+ def nonfrozen(*args)
+ @owner.send("#{name}_without_frozen_check", *args)
+ end
+
def freeze_to_db(data)
@owner.write_attribute("frozen_#{name}", data)
end
View
2 lib/association_freezer/method_generator.rb
@@ -26,7 +26,7 @@ def generate
end
generate_method "#{reflection.name}_with_frozen_check" do |*args|
- send(freezer).frozen(*args) || send("#{reflection.name}_without_frozen_check", *args)
+ send(freezer).fetch(*args)
end
model_class.alias_method_chain reflection.name, :frozen_check
end
View
10 spec/freezer_spec.rb
@@ -24,11 +24,19 @@
@order.should_not respond_to(:unfreeze_cart)
end
- it "should not freeze association without specifying" do
+ it "should not be frozen at first" do
@order.ship_method = ShipMethod.create!
@order.ship_method(true).should_not be_frozen
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
+ @order.freeze_ship_method
+ @order.ship_method.price.should == 3
+ end
+
describe "when freezing association" do
before(:each) do
@ship_method = ShipMethod.create!(:price => 5)

0 comments on commit 8d7e3d1

Please sign in to comment.
Something went wrong with that request. Please try again.