webpulser-habtm_list 0.1.2 → 0.1.3

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.
Files changed (2) hide show
  1. data/lib/webpulser-habtm_list.rb +62 -54
  2. metadata +35 -40
@@ -1,6 +1,6 @@
1
1
  module RailsExtensions
2
2
  module HabtmList
3
-
3
+
4
4
  def self.append_features(base) #:nodoc:
5
5
  super
6
6
  base.extend(ClassMethods)
@@ -20,7 +20,7 @@ module RailsExtensions
20
20
  before_remove_callback_symbol = "maintain_list_before_remove_for_#{name}".to_sym
21
21
  name_ids = "#{name.to_s.singularize}_ids"
22
22
  name_ids_symbol = ":#{name_ids}"
23
-
23
+
24
24
  options[:after_add] ||= []
25
25
  options[:after_add] << after_add_callback_symbol
26
26
 
@@ -31,7 +31,7 @@ module RailsExtensions
31
31
  def #{after_add_callback_symbol}(added)
32
32
  self.#{name}.add_to_list_bottom(added)
33
33
  end
34
-
34
+
35
35
  def #{before_remove_callback_symbol}(removed)
36
36
  self.#{name}.remove_from_list(removed)
37
37
  end
@@ -59,7 +59,7 @@ module RailsExtensions
59
59
  has_and_belongs_to_many_without_list_handling(name, options, &extension)
60
60
  end
61
61
  end
62
-
62
+
63
63
  module AssociationListMethods
64
64
  def move_to_position(item, position)
65
65
  return if !in_list?(item) || position.to_i == list_position(item)
@@ -69,7 +69,7 @@ module RailsExtensions
69
69
  end
70
70
  resort_array
71
71
  end
72
-
72
+
73
73
  def move_lower(item)
74
74
  list_item_class.transaction do
75
75
  lower = lower_item(item)
@@ -79,7 +79,7 @@ module RailsExtensions
79
79
  end
80
80
  resort_array
81
81
  end
82
-
82
+
83
83
  def move_higher(item)
84
84
  list_item_class.transaction do
85
85
  higher = higher_item(item)
@@ -89,7 +89,7 @@ module RailsExtensions
89
89
  end
90
90
  resort_array
91
91
  end
92
-
92
+
93
93
  def move_to_bottom(item)
94
94
  return unless in_list?(item)
95
95
  list_item_class.transaction do
@@ -98,7 +98,7 @@ module RailsExtensions
98
98
  end
99
99
  resort_array
100
100
  end
101
-
101
+
102
102
  def move_to_top(item)
103
103
  return unless in_list?(item)
104
104
  list_item_class.transaction do
@@ -117,11 +117,11 @@ module RailsExtensions
117
117
  def first?(item)
118
118
  item == self.first
119
119
  end
120
-
120
+
121
121
  def last?(item)
122
122
  item == self.last
123
123
  end
124
-
124
+
125
125
  def higher_item(item)
126
126
  return nil unless in_list?(item)
127
127
  self.find(:first, :conditions => "#{position_column} = #{(list_position(item) - 1).to_s}")
@@ -137,13 +137,13 @@ module RailsExtensions
137
137
  end
138
138
 
139
139
  def add_to_list_bottom(item)
140
- item.save! if item.id.nil? # Rails 2.0.2 - Callbacks don't save first on association.create()
140
+ item.save! if item.id.nil? # Rails 2.0.2 - Callbacks don't save first on association.create()
141
141
  list_item_class.transaction do
142
142
  assume_bottom_position(item)
143
143
  end
144
144
  resort_array
145
145
  end
146
-
146
+
147
147
  def add_to_list_top(item)
148
148
  list_item_class.transaction do
149
149
  increment_positions_on_all_items
@@ -151,7 +151,7 @@ module RailsExtensions
151
151
  end
152
152
  resort_array
153
153
  end
154
-
154
+
155
155
  # "First aid" method in case someone shifts the array around outside these methods, or
156
156
  # the positions in the joins table go totally out of whack. Don't use it for
157
157
  # simple ordering because it's inefficient.
@@ -160,42 +160,50 @@ module RailsExtensions
160
160
  item = self[i]
161
161
  connection.update(
162
162
  "UPDATE #{join_table} SET #{position_column} = #{i} " +
163
- "WHERE #{foreign_key} = #{@owner.id} AND #{list_item_foreign_key} = #{item.id}"
163
+ "WHERE #{foreign_key} = #{owner.id} AND #{list_item_foreign_key} = #{item.id}"
164
164
  )
165
165
  end
166
166
  end
167
-
167
+
168
168
  def reset_positions_by_ids(ids = self.collect(&:id))
