webpulser-habtm_list 0.1.3 → 0.1.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/webpulser-habtm_list.rb +128 -132
- metadata +54 -28
data/lib/webpulser-habtm_list.rb
CHANGED
@@ -62,14 +62,62 @@ module RailsExtensions
|
|
62
62
|
|
63
63
|
module AssociationListMethods
|
64
64
|
def move_to_position(item, position)
|
65
|
-
return if !in_list?(item) || position.to_i == list_position(item)
|
65
|
+
#return if !in_list?(item) || position.to_i == list_position(item)
|
66
66
|
list_item_class.transaction do
|
67
67
|
remove_from_list(item)
|
68
|
+
decrement_positions_on_lower_items(item)
|
68
69
|
insert_at_position(item, position)
|
69
70
|
end
|
70
71
|
resort_array
|
71
72
|
end
|
72
73
|
|
74
|
+
# should only be called externally from the before_remove callback
|
75
|
+
def remove_from_list(item)
|
76
|
+
item[position_column] = nil
|
77
|
+
end
|
78
|
+
|
79
|
+
def insert_at_position(item, position)
|
80
|
+
increment_positions_on_lower_items(position)
|
81
|
+
set_position(item, position)
|
82
|
+
end
|
83
|
+
|
84
|
+
# This has the effect of moving all the lower items up one.
|
85
|
+
def decrement_positions_on_lower_items(item)
|
86
|
+
return unless in_list?(item)
|
87
|
+
position = list_position(item)
|
88
|
+
connection.update(
|
89
|
+
"UPDATE #{join_table} SET #{position_column} = (#{position_column} - 1) " +
|
90
|
+
"WHERE #{foreign_key} = #{owner.id} AND #{position_column} > #{position}"
|
91
|
+
)
|
92
|
+
target.each { |obj|
|
93
|
+
obj[position_column] = obj[position_column].to_i - 1 if in_list?(obj) && obj[position_column].to_i > position
|
94
|
+
} if target
|
95
|
+
end
|
96
|
+
|
97
|
+
# This has the effect of moving all the higher items down one.
|
98
|
+
def increment_positions_on_higher_items(item)
|
99
|
+
return unless in_list?(item)
|
100
|
+
position = list_position(item)
|
101
|
+
connection.update(
|
102
|
+
"UPDATE #{join_table} SET #{position_column} = (#{position_column} + 1) " +
|
103
|
+
"WHERE #{foreign_key} = #{owner.id} AND #{position_column} < #{position}"
|
104
|
+
)
|
105
|
+
target.each { |obj|
|
106
|
+
obj[position_column] = obj[position_column].to_i + 1 if in_list?(obj) && obj[position_column].to_i < position
|
107
|
+
} if target
|
108
|
+
end
|
109
|
+
|
110
|
+
def set_position(item, position)
|
111
|
+
connection.update(
|
112
|
+
"UPDATE #{join_table} SET #{position_column} = #{position} " +
|
113
|
+
"WHERE #{foreign_key} = #{owner.id} AND #{list_item_foreign_key} = #{item.id}"
|
114
|
+
) if owner.id
|
115
|
+
if target
|
116
|
+
obj = target.find {|obj| obj.id == item.id}
|
117
|
+
obj[position_column] = position if obj
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
73
121
|
def move_lower(item)
|
74
122
|
list_item_class.transaction do
|
75
123
|
lower = lower_item(item)
|
@@ -108,12 +156,6 @@ module RailsExtensions
|
|
108
156
|
resort_array
|
109
157
|
end
|
110
158
|
|
111
|
-
# should only be called externally from the before_remove callback
|
112
|
-
def remove_from_list(item)
|
113
|
-
decrement_positions_on_lower_items(item) if in_list?(item)
|
114
|
-
item[position_column] = nil
|
115
|
-
end
|
116
|
-
|
117
159
|
def first?(item)
|
118
160
|
item == self.first
|
119
161
|
end
|
@@ -174,148 +216,102 @@ module RailsExtensions
|
|
174
216
|
end
|
175
217
|
end
|
176
218
|
|
219
|
+
def position_column
|
220
|
+
proxy_association.reflection.options[:order] || 'position'
|
221
|
+
end
|
177
222
|
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
end
|
182
|
-
|
183
|
-
def list_item_class
|
184
|
-
proxy_association.reflection.klass
|
185
|
-
end
|
186
|
-
|
187
|
-
def owner
|
188
|
-
proxy_association.owner
|
189
|
-
end
|
190
|
-
|
191
|
-
def target
|
192
|
-
proxy_association.target
|
193
|
-
end
|
194
|
-
|
195
|
-
def join_table
|
196
|
-
proxy_association.reflection.options[:join_table]
|
197
|
-
end
|
198
|
-
|
199
|
-
def foreign_key
|
200
|
-
proxy_association.reflection.primary_key_name
|
201
|
-
end
|
202
|
-
|
203
|
-
def list_item_foreign_key
|
204
|
-
proxy_association.reflection.association_foreign_key
|
205
|
-
end
|
223
|
+
def list_item_class
|
224
|
+
proxy_association.reflection.klass
|
225
|
+
end
|
206
226
|
|
207
|
-
|
208
|
-
|
209
|
-
|
227
|
+
def owner
|
228
|
+
proxy_association.owner
|
229
|
+
end
|
210
230
|
|
231
|
+
def target
|
232
|
+
proxy_association.target
|
233
|
+
end
|
211
234
|
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
"WHERE #{foreign_key} = #{owner.id} AND #{list_item_foreign_key} = #{item.id}"
|
216
|
-
) if owner.id
|
217
|
-
if target
|
218
|
-
obj = target.find {|obj| obj.id == item.id}
|
219
|
-
obj[position_column] = position if obj
|
220
|
-
end
|
221
|
-
end
|
235
|
+
def join_table
|
236
|
+
proxy_association.reflection.options[:join_table]
|
237
|
+
end
|
222
238
|
|
223
|
-
|
224
|
-
|
225
|
-
|
239
|
+
def foreign_key
|
240
|
+
proxy_association.reflection.primary_key_name
|
241
|
+
end
|
226
242
|
|
227
|
-
|
228
|
-
|
229
|
-
|
243
|
+
def list_item_foreign_key
|
244
|
+
proxy_association.reflection.association_foreign_key
|
245
|
+
end
|
230
246
|
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
"UPDATE #{join_table} SET #{position_column} = #{position_column} + (#{increment}) " +
|
235
|
-
"WHERE #{foreign_key} = #{owner.id} AND #{list_item_foreign_key} = #{item.id}"
|
236
|
-
)
|
237
|
-
if target
|
238
|
-
obj = target.find {|obj| obj.id == item.id}
|
239
|
-
obj[position_column] = obj[position_column].to_i + increment if obj
|
240
|
-
end
|
241
|
-
end
|
247
|
+
def list_position(item)
|
248
|
+
self.index(item)
|
249
|
+
end
|
242
250
|
|
243
|
-
|
244
|
-
|
245
|
-
|
251
|
+
def assume_bottom_position(item)
|
252
|
+
set_position(item, self.length - 1)
|
253
|
+
end
|
246
254
|
|
247
|
-
|
248
|
-
|
249
|
-
|
255
|
+
def assume_top_position(item)
|
256
|
+
set_position(item, 0)
|
257
|
+
end
|
250
258
|
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
259
|
+
def increment_position_by(item, increment)
|
260
|
+
return unless in_list?(item)
|
261
|
+
connection.update(
|
262
|
+
"UPDATE #{join_table} SET #{position_column} = #{position_column} + (#{increment}) " +
|
263
|
+
"WHERE #{foreign_key} = #{owner.id} AND #{list_item_foreign_key} = #{item.id}"
|
264
|
+
)
|
265
|
+
if target
|
266
|
+
obj = target.find {|obj| obj.id == item.id}
|
267
|
+
obj[position_column] = obj[position_column].to_i + increment if obj
|
260
268
|
end
|
269
|
+
end
|
261
270
|
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
position = list_position(item)
|
266
|
-
connection.update(
|
267
|
-
"UPDATE #{join_table} SET #{position_column} = (#{position_column} - 1) " +
|
268
|
-
"WHERE #{foreign_key} = #{owner.id} AND #{position_column} > #{position}"
|
269
|
-
)
|
270
|
-
target.each { |obj|
|
271
|
-
obj[position_column] = obj[position_column].to_i - 1 if in_list?(obj) && obj[position_column].to_i > position
|
272
|
-
} if target
|
273
|
-
end
|
271
|
+
def increment_position(item)
|
272
|
+
increment_position_by(item, 1)
|
273
|
+
end
|
274
274
|
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
position = list_position(item)
|
279
|
-
connection.update(
|
280
|
-
"UPDATE #{join_table} SET #{position_column} = (#{position_column} + 1) " +
|
281
|
-
"WHERE #{foreign_key} = #{owner.id} AND #{position_column} < #{position}"
|
282
|
-
)
|
283
|
-
target.each { |obj|
|
284
|
-
obj[position_column] = obj[position_column].to_i + 1 if in_list?(obj) && obj[position_column].to_i < position
|
285
|
-
} if target
|
286
|
-
end
|
275
|
+
def decrement_position(item)
|
276
|
+
increment_position_by(item, -1)
|
277
|
+
end
|
287
278
|
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
279
|
+
# This has the effect of moving all the higher items up one.
|
280
|
+
def decrement_positions_on_higher_items(position)
|
281
|
+
connection.update(
|
282
|
+
"UPDATE #{join_table} SET #{position_column} = (#{position_column} - 1) " +
|
283
|
+
"WHERE #{foreign_key} = #{owner.id} AND #{position_column} <= #{position}"
|
284
|
+
)
|
285
|
+
target.each { |obj|
|
286
|
+
obj[position_column] = obj[position_column].to_i - 1 if in_list?(obj) && obj[position_column].to_i <= position
|
287
|
+
} if target
|
288
|
+
end
|
298
289
|
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
290
|
+
# This has the effect of moving all the lower items down one.
|
291
|
+
def increment_positions_on_lower_items(position)
|
292
|
+
connection.update(
|
293
|
+
"UPDATE #{join_table} SET #{position_column} = (#{position_column} + 1) " +
|
294
|
+
"WHERE #{foreign_key} = #{owner.id} AND #{position_column} >= #{position}"
|
295
|
+
)
|
296
|
+
target.each { |obj|
|
297
|
+
obj[position_column] = obj[position_column].to_i + 1 if in_list?(obj) && obj[position_column].to_i >= position
|
298
|
+
} if target
|
299
|
+
end
|
308
300
|
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
301
|
+
def increment_positions_on_all_items
|
302
|
+
connection.update(
|
303
|
+
"UPDATE #{join_table} SET #{position_column} = (#{position_column} + 1) " +
|
304
|
+
"WHERE #{foreign_key} = #{owner.id}"
|
305
|
+
)
|
306
|
+
target.each { |obj|
|
307
|
+
obj[position_column] = obj[position_column].to_i + 1 if in_list?(obj)
|
308
|
+
} if target
|
309
|
+
end
|
314
310
|
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
311
|
+
# called after changing position values so the array reflects the updated ordering
|
312
|
+
def resort_array
|
313
|
+
target.sort! {|x,y| x[position_column].to_i <=> y[position_column].to_i} if target
|
314
|
+
end
|
319
315
|
end
|
320
316
|
end
|
321
317
|
end
|
metadata
CHANGED
@@ -1,61 +1,87 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: webpulser-habtm_list
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 73
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 3
|
10
|
+
- 1
|
11
|
+
version: 0.1.3.1
|
6
12
|
platform: ruby
|
7
|
-
authors:
|
13
|
+
authors:
|
8
14
|
- James Healy
|
9
15
|
- Cyril LEPAGNOT
|
10
16
|
autorequire:
|
11
17
|
bindir: bin
|
12
18
|
cert_chain: []
|
13
|
-
|
14
|
-
|
15
|
-
|
19
|
+
|
20
|
+
date: 2012-05-13 00:00:00 +02:00
|
21
|
+
default_executable:
|
22
|
+
dependencies:
|
23
|
+
- !ruby/object:Gem::Dependency
|
16
24
|
name: activerecord
|
17
|
-
|
25
|
+
prerelease: false
|
26
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
18
27
|
none: false
|
19
|
-
requirements:
|
20
|
-
- -
|
21
|
-
- !ruby/object:Gem::Version
|
28
|
+
requirements:
|
29
|
+
- - ">="
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
hash: 3
|
32
|
+
segments:
|
33
|
+
- 3
|
34
|
+
- 1
|
35
|
+
- 0
|
22
36
|
version: 3.1.0
|
23
37
|
type: :runtime
|
24
|
-
|
25
|
-
|
26
|
-
description: Adds list-like position functionality to Rails has_and_belongs_to_many
|
27
|
-
associations
|
38
|
+
version_requirements: *id001
|
39
|
+
description: Adds list-like position functionality to Rails has_and_belongs_to_many associations
|
28
40
|
email: cyril.lepagnot@webpulser.com
|
29
41
|
executables: []
|
42
|
+
|
30
43
|
extensions: []
|
44
|
+
|
31
45
|
extra_rdoc_files: []
|
32
|
-
|
46
|
+
|
47
|
+
files:
|
33
48
|
- README
|
34
49
|
- MIT-LICENSE
|
35
50
|
- Rakefile
|
36
51
|
- lib/webpulser-habtm_list.rb
|
52
|
+
has_rdoc: true
|
37
53
|
homepage: http://wiki.rubyonrails.org/rails/pages/BetterHabtmList
|
38
54
|
licenses: []
|
55
|
+
|
39
56
|
post_install_message:
|
40
57
|
rdoc_options: []
|
41
|
-
|
58
|
+
|
59
|
+
require_paths:
|
42
60
|
- lib
|
43
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
62
|
none: false
|
45
|
-
requirements:
|
46
|
-
- -
|
47
|
-
- !ruby/object:Gem::Version
|
48
|
-
|
49
|
-
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
hash: 3
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
71
|
none: false
|
51
|
-
requirements:
|
52
|
-
- -
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 3
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
55
79
|
requirements: []
|
80
|
+
|
56
81
|
rubyforge_project:
|
57
|
-
rubygems_version: 1.
|
82
|
+
rubygems_version: 1.6.1
|
58
83
|
signing_key:
|
59
84
|
specification_version: 3
|
60
85
|
summary: has_and_belongs_to_many list-like
|
61
86
|
test_files: []
|
87
|
+
|