Skip to content
This repository
Browse code

Wrapped multi-step operations in transactions

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@155 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 0a8f382b80efcaf26e2f3a432d07c137b72dd0d3 1 parent 647c5e0
David Heinemeier Hansson authored December 14, 2004
38  activerecord/lib/active_record/mixins/list.rb
@@ -37,24 +37,34 @@ def self.append_features(base)
37 37
 
38 38
       def move_lower
39 39
         return unless lower_item
40  
-        lower_item.decrement_position
41  
-        increment_position
  40
+
  41
+        self.class.transaction do
  42
+          lower_item.decrement_position
  43
+          increment_position
  44
+        end
42 45
       end
43 46
 
44 47
       def move_higher
45 48
         return unless higher_item
46  
-        higher_item.increment_position
47  
-        decrement_position
  49
+
  50
+        self.class.transaction do
  51
+          higher_item.increment_position
  52
+          decrement_position
  53
+        end
48 54
       end
49 55
     
50 56
       def move_to_bottom
51  
-        decrement_positions_on_lower_items
52  
-        assume_bottom_position
  57
+        self.class.transaction do
  58
+          decrement_positions_on_lower_items
  59
+          assume_bottom_position
  60
+        end
53 61
       end
54 62
 
55 63
       def move_to_top
56  
-        increment_positions_on_higher_items
57  
-        assume_top_position
  64
+        self.class.transaction do
  65
+          increment_positions_on_higher_items
  66
+          assume_top_position
  67
+        end
58 68
       end
59 69
     
60 70
 
@@ -76,13 +86,11 @@ def remove_from_list
76 86
       # Changing the position
77 87
 
78 88
       def increment_position
79  
-        self.position = position.to_i + 1
80  
-        save
  89
+        update_attribute "position", position.to_i + 1
81 90
       end
82 91
     
83 92
       def decrement_position
84  
-        self.position = position.to_i - 1
85  
-        save
  93
+        update_attribute "position", position.to_i - 1
86 94
       end
87 95
     
88 96
     
@@ -125,13 +133,11 @@ def bottom_item
125 133
         end
126 134
 
127 135
         def assume_bottom_position
128  
-          self.position = bottom_position_in_list.to_i + 1
129  
-          save
  136
+          update_attribute "position", bottom_position_in_list.to_i + 1
130 137
         end
131 138
       
132 139
         def assume_top_position
133  
-          self.position = 1
134  
-          save
  140
+          update_attribute "position", 1
135 141
         end
136 142
       
137 143
         def decrement_positions_on_lower_items

0 notes on commit 0a8f382

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