169
169
  ids.each_with_index do |id, i|
170
170
  connection.update(
171
171
  "UPDATE #{join_table} SET #{position_column} = #{i} " +
172
- "WHERE #{foreign_key} = #{@owner.id} AND #{list_item_foreign_key} = #{id}"
172
+ "WHERE #{foreign_key} = #{owner.id} AND #{list_item_foreign_key} = #{id}"
173
173
  ) if id.to_i != 0
174
174
  end
175
175
  end
176
-
177
-
176
+
177
+
178
178
  private
179
179
  def position_column
180
- @reflection.options[:order] || 'position'
180
+ proxy_association.reflection.options[:order] || 'position'
181
181
  end
182
-
182
+
183
183
  def list_item_class
184
- @reflection.klass
184
+ proxy_association.reflection.klass
185
+ end
186
+
187
+ def owner
188
+ proxy_association.owner
185
189
  end
186
-
190
+
191
+ def target
192
+ proxy_association.target
193
+ end
194
+
187
195
  def join_table
188
- @reflection.options[:join_table]
196
+ proxy_association.reflection.options[:join_table]
189
197
  end
190
-
198
+
191
199
  def foreign_key
192
- @reflection.primary_key_name
200
+ proxy_association.reflection.primary_key_name
193
201
  end
194
-
202
+
195
203
  def list_item_foreign_key
196
- @reflection.association_foreign_key
204
+ proxy_association.reflection.association_foreign_key
197
205
  end
198
-
206
+
199
207
  def list_position(item)
200
208
  self.index(item)
201
209
  end
@@ -204,10 +212,10 @@ module RailsExtensions
204
212
  def set_position(item, position)
205
213
  connection.update(
206
214
  "UPDATE #{join_table} SET #{position_column} = #{position} " +
207
- "WHERE #{foreign_key} = #{@owner.id} AND #{list_item_foreign_key} = #{item.id}"
208
- ) if @owner.id
209
- if @target
210
- obj = @target.find {|obj| obj.id == item.id}
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}
211
219
  obj[position_column] = position if obj
212
220
  end
213
221
  end
@@ -224,10 +232,10 @@ module RailsExtensions
224
232
  return unless in_list?(item)
225
233
  connection.update(
226
234
  "UPDATE #{join_table} SET #{position_column} = #{position_column} + (#{increment}) " +
227
- "WHERE #{foreign_key} = #{@owner.id} AND #{list_item_foreign_key} = #{item.id}"
235
+ "WHERE #{foreign_key} = #{owner.id} AND #{list_item_foreign_key} = #{item.id}"
228
236
  )
229
- if @target
230
- obj = @target.find {|obj| obj.id == item.id}
237
+ if target
238
+ obj = target.find {|obj| obj.id == item.id}
231
239
  obj[position_column] = obj[position_column].to_i + increment if obj
232
240
  end
233
241
  end
@@ -244,24 +252,24 @@ module RailsExtensions
244
252
  def decrement_positions_on_higher_items(position)
245
253
  connection.update(
246
254
  "UPDATE #{join_table} SET #{position_column} = (#{position_column} - 1) " +
247
- "WHERE #{foreign_key} = #{@owner.id} AND #{position_column} <= #{position}"
255
+ "WHERE #{foreign_key} = #{owner.id} AND #{position_column} <= #{position}"
248
256
  )
249
- @target.each { |obj|
257
+ target.each { |obj|
250
258
  obj[position_column] = obj[position_column].to_i - 1 if in_list?(obj) && obj[position_column].to_i <= position
251
- } if @target
259
+ } if target
252
260
  end
253
-
261
+
254
262
  # This has the effect of moving all the lower items up one.
255
263
  def decrement_positions_on_lower_items(item)
256
264
  return unless in_list?(item)
257
265
  position = list_position(item)
258
266
  connection.update(
259
267
  "UPDATE #{join_table} SET #{position_column} = (#{position_column} - 1) " +
260
- "WHERE #{foreign_key} = #{@owner.id} AND #{position_column} > #{position}"
268
+ "WHERE #{foreign_key} = #{owner.id} AND #{position_column} > #{position}"
261
269
  )
262
- @target.each { |obj|
270
+ target.each { |obj|
263
271
  obj[position_column] = obj[position_column].to_i - 1 if in_list?(obj) && obj[position_column].to_i > position
264
- } if @target
272
+ } if target
265
273
  end
266
274
 
267
275
  # This has the effect of moving all the higher items down one.
@@ -270,32 +278,32 @@ module RailsExtensions
270
278
  position = list_position(item)
