ryanb-acts-as-list 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/acts-as-list.gemspec +4 -4
- data/lib/acts_as_list.rb +10 -10
- data/test/list_test.rb +37 -0
- metadata +2 -2
data/acts-as-list.gemspec
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
|
2
|
-
# Gem::Specification for Acts-as-list-0.1.
|
2
|
+
# Gem::Specification for Acts-as-list-0.1.2
|
3
3
|
# Originally generated by Echoe
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = %q{acts-as-list}
|
7
|
-
s.version = "0.1.
|
7
|
+
s.version = "0.1.2"
|
8
8
|
|
9
9
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
10
10
|
s.authors = ["Ryan Bates, Rails Core"]
|
11
|
-
s.date = %q{2008-07-
|
11
|
+
s.date = %q{2008-07-21}
|
12
12
|
s.description = %q{Gem version of acts_as_list Rails plugin.}
|
13
13
|
s.email = %q{ryan (at) railscasts (dot) com}
|
14
14
|
s.extra_rdoc_files = ["lib/acts_as_list.rb", "README", "tasks/deployment.rake"]
|
@@ -42,7 +42,7 @@ end
|
|
42
42
|
# begin
|
43
43
|
# require 'echoe'
|
44
44
|
#
|
45
|
-
# Echoe.new('acts-as-list', '0.1.
|
45
|
+
# Echoe.new('acts-as-list', '0.1.2') do |p|
|
46
46
|
# p.summary = "Gem version of acts_as_list Rails plugin."
|
47
47
|
# p.description = "Gem version of acts_as_list Rails plugin."
|
48
48
|
# p.url = "http://github.com/ryanb/acts-as-list"
|
data/lib/acts_as_list.rb
CHANGED
@@ -79,21 +79,21 @@ module ActsAsList
|
|
79
79
|
|
80
80
|
# Swap positions with the next lower item, if one exists.
|
81
81
|
def move_lower
|
82
|
-
|
83
|
-
|
82
|
+
lower = lower_item
|
83
|
+
return unless lower
|
84
84
|
acts_as_list_class.transaction do
|
85
|
-
|
86
|
-
|
85
|
+
self.update_attribute(position_column, lower.send(position_column))
|
86
|
+
lower.decrement_position
|
87
87
|
end
|
88
88
|
end
|
89
89
|
|
90
90
|
# Swap positions with the next higher item, if one exists.
|
91
91
|
def move_higher
|
92
|
-
|
93
|
-
|
92
|
+
higher = higher_item
|
93
|
+
return unless higher
|
94
94
|
acts_as_list_class.transaction do
|
95
|
-
|
96
|
-
|
95
|
+
self.update_attribute(position_column, higher.send(position_column))
|
96
|
+
higher.increment_position
|
97
97
|
end
|
98
98
|
end
|
99
99
|
|
@@ -153,7 +153,7 @@ module ActsAsList
|
|
153
153
|
def higher_item
|
154
154
|
return nil unless in_list?
|
155
155
|
acts_as_list_class.find(:first, :conditions =>
|
156
|
-
"#{scope_condition} AND #{position_column}
|
156
|
+
"#{scope_condition} AND #{position_column} < #{send(position_column).to_s}", :order => "#{position_column} DESC"
|
157
157
|
)
|
158
158
|
end
|
159
159
|
|
@@ -161,7 +161,7 @@ module ActsAsList
|
|
161
161
|
def lower_item
|
162
162
|
return nil unless in_list?
|
163
163
|
acts_as_list_class.find(:first, :conditions =>
|
164
|
-
"#{scope_condition} AND #{position_column}
|
164
|
+
"#{scope_condition} AND #{position_column} > #{send(position_column).to_s}", :order => "#{position_column} ASC"
|
165
165
|
)
|
166
166
|
end
|
167
167
|
|
data/test/list_test.rb
CHANGED
@@ -221,6 +221,43 @@ class ListTest < Test::Unit::TestCase
|
|
221
221
|
assert_equal 3, ListMixin.find(4).pos
|
222
222
|
end
|
223
223
|
|
224
|
+
# special thanks to openhood on github
|
225
|
+
def test_delete_middle_with_holes
|
226
|
+
# first we check everything is at expected place
|
227
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
228
|
+
|
229
|
+
# then we create a hole in the list, say you're working with existing data in which you already have holes
|
230
|
+
# or your scope is very complex
|
231
|
+
ListMixin.delete(2)
|
232
|
+
|
233
|
+
# we ensure the hole is really here
|
234
|
+
assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
235
|
+
assert_equal 1, ListMixin.find(1).pos
|
236
|
+
assert_equal 3, ListMixin.find(3).pos
|
237
|
+
assert_equal 4, ListMixin.find(4).pos
|
238
|
+
|
239
|
+
# can we retrieve lower item despite the hole?
|
240
|
+
assert_equal 3, ListMixin.find(1).lower_item.id
|
241
|
+
|
242
|
+
# can we move an item lower jumping more than one position?
|
243
|
+
ListMixin.find(1).move_lower
|
244
|
+
assert_equal [3, 1, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
245
|
+
assert_equal 2, ListMixin.find(3).pos
|
246
|
+
assert_equal 3, ListMixin.find(1).pos
|
247
|
+
assert_equal 4, ListMixin.find(4).pos
|
248
|
+
|
249
|
+
# create another hole
|
250
|
+
ListMixin.delete(1)
|
251
|
+
|
252
|
+
# can we retrieve higher item despite the hole?
|
253
|
+
assert_equal 3, ListMixin.find(4).higher_item.id
|
254
|
+
|
255
|
+
# can we move an item higher jumping more than one position?
|
256
|
+
ListMixin.find(4).move_higher
|
257
|
+
assert_equal [4, 3], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
258
|
+
assert_equal 2, ListMixin.find(4).pos
|
259
|
+
assert_equal 3, ListMixin.find(3).pos
|
260
|
+
end
|
224
261
|
end
|
225
262
|
|
226
263
|
class ListSubTest < Test::Unit::TestCase
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ryanb-acts-as-list
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Bates, Rails Core
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-07-
|
12
|
+
date: 2008-07-21 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|