acts_as_silent_list 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.rdoc
CHANGED
|
@@ -5,9 +5,10 @@ module ActiveRecord
|
|
|
5
5
|
base.extend(ClassMethods)
|
|
6
6
|
end
|
|
7
7
|
|
|
8
|
-
# This +acts_as+ extension provides the capabilities for sorting and
|
|
9
|
-
#
|
|
10
|
-
#
|
|
8
|
+
# This +acts_as+ extension provides the capabilities for sorting and
|
|
9
|
+
# reordering a number of objects in a list. The class that has this
|
|
10
|
+
# specified needs to have a +position+ column defined as an integer on the
|
|
11
|
+
# mapped database table.
|
|
11
12
|
#
|
|
12
13
|
# Todo list example:
|
|
13
14
|
#
|
|
@@ -25,13 +26,17 @@ module ActiveRecord
|
|
|
25
26
|
module ClassMethods
|
|
26
27
|
# Configuration options are:
|
|
27
28
|
#
|
|
28
|
-
# * +column+ - specifies the column name to use for keeping the position
|
|
29
|
-
#
|
|
30
|
-
#
|
|
31
|
-
#
|
|
32
|
-
#
|
|
33
|
-
#
|
|
34
|
-
#
|
|
29
|
+
# * +column+ - specifies the column name to use for keeping the position
|
|
30
|
+
# integer (default: +position+)
|
|
31
|
+
# * +scope+ - restricts what is to be considered a list. Given a symbol,
|
|
32
|
+
# it'll attach <tt>_id</tt> (if it hasn't already been added) and use
|
|
33
|
+
# that as the foreign key restriction. It's also possible to give it
|
|
34
|
+
# an entire string that is interpolated if you need a tighter scope
|
|
35
|
+
# than just a foreign key. Example: <tt>acts_as_silent_list :scope =>
|
|
36
|
+
# 'todo_list_id = #{todo_list_id} AND completed = 0'</tt>
|
|
37
|
+
# * +top_of_list+ - defines the integer used for the top of the list.
|
|
38
|
+
# Defaults to 1. Use 0 to make the collection act more like an array
|
|
39
|
+
# in its indexing.
|
|
35
40
|
def acts_as_silent_list(options = {})
|
|
36
41
|
configuration = { :column => "position", :scope => "1 = 1", :top_of_list => 1}
|
|
37
42
|
configuration.update(options) if options.is_a?(Hash)
|
|
@@ -85,12 +90,15 @@ module ActiveRecord
|
|
|
85
90
|
end
|
|
86
91
|
end
|
|
87
92
|
|
|
88
|
-
# All the methods available to a record that has had
|
|
89
|
-
#
|
|
90
|
-
#
|
|
91
|
-
#
|
|
93
|
+
# All the methods available to a record that has had
|
|
94
|
+
# <tt>acts_as_silent_list</tt> specified. Each method works by assuming
|
|
95
|
+
# the object to be the item in the list, so <tt>chapter.move_lower</tt>
|
|
96
|
+
# would move that chapter lower in the list of all chapters. Likewise,
|
|
97
|
+
# <tt>chapter.first?</tt> would return +true+ if that chapter is the first
|
|
98
|
+
# in the list of all chapters.
|
|
92
99
|
module InstanceMethods
|
|
93
|
-
# Insert the item at the given position
|
|
100
|
+
# Insert the item at the given position
|
|
101
|
+
# (defaults to the top position of 1).
|
|
94
102
|
def insert_at(position = acts_as_silent_list_top)
|
|
95
103
|
insert_at_position(position)
|
|
96
104
|
end
|
|
@@ -115,8 +123,8 @@ module ActiveRecord
|
|
|
115
123
|
end
|
|
116
124
|
end
|
|
117
125
|
|
|
118
|
-
# Move to the bottom of the list. If the item is already in the list,
|
|
119
|
-
# position adjusted accordingly.
|
|
126
|
+
# Move to the bottom of the list. If the item is already in the list,
|
|
127
|
+
# the items below it have their position adjusted accordingly.
|
|
120
128
|
def move_to_bottom
|
|
121
129
|
return unless in_list?
|
|
122
130
|
acts_as_silent_list_class.transaction do
|
|
@@ -125,8 +133,8 @@ module ActiveRecord
|
|
|
125
133
|
end
|
|
126
134
|
end
|
|
127
135
|
|
|
128
|
-
# Move to the top of the list. If the item is already in the list, the
|
|
129
|
-
# position adjusted accordingly.
|
|
136
|
+
# Move to the top of the list. If the item is already in the list, the
|
|
137
|
+
# items above it have their position adjusted accordingly.
|
|
130
138
|
def move_to_top
|
|
131
139
|
return unless in_list?
|
|
132
140
|
acts_as_silent_list_class.transaction do
|
|
@@ -139,17 +147,19 @@ module ActiveRecord
|
|
|
139
147
|
def remove_from_list
|
|
140
148
|
if in_list?
|
|
141
149
|
decrement_positions_on_lower_items
|
|
142
|
-
|
|
150
|
+
assume_not_in_list
|
|
143
151
|
end
|
|
144
152
|
end
|
|
145
153
|
|
|
146
|
-
# Increase the position of this item without adjusting the rest of the
|
|
154
|
+
# Increase the position of this item without adjusting the rest of the
|
|
155
|
+
# list.
|
|
147
156
|
def increment_position
|
|
148
157
|
return unless in_list?
|
|
149
158
|
update_attribute_silently(position_column, self.send(position_column).to_i + 1)
|
|
150
159
|
end
|
|
151
160
|
|
|
152
|
-
# Decrease the position of this item without adjusting the rest of the
|
|
161
|
+
# Decrease the position of this item without adjusting the rest of the
|
|
162
|
+
# list.
|
|
153
163
|
def decrement_position
|
|
154
164
|
return unless in_list?
|
|
155
165
|
update_attribute_silently(position_column, self.send(position_column).to_i - 1)
|
|
@@ -228,13 +238,16 @@ module ActiveRecord
|
|
|
228
238
|
conditions = scope_condition
|
|
229
239
|
conditions = "#{conditions} AND #{self.class.primary_key} != #{except.id}" if except
|
|
230
240
|
acts_as_silent_list_class.unscoped do
|
|
231
|
-
acts_as_silent_list_class.find(:first,
|
|
241
|
+
acts_as_silent_list_class.find(:first,
|
|
242
|
+
:conditions => conditions,
|
|
243
|
+
:order => "#{position_column} DESC")
|
|
232
244
|
end
|
|
233
245
|
end
|
|
234
246
|
|
|
235
247
|
# Forces item to assume the bottom position in the list.
|
|
236
248
|
def assume_bottom_position
|
|
237
|
-
update_attribute_silently(position_column,
|
|
249
|
+
update_attribute_silently(position_column,
|
|
250
|
+
bottom_position_in_list(self).to_i + 1)
|
|
238
251
|
end
|
|
239
252
|
|
|
240
253
|
# Forces item to assume the top position in the list.
|
|
@@ -242,10 +255,16 @@ module ActiveRecord
|
|
|
242
255
|
update_attribute_silently(position_column, acts_as_silent_list_top)
|
|
243
256
|
end
|
|
244
257
|
|
|
258
|
+
# Force item to assume no position in the list.
|
|
259
|
+
def assume_not_in_list
|
|
260
|
+
update_attribute_silently(position_column, nil)
|
|
261
|
+
end
|
|
262
|
+
|
|
245
263
|
# This has the effect of moving all the higher items up one.
|
|
246
264
|
def decrement_positions_on_higher_items(position)
|
|
247
265
|
acts_as_silent_list_class.update_all(
|
|
248
|
-
"#{position_column} = (#{position_column} - 1)",
|
|
266
|
+
"#{position_column} = (#{position_column} - 1)",
|
|
267
|
+
"#{scope_condition} AND #{position_column} <= #{position}"
|
|
249
268
|
)
|
|
250
269
|
end
|
|
251
270
|
|
|
@@ -253,7 +272,8 @@ module ActiveRecord
|
|
|
253
272
|
def decrement_positions_on_lower_items
|
|
254
273
|
return unless in_list?
|
|
255
274
|
acts_as_silent_list_class.update_all(
|
|
256
|
-
"#{position_column} = (#{position_column} - 1)",
|
|
275
|
+
"#{position_column} = (#{position_column} - 1)",
|
|
276
|
+
"#{scope_condition} AND #{position_column} > #{send(position_column).to_i}"
|
|
257
277
|
)
|
|
258
278
|
end
|
|
259
279
|
|
|
@@ -261,21 +281,25 @@ module ActiveRecord
|
|
|
261
281
|
def increment_positions_on_higher_items
|
|
262
282
|
return unless in_list?
|
|
263
283
|
acts_as_silent_list_class.update_all(
|
|
264
|
-
"#{position_column} = (#{position_column} + 1)",
|
|
284
|
+
"#{position_column} = (#{position_column} + 1)",
|
|
285
|
+
"#{scope_condition} AND #{position_column} < #{send(position_column).to_i}"
|
|
265
286
|
)
|
|
266
287
|
end
|
|
267
288
|
|
|
268
289
|
# This has the effect of moving all the lower items down one.
|
|
269
290
|
def increment_positions_on_lower_items(position)
|
|
270
291
|
acts_as_silent_list_class.update_all(
|
|
271
|
-
"#{position_column} = (#{position_column} + 1)",
|
|
272
|
-
|
|
292
|
+
"#{position_column} = (#{position_column} + 1)",
|
|
293
|
+
"#{scope_condition} AND #{position_column} >= #{position}"
|
|
294
|
+
)
|
|
273
295
|
end
|
|
274
296
|
|
|
275
|
-
# Increments position (<tt>position_column</tt>) of all items in the
|
|
297
|
+
# Increments position (<tt>position_column</tt>) of all items in the
|
|
298
|
+
# list.
|
|
276
299
|
def increment_positions_on_all_items
|
|
277
300
|
acts_as_silent_list_class.update_all(
|
|
278
|
-
"#{position_column} = (#{position_column} + 1)",
|
|
301
|
+
"#{position_column} = (#{position_column} + 1)",
|
|
302
|
+
"#{scope_condition}"
|
|
279
303
|
)
|
|
280
304
|
end
|
|
281
305
|
|
|
@@ -285,7 +309,8 @@ module ActiveRecord
|
|
|
285
309
|
update_attribute_silently(position_column, position)
|
|
286
310
|
end
|
|
287
311
|
|
|
288
|
-
# used by insert_at_position instead of remove_from_list, as
|
|
312
|
+
# used by insert_at_position instead of remove_from_list, as
|
|
313
|
+
# postgresql raises error if position_column has non-null constraint
|
|
289
314
|
def store_at_0
|
|
290
315
|
if in_list?
|
|
291
316
|
decrement_positions_on_lower_items
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: acts_as_silent_list
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
hash:
|
|
4
|
+
hash: 19
|
|
5
5
|
prerelease:
|
|
6
6
|
segments:
|
|
7
7
|
- 1
|
|
8
|
+
- 1
|
|
8
9
|
- 0
|
|
9
|
-
|
|
10
|
-
version: 1.0.0
|
|
10
|
+
version: 1.1.0
|
|
11
11
|
platform: ruby
|
|
12
12
|
authors:
|
|
13
13
|
- David Heinemeier Hansson
|
|
@@ -18,11 +18,10 @@ autorequire:
|
|
|
18
18
|
bindir: bin
|
|
19
19
|
cert_chain: []
|
|
20
20
|
|
|
21
|
-
date: 2011-10-
|
|
21
|
+
date: 2011-10-19 00:00:00 +02:00
|
|
22
22
|
default_executable:
|
|
23
23
|
dependencies:
|
|
24
24
|
- !ruby/object:Gem::Dependency
|
|
25
|
-
name: bundler
|
|
26
25
|
prerelease: false
|
|
27
26
|
requirement: &id001 !ruby/object:Gem::Requirement
|
|
28
27
|
none: false
|
|
@@ -36,9 +35,9 @@ dependencies:
|
|
|
36
35
|
- 0
|
|
37
36
|
version: 1.0.0
|
|
38
37
|
type: :development
|
|
38
|
+
name: bundler
|
|
39
39
|
version_requirements: *id001
|
|
40
40
|
- !ruby/object:Gem::Dependency
|
|
41
|
-
name: activerecord
|
|
42
41
|
prerelease: false
|
|
43
42
|
requirement: &id002 !ruby/object:Gem::Requirement
|
|
44
43
|
none: false
|
|
@@ -53,9 +52,9 @@ dependencies:
|
|
|
53
52
|
- 7794
|
|
54
53
|
version: 1.15.4.7794
|
|
55
54
|
type: :development
|
|
55
|
+
name: activerecord
|
|
56
56
|
version_requirements: *id002
|
|
57
57
|
- !ruby/object:Gem::Dependency
|
|
58
|
-
name: rake
|
|
59
58
|
prerelease: false
|
|
60
59
|
requirement: &id003 !ruby/object:Gem::Requirement
|
|
61
60
|
none: false
|
|
@@ -67,9 +66,9 @@ dependencies:
|
|
|
67
66
|
- 0
|
|
68
67
|
version: "0"
|
|
69
68
|
type: :development
|
|
69
|
+
name: rake
|
|
70
70
|
version_requirements: *id003
|
|
71
71
|
- !ruby/object:Gem::Dependency
|
|
72
|
-
name: rdoc
|
|
73
72
|
prerelease: false
|
|
74
73
|
requirement: &id004 !ruby/object:Gem::Requirement
|
|
75
74
|
none: false
|
|
@@ -81,9 +80,9 @@ dependencies:
|
|
|
81
80
|
- 0
|
|
82
81
|
version: "0"
|
|
83
82
|
type: :development
|
|
83
|
+
name: rdoc
|
|
84
84
|
version_requirements: *id004
|
|
85
85
|
- !ruby/object:Gem::Dependency
|
|
86
|
-
name: sqlite3
|
|
87
86
|
prerelease: false
|
|
88
87
|
requirement: &id005 !ruby/object:Gem::Requirement
|
|
89
88
|
none: false
|
|
@@ -95,6 +94,7 @@ dependencies:
|
|
|
95
94
|
- 0
|
|
96
95
|
version: "0"
|
|
97
96
|
type: :development
|
|
97
|
+
name: sqlite3
|
|
98
98
|
version_requirements: *id005
|
|
99
99
|
description: This "acts_as" extension is a clone of the well known acts_as_list, only it avoids triggering active record callbacks.
|
|
100
100
|
email:
|