resort 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/resort/version.rb +1 -1
- data/lib/resort.rb +14 -12
- data/spec/resort_spec.rb +30 -0
- metadata +3 -5
data/lib/resort/version.rb
CHANGED
data/lib/resort.rb
CHANGED
@@ -93,7 +93,7 @@ module Resort
|
|
93
93
|
elements = {}
|
94
94
|
|
95
95
|
scoped.each do |element|
|
96
|
-
if element.first?
|
96
|
+
if ordered_elements.empty? && element.first?
|
97
97
|
ordered_elements << element
|
98
98
|
else
|
99
99
|
elements[element.id] = element
|
@@ -149,17 +149,17 @@ module Resort
|
|
149
149
|
|
150
150
|
# Puts the object in the first position of the list.
|
151
151
|
def prepend
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
self.lock!
|
152
|
+
self.class.transaction do
|
153
|
+
self.lock!
|
154
|
+
return if first?
|
155
|
+
if _siblings.count > 0
|
157
156
|
delete_from_list
|
158
|
-
|
157
|
+
old_first = self.class.first_in_order
|
158
|
+
self.update_attribute(:next_id, old_first.id)
|
159
|
+
old_first.update_attribute(:first, false)
|
159
160
|
end
|
161
|
+
self.update_attribute(:first, true)
|
160
162
|
end
|
161
|
-
|
162
|
-
self.update_attribute(:first, true)
|
163
163
|
end
|
164
164
|
|
165
165
|
# Puts the object in the last position of the list.
|
@@ -172,10 +172,9 @@ module Resort
|
|
172
172
|
|
173
173
|
# Puts the object right after another object in the list.
|
174
174
|
def append_to(another)
|
175
|
-
return if another.next_id == id
|
176
|
-
|
177
175
|
self.class.transaction do
|
178
176
|
self.lock!
|
177
|
+
return if another.next_id == id
|
179
178
|
another.lock!
|
180
179
|
delete_from_list
|
181
180
|
self.update_attribute(:next_id, another.next_id) if self.next_id or (another && another.next_id)
|
@@ -207,7 +206,10 @@ module Resort
|
|
207
206
|
end
|
208
207
|
|
209
208
|
def last!
|
210
|
-
|
209
|
+
self.class.transaction do
|
210
|
+
self.lock!
|
211
|
+
_siblings.last_in_order.update_attribute(:next_id, self.id)
|
212
|
+
end
|
211
213
|
end
|
212
214
|
|
213
215
|
def _siblings
|
data/spec/resort_spec.rb
CHANGED
@@ -168,6 +168,25 @@ module Resort
|
|
168
168
|
|
169
169
|
first.next_id.should eq(last.id)
|
170
170
|
end
|
171
|
+
|
172
|
+
it 'prepends the last element' do
|
173
|
+
one_list = List.create(:name => 'My list')
|
174
|
+
ListItem.create(:name => "First", :list => one_list)
|
175
|
+
ListItem.create(:name => "Second", :list => one_list)
|
176
|
+
third = ListItem.create(:name => "Third", :list => one_list)
|
177
|
+
|
178
|
+
third.prepend
|
179
|
+
first = ListItem.where(:name => "First", :list_id => one_list).first
|
180
|
+
second = ListItem.where(:name => "Second", :list_id => one_list).first
|
181
|
+
third = ListItem.where(:name => "Third", :list_id => one_list).first
|
182
|
+
|
183
|
+
first.should_not be_first
|
184
|
+
second.should_not be_first
|
185
|
+
third.should be_first
|
186
|
+
third.next.name.should == 'First'
|
187
|
+
first.next.name.should == 'Second'
|
188
|
+
second.next.should be_nil
|
189
|
+
end
|
171
190
|
end
|
172
191
|
after do
|
173
192
|
List.destroy_all
|
@@ -269,6 +288,17 @@ module Resort
|
|
269
288
|
article3.previous.should be_nil
|
270
289
|
article3.next.name.should == '1'
|
271
290
|
end
|
291
|
+
|
292
|
+
it "prepends the last element" do
|
293
|
+
@article4.prepend
|
294
|
+
|
295
|
+
article4 = Article.find_by_name('4')
|
296
|
+
|
297
|
+
article4.should be_first
|
298
|
+
article4.previous.should be_nil
|
299
|
+
article4.next.name.should == '1'
|
300
|
+
end
|
301
|
+
|
272
302
|
context 'when the article is already first' do
|
273
303
|
it 'does nothing' do
|
274
304
|
@article1.prepend
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: resort
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.2.
|
5
|
+
version: 0.2.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Oriol Gual
|
@@ -12,8 +12,7 @@ autorequire:
|
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
14
|
|
15
|
-
date: 2011-04-13 00:00:00
|
16
|
-
default_executable:
|
15
|
+
date: 2011-04-13 00:00:00 Z
|
17
16
|
dependencies:
|
18
17
|
- !ruby/object:Gem::Dependency
|
19
18
|
name: activerecord
|
@@ -106,7 +105,6 @@ files:
|
|
106
105
|
- spec/generators/migration_spec.rb
|
107
106
|
- spec/resort_spec.rb
|
108
107
|
- spec/spec_helper.rb
|
109
|
-
has_rdoc: true
|
110
108
|
homepage: http://codegram.github.com/resort
|
111
109
|
licenses: []
|
112
110
|
|
@@ -130,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
130
128
|
requirements: []
|
131
129
|
|
132
130
|
rubyforge_project: resort
|
133
|
-
rubygems_version: 1.
|
131
|
+
rubygems_version: 1.7.1
|
134
132
|
signing_key:
|
135
133
|
specification_version: 3
|
136
134
|
summary: Positionless model sorting for Rails 3.
|