resort 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
1
  module Resort
2
2
  # Resort's version number
3
- VERSION = "0.2.0"
3
+ VERSION = "0.2.1"
4
4
  end
data/lib/resort.rb CHANGED
@@ -140,8 +140,11 @@ module Resort
140
140
  # in the first position. Otherwise, it appends it to the end of the
141
141
  # empty list.
142
142
  def include_in_list!
143
- _siblings.count > 0 ? last!\
144
- : prepend
143
+ self.class.transaction do
144
+ self.lock!
145
+ _siblings.count > 0 ? last!\
146
+ : prepend
147
+ end
145
148
  end
146
149
 
147
150
  # Puts the object in the first position of the list.
@@ -149,8 +152,11 @@ module Resort
149
152
  return if first?
150
153
 
151
154
  if _siblings.count > 0
152
- delete_from_list
153
- _siblings.where(:first => true).first.append_to(self)
155
+ self.class.transaction do
156
+ self.lock!
157
+ delete_from_list
158
+ _siblings.where(:first => true).first.append_to(self)
159
+ end
154
160
  end
155
161
 
156
162
  self.update_attribute(:first, true)
@@ -158,16 +164,20 @@ module Resort
158
164
 
159
165
  # Puts the object in the last position of the list.
160
166
  def push
161
- self.append_to(_siblings.last_in_order) unless last?
167
+ self.class.transaction do
168
+ self.lock!
169
+ self.append_to(_siblings.last_in_order) unless last?
170
+ end
162
171
  end
163
172
 
164
173
  # Puts the object right after another object in the list.
165
174
  def append_to(another)
166
175
  return if another.next_id == id
167
176
 
168
- delete_from_list
169
-
170
177
  self.class.transaction do
178
+ self.lock!
179
+ another.lock!
180
+ delete_from_list
171
181
  self.update_attribute(:next_id, another.next_id) if self.next_id or (another && another.next_id)
172
182
  another.update_attribute(:next_id, self.id) if another
173
183
  end
@@ -176,19 +186,19 @@ module Resort
176
186
  private
177
187
 
178
188
  def delete_from_list
179
- self.class.transaction do
180
- if first? && self.next
181
- self.next.update_attribute(:first, true)
182
- elsif self.previous
183
- self.previous.update_attribute(:next_id, self.next_id)
184
- end
189
+ if first? && self.next
190
+ self.next.lock!
191
+ self.next.update_attribute(:first, true)
192
+ elsif self.previous
193
+ self.previous.lock!
194
+ self.previous.update_attribute(:next_id, self.next_id)
195
+ end
185
196
 
186
- unless frozen?
187
- self.first = false
188
- self.next = nil
189
- self.previous = nil
190
- save!
191
- end
197
+ unless frozen?
198
+ self.first = false
199
+ self.next = nil
200
+ self.previous = nil
201
+ save!
192
202
  end
193
203
  end
194
204
 
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,8 @@
1
1
  require 'rspec'
2
+
3
+ module Rails
4
+ end
5
+
2
6
  require 'resort'
3
7
  require 'logger'
4
8
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: resort
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.0
5
+ version: 0.2.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Oriol Gual
@@ -12,7 +12,7 @@ autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
14
 
15
- date: 2011-03-30 00:00:00 +02:00
15
+ date: 2011-04-13 00:00:00 +02:00
16
16
  default_executable:
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
@@ -130,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
130
  requirements: []
131
131
 
132
132
  rubyforge_project: resort
133
- rubygems_version: 1.5.3
133
+ rubygems_version: 1.5.2
134
134
  signing_key:
135
135
  specification_version: 3
136
136
  summary: Positionless model sorting for Rails 3.