271
279
  connection.update(
272
280
  "UPDATE #{join_table} SET #{position_column} = (#{position_column} + 1) " +
273
- "WHERE #{foreign_key} = #{@owner.id} AND #{position_column} < #{position}"
281
+ "WHERE #{foreign_key} = #{owner.id} AND #{position_column} < #{position}"
274
282
  )
275
- @target.each { |obj|
283
+ target.each { |obj|
276
284
  obj[position_column] = obj[position_column].to_i + 1 if in_list?(obj) && obj[position_column].to_i < position
277
- } if @target
285
+ } if target
278
286
  end
279
287
 
280
288
  # This has the effect of moving all the lower items down one.
281
289
  def increment_positions_on_lower_items(position)
282
290
  connection.update(
283
291
  "UPDATE #{join_table} SET #{position_column} = (#{position_column} + 1) " +
284
- "WHERE #{foreign_key} = #{@owner.id} AND #{position_column} >= #{position}"
292
+ "WHERE #{foreign_key} = #{owner.id} AND #{position_column} >= #{position}"
285
293
  )
286
- @target.each { |obj|
294
+ target.each { |obj|
287
295
  obj[position_column] = obj[position_column].to_i + 1 if in_list?(obj) && obj[position_column].to_i >= position
288
- } if @target
296
+ } if target
289
297
  end
290
298
 
291
299
  def increment_positions_on_all_items
292
300
  connection.update(
293
301
  "UPDATE #{join_table} SET #{position_column} = (#{position_column} + 1) " +
294
- "WHERE #{foreign_key} = #{@owner.id}"
302
+ "WHERE #{foreign_key} = #{owner.id}"
295
303
  )
296
- @target.each { |obj|
304
+ target.each { |obj|
297
305
  obj[position_column] = obj[position_column].to_i + 1 if in_list?(obj)
298
- } if @target
306
+ } if target
299
307
  end
300
308
 
301
309
  def insert_at_position(item, position)
@@ -303,11 +311,11 @@ module RailsExtensions
303
311
  increment_positions_on_lower_items(position)
304
312
  set_position(item, position)
305
313
  end
306
-
314
+
307
315
  # called after changing position values so the array reflects the updated ordering
308
316
  def resort_array
309
- @target.sort! {|x,y| x[position_column].to_i <=> y[position_column].to_i} if @target
310
- end
317
+ target.sort! {|x,y| x[position_column].to_i <=> y[position_column].to_i} if target
318
+ end
311
319
  end
312
320
  end
313
321
  end
metadata CHANGED
@@ -1,66 +1,61 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: webpulser-habtm_list
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 1
8
- - 2
9
- version: 0.1.2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - James Healy
13
9
  - Cyril LEPAGNOT
14
10
  autorequire:
15
11
  bindir: bin
16
12
  cert_chain: []
17
-
18
- date: 2009-08-16 00:00:00 +02:00
19
- default_executable:
20
- dependencies: []
21
-
22
- description: Adds list-like position functionality to Rails has_and_belongs_to_many associations
13
+ date: 2011-09-01 00:00:00.000000000Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activerecord
17
+ requirement: &7573920 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 3.1.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *7573920
26
+ description: Adds list-like position functionality to Rails has_and_belongs_to_many
27
+ associations
23
28
  email: cyril.lepagnot@webpulser.com
24
29
  executables: []
25
-
26
30
  extensions: []
27
-
28
31
  extra_rdoc_files: []
29
-
30
- files:
32
+ files:
31
33
  - README
32
34
  - MIT-LICENSE
33
35
  - Rakefile
34
36
  - lib/webpulser-habtm_list.rb
35
- has_rdoc: true
36
37
  homepage: http://wiki.rubyonrails.org/rails/pages/BetterHabtmList
37
38
  licenses: []
38
-
39
39
  post_install_message:
40
40
  rdoc_options: []
41
-
42
- require_paths:
41
+ require_paths:
43
42
  - lib
44
- required_ruby_version: !ruby/object:Gem::Requirement
45
- requirements:
46
- - - ">="
47
- - !ruby/object:Gem::Version
48
- segments:
49
- - 0
50
- version: "0"
51
- required_rubygems_version: !ruby/object:Gem::Requirement
52
- requirements:
53
- - - ">="
54
- - !ruby/object:Gem::Version
55
- segments:
56
- - 0
57
- version: "0"
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
58
55
  requirements: []
59
-
60
56
  rubyforge_project:
61
- rubygems_version: 1.3.6
57
+ rubygems_version: 1.8.6
62
58
  signing_key:
63
59
  specification_version: 3
64
60
  summary: has_and_belongs_to_many list-like
65
61
  test_files: []
66
-