Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

unfreeze association

  • Loading branch information...
commit 59ce88e49c613588d8ce4189d185c79e24e65838 1 parent 8d7e3d1
@ryanb authored
Showing with 32 additions and 13 deletions.
  1. +13 −12 lib/association_freezer/belongs_to_freezer.rb
  2. +19 −1 spec/freezer_spec.rb
View
25 lib/association_freezer/belongs_to_freezer.rb
@@ -6,36 +6,37 @@ def initialize(owner, reflection)
end
def freeze
- freeze_to_db(Marshal.dump(nonfrozen.attributes))
+ self.frozen_data = Marshal.dump(nonfrozen.attributes)
end
def unfreeze
- # TODO
+ @frozen = nil
+ self.frozen_data = nil
end
def fetch(*args)
- frozen(*args) || nonfrozen(*args)
+ frozen || nonfrozen(*args)
end
- def frozen(force_reload = false)
- if @frozen && !force_reload
- @frozen
- elsif frozen_in_db
- @frozen = target_class.new(Marshal.load(frozen_in_db).except('id')).freeze
- end
+ private
+
+ def frozen
+ @frozen ||= load_frozen if frozen_data
end
- private
+ def load_frozen
+ target_class.new(Marshal.load(frozen_data).except('id')).freeze
+ end
def nonfrozen(*args)
@owner.send("#{name}_without_frozen_check", *args)
end
- def freeze_to_db(data)
+ def frozen_data=(data)
@owner.write_attribute("frozen_#{name}", data)
end
- def frozen_in_db
+ def frozen_data
@owner.read_attribute("frozen_#{name}")
end
View
20 spec/freezer_spec.rb
@@ -52,14 +52,32 @@
@ship_method.should_not be_frozen
end
- it "should still consider model frozen after reload" do
+ it "should still consider model frozen after reloading association" do
@order.ship_method(true).should be_frozen
end
it "should ignore changes to associated model" do
@ship_method.update_attribute(:price, 8)
+ @order.ship_method.price.should == 5
@order.ship_method(true).price.should == 5
end
+
+ it "should be frozen after reloading order" do
+ @order.save!
+ @order.reload
+ @order.ship_method.should be_frozen
+ end
+
+ it "should not be frozen when unfreezing" do
+ @order.unfreeze_ship_method
+ @order.ship_method.should_not be_frozen
+ end
+
+ it "should restore original attributes when unfreezing" do
+ @ship_method.update_attribute(:price, 8)
+ @order.unfreeze_ship_method
+ @order.ship_method.price.should == 8
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.