Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

overriding association accessor method so we can refreeze it when fet…

…ched
  • Loading branch information...
commit b16764ac059a21301a28173acd431eead5bd648c 1 parent 0bee132
Ryan Bates authored July 11, 2008
23  lib/association_freezer/model_additions.rb
@@ -2,13 +2,28 @@ module AssociationFreezer
2 2
   module ModelAdditions
3 3
     def enable_association_freezer
4 4
       reflect_on_all_associations(:belongs_to).each do |association|
5  
-        if column_names.include? "frozen_#{association.name}"
6  
-          define_method "freeze_#{association.name}" do
  5
+        
  6
+        # it is very important that we first make sure this hasn't already been done
  7
+        # because otherwise it will result in an endless loop the way alias method works.
  8
+        unless instance_methods.include? "freeze_#{association.name}"
7 9
           
8  
-          end
  10
+          if column_names.include? "frozen_#{association.name}"
  11
+            define_method "freeze_#{association.name}" do
  12
+              send(association.name, true)
  13
+            end
9 14
         
10  
-          define_method "unfreeze_#{association.name}" do
  15
+            define_method "unfreeze_#{association.name}" do
  16
+              
  17
+            end
  18
+          
11 19
           
  20
+            define_method "#{association.name}_with_frozen_check" do |*args|
  21
+              if !instance_variable_defined?("@#{association.name}") || args.first # force reload
  22
+                send("#{association.name}_without_frozen_check", *args).freeze
  23
+              end
  24
+              instance_variable_get("@#{association.name}")
  25
+            end
  26
+            alias_method_chain association.name, :frozen_check
12 27
           end
13 28
         end
14 29
       end
29  spec/freezer_spec.rb
@@ -8,19 +8,40 @@
8 8
   describe "with freezer" do
9 9
     before(:each) do
10 10
       Order.enable_association_freezer
  11
+      @order = Order.new
11 12
     end
12 13
     
13 14
     it "should add a freeze association method when enabling freezing" do
14  
-      Order.new.should respond_to(:freeze_ship_method)
  15
+      @order.should respond_to(:freeze_ship_method)
15 16
     end
16 17
   
17 18
     it "should add an unfreeze association method when enabling freezing" do
18  
-      Order.new.should respond_to(:unfreeze_ship_method)
  19
+      @order.should respond_to(:unfreeze_ship_method)
19 20
     end
20 21
   
21 22
     it "should not add freeze/unfreeze methods to cart association since it doesn't have a frozen column" do
22  
-      Order.new.should_not respond_to(:freeze_cart)
23  
-      Order.new.should_not respond_to(:unfreeze_cart)
  23
+      @order.should_not respond_to(:freeze_cart)
  24
+      @order.should_not respond_to(:unfreeze_cart)
  25
+    end
  26
+    
  27
+    describe "when freezing association" do
  28
+      before(:each) do
  29
+        @ship_method = ShipMethod.create!(:price => 5)
  30
+        @order.ship_method = @ship_method
  31
+        @order.freeze_ship_method
  32
+      end
  33
+      
  34
+      it "should freeze associated model" do
  35
+        @order.ship_method.should be_frozen
  36
+      end
  37
+      
  38
+      it "should not freeze original object" do
  39
+        @ship_method.should_not be_frozen
  40
+      end
  41
+      
  42
+      it "should still consider model frozen after reload" do
  43
+        @order.ship_method(true).should be_frozen
  44
+      end
24 45
     end
25 46
   end
26 47
 end

0 notes on commit b16764a

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