acts_as_list 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/acts_as_list/active_record/acts/list.rb +15 -15
- data/lib/acts_as_list/version.rb +1 -1
- data/test/shared_array_scope_list.rb +5 -0
- data/test/test_list.rb +8 -8
- metadata +10 -10
@@ -26,8 +26,8 @@ module ActiveRecord
|
|
26
26
|
# Configuration options are:
|
27
27
|
#
|
28
28
|
# * +column+ - specifies the column name to use for keeping the position integer (default: +position+)
|
29
|
-
# * +scope+ - restricts what is to be considered a list. Given a symbol, it'll attach <tt>_id</tt>
|
30
|
-
# (if it hasn't already been added) and use that as the foreign key restriction. It's also possible
|
29
|
+
# * +scope+ - restricts what is to be considered a list. Given a symbol, it'll attach <tt>_id</tt>
|
30
|
+
# (if it hasn't already been added) and use that as the foreign key restriction. It's also possible
|
31
31
|
# to give it an entire string that is interpolated if you need a tighter scope than just a foreign key.
|
32
32
|
# Example: <tt>acts_as_list :scope => 'todo_list_id = #{todo_list_id} AND completed = 0'</tt>
|
33
33
|
# * +top_of_list+ - defines the integer used for the top of the list. Defaults to 1. Use 0 to make the collection
|
@@ -48,7 +48,7 @@ module ActiveRecord
|
|
48
48
|
elsif configuration[:scope].is_a?(Array)
|
49
49
|
scope_condition_method = %(
|
50
50
|
def scope_condition
|
51
|
-
attrs = %w(#{configuration[:scope].join(" ")}).inject({}) do |memo,column|
|
51
|
+
attrs = %w(#{configuration[:scope].join(" ")}).inject({}) do |memo,column|
|
52
52
|
memo[column.intern] = send(column.intern); memo
|
53
53
|
end
|
54
54
|
self.class.send(:sanitize_sql_hash_for_conditions, attrs)
|
@@ -136,20 +136,20 @@ module ActiveRecord
|
|
136
136
|
def remove_from_list
|
137
137
|
if in_list?
|
138
138
|
decrement_positions_on_lower_items
|
139
|
-
|
139
|
+
update_attributes! position_column => nil
|
140
140
|
end
|
141
141
|
end
|
142
142
|
|
143
143
|
# Increase the position of this item without adjusting the rest of the list.
|
144
144
|
def increment_position
|
145
145
|
return unless in_list?
|
146
|
-
|
146
|
+
update_attributes! position_column => self.send(position_column).to_i + 1
|
147
147
|
end
|
148
148
|
|
149
149
|
# Decrease the position of this item without adjusting the rest of the list.
|
150
150
|
def decrement_position
|
151
151
|
return unless in_list?
|
152
|
-
|
152
|
+
update_attributes! position_column => self.send(position_column).to_i - 1
|
153
153
|
end
|
154
154
|
|
155
155
|
# Return +true+ if this object is the first in the list.
|
@@ -184,11 +184,11 @@ module ActiveRecord
|
|
184
184
|
def in_list?
|
185
185
|
!not_in_list?
|
186
186
|
end
|
187
|
-
|
187
|
+
|
188
188
|
def not_in_list?
|
189
189
|
send(position_column).nil?
|
190
190
|
end
|
191
|
-
|
191
|
+
|
192
192
|
def default_position
|
193
193
|
acts_as_list_class.columns_hash[position_column.to_s].default
|
194
194
|
end
|
@@ -230,12 +230,12 @@ module ActiveRecord
|
|
230
230
|
|
231
231
|
# Forces item to assume the bottom position in the list.
|
232
232
|
def assume_bottom_position
|
233
|
-
|
233
|
+
update_attributes!(position_column => bottom_position_in_list(self).to_i + 1)
|
234
234
|
end
|
235
235
|
|
236
236
|
# Forces item to assume the top position in the list.
|
237
237
|
def assume_top_position
|
238
|
-
|
238
|
+
update_attributes!(position_column => acts_as_list_top)
|
239
239
|
end
|
240
240
|
|
241
241
|
# This has the effect of moving all the higher items up one.
|
@@ -307,25 +307,25 @@ module ActiveRecord
|
|
307
307
|
else
|
308
308
|
increment_positions_on_lower_items(position)
|
309
309
|
end
|
310
|
-
self.
|
310
|
+
self.update_attributes!(position_column => position)
|
311
311
|
end
|
312
312
|
|
313
313
|
# used by insert_at_position instead of remove_from_list, as postgresql raises error if position_column has non-null constraint
|
314
314
|
def store_at_0
|
315
315
|
if in_list?
|
316
316
|
old_position = send(position_column).to_i
|
317
|
-
|
317
|
+
update_attributes!(position_column => 0)
|
318
318
|
decrement_positions_on_lower_items(old_position)
|
319
319
|
end
|
320
320
|
end
|
321
|
-
|
321
|
+
|
322
322
|
def update_positions
|
323
323
|
old_position = send("#{position_column}_was").to_i
|
324
324
|
new_position = send(position_column).to_i
|
325
|
-
return unless acts_as_list_class.unscoped.where("#{position_column} = #{new_position}").count > 1
|
325
|
+
return unless acts_as_list_class.unscoped.where("#{scope_condition} AND #{position_column} = #{new_position}").count > 1
|
326
326
|
shuffle_positions_on_intermediate_items old_position, new_position, id
|
327
327
|
end
|
328
|
-
end
|
328
|
+
end
|
329
329
|
end
|
330
330
|
end
|
331
331
|
end
|
data/lib/acts_as_list/version.rb
CHANGED
@@ -2,6 +2,7 @@ module Shared
|
|
2
2
|
module ArrayScopeList
|
3
3
|
def setup
|
4
4
|
(1..4).each { |counter| ArrayScopeListMixin.create! :pos => counter, :parent_id => 5, :parent_type => 'ParentClass' }
|
5
|
+
(1..4).each { |counter| ArrayScopeListMixin.create! :pos => counter, :parent_id => 6, :parent_type => 'ParentClass' }
|
5
6
|
end
|
6
7
|
|
7
8
|
def test_reordering
|
@@ -24,6 +25,10 @@ module Shared
|
|
24
25
|
|
25
26
|
ArrayScopeListMixin.find(4).move_to_top
|
26
27
|
assert_equal [4, 1, 3, 2], ArrayScopeListMixin.find(:all, :conditions => "parent_id = 5 AND parent_type = 'ParentClass'", :order => 'pos').map(&:id)
|
28
|
+
|
29
|
+
ArrayScopeListMixin.find(4).insert_at(4)
|
30
|
+
assert_equal [1, 3, 2, 4], ArrayScopeListMixin.find(:all, :conditions => "parent_id = 5 AND parent_type = 'ParentClass'", :order => 'pos').map(&:id)
|
31
|
+
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.find(:all, :conditions => "parent_id = 5 AND parent_type = 'ParentClass'", :order => 'pos').map(&:pos)
|
27
32
|
end
|
28
33
|
|
29
34
|
def test_move_to_bottom_with_next_to_last_item
|
data/test/test_list.rb
CHANGED
@@ -243,13 +243,13 @@ class DefaultScopedTest < ActsAsListTestCase
|
|
243
243
|
|
244
244
|
def test_update_position
|
245
245
|
assert_equal [1, 2, 3, 4], DefaultScopedMixin.find(:all).map(&:id)
|
246
|
-
DefaultScopedMixin.find(2).
|
246
|
+
DefaultScopedMixin.find(2).update_attributes!(:pos => 4)
|
247
247
|
assert_equal [1, 3, 4, 2], DefaultScopedMixin.find(:all).map(&:id)
|
248
|
-
DefaultScopedMixin.find(2).
|
248
|
+
DefaultScopedMixin.find(2).update_attributes!(:pos => 2)
|
249
249
|
assert_equal [1, 2, 3, 4], DefaultScopedMixin.find(:all).map(&:id)
|
250
|
-
DefaultScopedMixin.find(1).
|
250
|
+
DefaultScopedMixin.find(1).update_attributes!(:pos => 4)
|
251
251
|
assert_equal [2, 3, 4, 1], DefaultScopedMixin.find(:all).map(&:id)
|
252
|
-
DefaultScopedMixin.find(1).
|
252
|
+
DefaultScopedMixin.find(1).update_attributes!(:pos => 1)
|
253
253
|
assert_equal [1, 2, 3, 4], DefaultScopedMixin.find(:all).map(&:id)
|
254
254
|
end
|
255
255
|
|
@@ -337,13 +337,13 @@ class DefaultScopedWhereTest < ActsAsListTestCase
|
|
337
337
|
|
338
338
|
def test_update_position
|
339
339
|
assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
|
340
|
-
DefaultScopedWhereMixin.where(:active => false).find(2).
|
340
|
+
DefaultScopedWhereMixin.where(:active => false).find(2).update_attributes!(:pos => 4)
|
341
341
|
assert_equal [1, 3, 4, 2], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
|
342
|
-
DefaultScopedWhereMixin.where(:active => false).find(2).
|
342
|
+
DefaultScopedWhereMixin.where(:active => false).find(2).update_attributes!(:pos => 2)
|
343
343
|
assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
|
344
|
-
DefaultScopedWhereMixin.where(:active => false).find(1).
|
344
|
+
DefaultScopedWhereMixin.where(:active => false).find(1).update_attributes!(:pos => 4)
|
345
345
|
assert_equal [2, 3, 4, 1], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
|
346
|
-
DefaultScopedWhereMixin.where(:active => false).find(1).
|
346
|
+
DefaultScopedWhereMixin.where(:active => false).find(1).update_attributes!(:pos => 1)
|
347
347
|
assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
|
348
348
|
end
|
349
349
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_list
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.8
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,11 +11,11 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2012-
|
14
|
+
date: 2012-08-09 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: bundler
|
18
|
-
requirement: &
|
18
|
+
requirement: &2152115680 !ruby/object:Gem::Requirement
|
19
19
|
none: false
|
20
20
|
requirements:
|
21
21
|
- - ! '>='
|
@@ -23,10 +23,10 @@ dependencies:
|
|
23
23
|
version: 1.0.0
|
24
24
|
type: :development
|
25
25
|
prerelease: false
|
26
|
-
version_requirements: *
|
26
|
+
version_requirements: *2152115680
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activerecord
|
29
|
-
requirement: &
|
29
|
+
requirement: &2152115040 !ruby/object:Gem::Requirement
|
30
30
|
none: false
|
31
31
|
requirements:
|
32
32
|
- - ! '>='
|
@@ -34,10 +34,10 @@ dependencies:
|
|
34
34
|
version: 1.15.4.7794
|
35
35
|
type: :development
|
36
36
|
prerelease: false
|
37
|
-
version_requirements: *
|
37
|
+
version_requirements: *2152115040
|
38
38
|
- !ruby/object:Gem::Dependency
|
39
39
|
name: rdoc
|
40
|
-
requirement: &
|
40
|
+
requirement: &2152114620 !ruby/object:Gem::Requirement
|
41
41
|
none: false
|
42
42
|
requirements:
|
43
43
|
- - ! '>='
|
@@ -45,10 +45,10 @@ dependencies:
|
|
45
45
|
version: '0'
|
46
46
|
type: :development
|
47
47
|
prerelease: false
|
48
|
-
version_requirements: *
|
48
|
+
version_requirements: *2152114620
|
49
49
|
- !ruby/object:Gem::Dependency
|
50
50
|
name: sqlite3
|
51
|
-
requirement: &
|
51
|
+
requirement: &2152114020 !ruby/object:Gem::Requirement
|
52
52
|
none: false
|
53
53
|
requirements:
|
54
54
|
- - ! '>='
|
@@ -56,7 +56,7 @@ dependencies:
|
|
56
56
|
version: '0'
|
57
57
|
type: :development
|
58
58
|
prerelease: false
|
59
|
-
version_requirements: *
|
59
|
+
version_requirements: *2152114020
|
60
60
|
description: This "acts_as" extension provides the capabilities for sorting and reordering
|
61
61
|
a number of objects in a list. The class that has this specified needs to have a
|
62
62
|
"position" column defined as an integer on the mapped database table.
|