acts_as_list 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0cf6c26261c5ef1690443023c346ddf8de5f5a95
4
- data.tar.gz: 419d7210f9a06bd54a113da0b8e8c929f716f4b6
3
+ metadata.gz: bfacc8a69876f7c5ccd4935f23c19c536f0d22a6
4
+ data.tar.gz: 2bc0fca1b08221c3a200a8f9da5bf6576583dd7a
5
5
  SHA512:
6
- metadata.gz: 07d0a625d3d0ea4344b4fea41be748b0191a0495edd5255e679c3c543b3101e70711f47595f5f6009f2490f01cf7c8e5683370254a65d654bed00ec7e6eb5265
7
- data.tar.gz: aaea3b5d86259b0564ea278a7dc936b2826b82a66d6c25e8abc33cb9124d434fb4a57de70c01751176ef285d8d36a25f8b62b9b60e18a6dcf198713fbe5cd2d6
6
+ metadata.gz: 73e20a1ce4c86cedd029f7b9b661294c64ce44befcc578be2b9ff7e48a39aaad23d2d0b83991e327da4ba640fdaa90da02ad8a5ab4f39b2220290c08144eeaa2
7
+ data.tar.gz: f62dbda7b65bd7e57a355ecb0774382acd0250d299096a688c8e647de3d20e9e8f97b3d08b73c451971da54d8fc972b59b4da9c40cbe7778cb76057a77f01a8a
@@ -1,6 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.2
4
3
  - 1.9.3
5
4
  - 2.0.0
6
5
  - 2.1.0
@@ -9,7 +8,3 @@ rvm:
9
8
  gemfile:
10
9
  - gemfiles/rails3/Gemfile
11
10
  - gemfiles/rails4/Gemfile
12
- matrix:
13
- allow_failures:
14
- - rvm: 1.9.2
15
- gemfile: gemfiles/rails4/Gemfile
@@ -43,7 +43,7 @@ module ActiveRecord
43
43
  if configuration[:scope].is_a?(Symbol)
44
44
  scope_methods = %(
45
45
  def scope_condition
46
- self.class.send(:sanitize_sql_hash_for_conditions, { :#{configuration[:scope].to_s} => send(:#{configuration[:scope].to_s}) })
46
+ { :#{configuration[:scope].to_s} => send(:#{configuration[:scope].to_s}) }
47
47
  end
48
48
 
49
49
  def scope_changed?
@@ -54,7 +54,7 @@ module ActiveRecord
54
54
  scope_methods = %(
55
55
  def attrs
56
56
  %w(#{configuration[:scope].join(" ")}).inject({}) do |memo,column|
57
- memo[column.intern] = send(column.intern); memo
57
+ memo[column.intern] = read_attribute(column.intern); memo
58
58
  end
59
59
  end
60
60
 
@@ -63,7 +63,7 @@ module ActiveRecord
63
63
  end
64
64
 
65
65
  def scope_condition
66
- self.class.send(:sanitize_sql_hash_for_conditions, attrs)
66
+ attrs
67
67
  end
68
68
  )
69
69
  else
@@ -84,7 +84,7 @@ module ActiveRecord
84
84
  end
85
85
 
86
86
  def acts_as_list_class
87
- ::#{self.name}
87
+ self.class
88
88
  end
89
89
 
90
90
  def position_column
@@ -222,7 +222,7 @@ module ActiveRecord
222
222
  def higher_item
223
223
  return nil unless in_list?
224
224
  acts_as_list_class.unscoped do
225
- acts_as_list_class.where("#{scope_condition} AND #{position_column} < #{(send(position_column).to_i).to_s}").
225
+ acts_as_list_class.where(scope_condition).where("#{position_column} < #{(send(position_column).to_i).to_s}").
226
226
  order("#{acts_as_list_class.table_name}.#{position_column} DESC").first
227
227
  end
228
228
  end
@@ -243,7 +243,7 @@ module ActiveRecord
243
243
  def lower_item
244
244
  return nil unless in_list?
245
245
  acts_as_list_class.unscoped do
246
- acts_as_list_class.where("#{scope_condition} AND #{position_column} > #{(send(position_column).to_i).to_s}").
246
+ acts_as_list_class.where(scope_condition).where("#{position_column} > #{(send(position_column).to_i).to_s}").
247
247
  order("#{acts_as_list_class.table_name}.#{position_column} ASC").first
248
248
  end
249
249
  end
@@ -304,7 +304,7 @@ module ActiveRecord
304
304
  end
305
305
 
306
306
  # Overwrite this method to define the scope of the list changes
307
- def scope_condition() "1" end
307
+ def scope_condition() {} end
308
308
 
309
309
  # Returns the bottom position number in the list.
310
310
  # bottom_position_in_list # => 2
@@ -316,9 +316,9 @@ module ActiveRecord
316
316
  # Returns the bottom item
317
317
  def bottom_item(except = nil)
318
318
  conditions = scope_condition
319
- conditions = "#{conditions} AND #{self.class.primary_key} != #{self.class.connection.quote(except.id)}" if except
319
+ conditions = except ? "#{self.class.primary_key} != #{self.class.connection.quote(except.id)}" : {}
320
320
  acts_as_list_class.unscoped do
321
- acts_as_list_class.in_list.where(conditions).order("#{acts_as_list_class.table_name}.#{position_column} DESC").first
321
+ acts_as_list_class.in_list.where(scope_condition).where(conditions).order("#{acts_as_list_class.table_name}.#{position_column} DESC").first
322
322
  end
323
323
  end
324
324
 
@@ -335,8 +335,8 @@ module ActiveRecord
335
335
  # This has the effect of moving all the higher items up one.
336
336
  def decrement_positions_on_higher_items(position)
337
337
  acts_as_list_class.unscoped do
338
- acts_as_list_class.where(
339
- "#{scope_condition} AND #{position_column} <= #{position}"
338
+ acts_as_list_class.where(scope_condition).where(
339
+ "#{position_column} <= #{position}"
340
340
  ).update_all(
341
341
  "#{position_column} = (#{position_column} - 1)"
342
342
  )
@@ -348,8 +348,8 @@ module ActiveRecord
348
348
  return unless in_list?
349
349
  position ||= send(position_column).to_i
350
350
  acts_as_list_class.unscoped do
351
- acts_as_list_class.where(
352
- "#{scope_condition} AND #{position_column} > #{position}"
351
+ acts_as_list_class.where(scope_condition).where(
352
+ "#{position_column} > #{position}"
353
353
  ).update_all(
354
354
  "#{position_column} = (#{position_column} - 1)"
355
355
  )
@@ -360,8 +360,8 @@ module ActiveRecord
360
360
  def increment_positions_on_higher_items
361
361
  return unless in_list?
362
362
  acts_as_list_class.unscoped do
363
- acts_as_list_class.where(
364
- "#{scope_condition} AND #{position_column} < #{send(position_column).to_i}"
363
+ acts_as_list_class.where(scope_condition).where(
364
+ "#{position_column} < #{send(position_column).to_i}"
365
365
  ).update_all(
366
366
  "#{position_column} = (#{position_column} + 1)"
367
367
  )
@@ -373,8 +373,8 @@ module ActiveRecord
373
373
  avoid_id_condition = avoid_id ? " AND #{self.class.primary_key} != #{self.class.connection.quote(avoid_id)}" : ''
374
374
 
375
375
  acts_as_list_class.unscoped do
376
- acts_as_list_class.where(
377
- "#{scope_condition} AND #{position_column} >= #{position}#{avoid_id_condition}"
376
+ acts_as_list_class.where(scope_condition).where(
377
+ "#{position_column} >= #{position}#{avoid_id_condition}"
378
378
  ).update_all(
379
379
  "#{position_column} = (#{position_column} + 1)"
380
380
  )
@@ -385,7 +385,7 @@ module ActiveRecord
385
385
  def increment_positions_on_all_items
386
386
  acts_as_list_class.unscoped do
387
387
  acts_as_list_class.where(
388
- "#{scope_condition}"
388
+ scope_condition
389
389
  ).update_all(
390
390
  "#{position_column} = (#{position_column} + 1)"
391
391
  )
@@ -403,8 +403,10 @@ module ActiveRecord
403
403
  # e.g., if moving an item from 2 to 5,
404
404
  # move [3, 4, 5] to [2, 3, 4]
405
405
  acts_as_list_class.unscoped do
406
- acts_as_list_class.where(
407
- "#{scope_condition} AND #{position_column} > #{old_position} AND #{position_column} <= #{new_position}#{avoid_id_condition}"
406
+ acts_as_list_class.where(scope_condition).where(
407
+ "#{position_column} > #{old_position}"
408
+ ).where(
409
+ "#{position_column} <= #{new_position}#{avoid_id_condition}"
408
410
  ).update_all(
409
411
  "#{position_column} = (#{position_column} - 1)"
410
412
  )
@@ -415,8 +417,10 @@ module ActiveRecord
415
417
  # e.g., if moving an item from 5 to 2,
416
418
  # move [2, 3, 4] to [3, 4, 5]
417
419
  acts_as_list_class.unscoped do
418
- acts_as_list_class.where(
419
- "#{scope_condition} AND #{position_column} >= #{new_position} AND #{position_column} < #{old_position}#{avoid_id_condition}"
420
+ acts_as_list_class.where(scope_condition).where(
421
+ "#{position_column} >= #{new_position}"
422
+ ).where(
423
+ "#{position_column} < #{old_position}#{avoid_id_condition}"
420
424
  ).update_all(
421
425
  "#{position_column} = (#{position_column} + 1)"
422
426
  )
@@ -450,7 +454,7 @@ module ActiveRecord
450
454
  new_position = send(position_column).to_i
451
455
 
452
456
  return unless acts_as_list_class.unscoped do
453
- acts_as_list_class.where("#{scope_condition} AND #{position_column} = #{new_position}").count > 1
457
+ acts_as_list_class.where(scope_condition).where("#{position_column} = #{new_position}").count > 1
454
458
  end
455
459
  shuffle_positions_on_intermediate_items old_position, new_position, id
456
460
  end
@@ -1,7 +1,7 @@
1
1
  module ActiveRecord
2
2
  module Acts
3
3
  module List
4
- VERSION = '0.5.0'
4
+ VERSION = '0.6.0'
5
5
  end
6
6
  end
7
7
  end
@@ -45,7 +45,7 @@ module Shared
45
45
 
46
46
  def test_injection
47
47
  item = ListMixin.new(parent_id: 1)
48
- assert_equal '"mixins"."parent_id" = 1', item.scope_condition
48
+ assert_equal({ parent_id: 1 }, item.scope_condition)
49
49
  assert_equal "pos", item.position_column
50
50
  end
51
51
 
@@ -61,7 +61,7 @@ module Shared
61
61
 
62
62
  def test_injection
63
63
  item = ListMixin.new("parent_id"=>1)
64
- assert_equal '"mixins"."parent_id" = 1', item.scope_condition
64
+ assert_equal({ parent_id: 1 }, item.scope_condition)
65
65
  assert_equal "pos", item.position_column
66
66
  end
67
67
 
@@ -118,5 +118,10 @@ module Shared
118
118
  assert_equal 1, ListMixin.where(id: 3).first.pos
119
119
  assert_equal 2, ListMixin.where(id: 4).first.pos
120
120
  end
121
+
122
+ def test_acts_as_list_class
123
+ assert_equal TheSubClass1, TheSubClass1.new.acts_as_list_class
124
+ assert_equal TheSubClass2, TheSubClass2.new.acts_as_list_class
125
+ end
121
126
  end
122
127
  end
@@ -28,7 +28,7 @@ module Shared
28
28
 
29
29
  def test_injection
30
30
  item = TopAdditionMixin.new(parent_id: 1)
31
- assert_equal '"mixins"."parent_id" = 1', item.scope_condition
31
+ assert_equal({ parent_id: 1 }, item.scope_condition)
32
32
  assert_equal "pos", item.position_column
33
33
  end
34
34
 
@@ -13,13 +13,17 @@ def setup_db(position_options = {})
13
13
  t.column :parent_type, :string
14
14
  t.column :created_at, :datetime
15
15
  t.column :updated_at, :datetime
16
+ t.column :state, :integer
16
17
  end
17
-
18
- ActiveRecord::Base.connection.schema_cache.clear!
19
- [ Mixin, ListMixin, ListMixinSub1, ListMixinSub2, ListWithStringScopeMixin,
18
+
19
+ mixins = [ Mixin, ListMixin, ListMixinSub1, ListMixinSub2, ListWithStringScopeMixin,
20
20
  ArrayScopeListMixin, ZeroBasedMixin, DefaultScopedMixin,
21
- DefaultScopedWhereMixin, TopAdditionMixin, NoAdditionMixin ].each do |klass|
22
-
21
+ DefaultScopedWhereMixin, TopAdditionMixin, NoAdditionMixin ]
22
+
23
+ mixins << EnumArrayScopeListMixin if rails_4
24
+
25
+ ActiveRecord::Base.connection.schema_cache.clear!
26
+ mixins.each do |klass|
23
27
  klass.reset_column_information
24
28
  end
25
29
  end
@@ -33,6 +37,10 @@ def rails_3
33
37
  defined?(ActiveRecord::VERSION) && ActiveRecord::VERSION::MAJOR >= 3
34
38
  end
35
39
 
40
+ def rails_4
41
+ defined?(ActiveRecord::VERSION) && ActiveRecord::VERSION::MAJOR >= 4
42
+ end
43
+
36
44
  def teardown_db
37
45
  ActiveRecord::Base.connection.tables.each do |table|
38
46
  ActiveRecord::Base.connection.drop_table(table)
@@ -66,6 +74,15 @@ class ArrayScopeListMixin < Mixin
66
74
  acts_as_list column: "pos", scope: [:parent_id, :parent_type]
67
75
  end
68
76
 
77
+ if rails_4
78
+ class EnumArrayScopeListMixin < Mixin
79
+ STATE_VALUES = %w(active archived)
80
+ enum state: STATE_VALUES
81
+
82
+ acts_as_list column: "pos", scope: [:parent_id, :state]
83
+ end
84
+ end
85
+
69
86
  class ZeroBasedMixin < Mixin
70
87
  acts_as_list column: "pos", top_of_list: 0, scope: [:parent_id]
71
88
  end
@@ -92,6 +109,19 @@ class NoAdditionMixin < Mixin
92
109
  acts_as_list column: "pos", add_new_at: nil, scope: :parent_id
93
110
  end
94
111
 
112
+ class TheBaseClass < ActiveRecord::Base
113
+ self.abstract_class = true
114
+ acts_as_list column: "pos", scope: :parent
115
+ end
116
+
117
+ class TheSubClass1 < TheBaseClass
118
+ self.table_name = 'mixins'
119
+ end
120
+
121
+ class TheSubClass2 < TheBaseClass
122
+ self.table_name = 'mixins'
123
+ end
124
+
95
125
  class ActsAsListTestCase < Minitest::Test
96
126
  # No default test required as this class is abstract.
97
127
  # Need for test/unit.
@@ -459,6 +489,25 @@ class MultipleListsTest < ActsAsListTestCase
459
489
  end
460
490
  end
461
491
 
492
+ if rails_4
493
+ class EnumArrayScopeListMixinTest < ActsAsListTestCase
494
+ def setup
495
+ setup_db
496
+ EnumArrayScopeListMixin.create! :parent_id => 1, :state => EnumArrayScopeListMixin.states['active']
497
+ EnumArrayScopeListMixin.create! :parent_id => 1, :state => EnumArrayScopeListMixin.states['archived']
498
+ EnumArrayScopeListMixin.create! :parent_id => 2, :state => EnumArrayScopeListMixin.states["active"]
499
+ EnumArrayScopeListMixin.create! :parent_id => 2, :state => EnumArrayScopeListMixin.states["archived"]
500
+ end
501
+
502
+ def test_positions
503
+ assert_equal [1], EnumArrayScopeListMixin.where(:parent_id => 1, :state => EnumArrayScopeListMixin.states['active']).map(&:pos)
504
+ assert_equal [1], EnumArrayScopeListMixin.where(:parent_id => 1, :state => EnumArrayScopeListMixin.states['archived']).map(&:pos)
505
+ assert_equal [1], EnumArrayScopeListMixin.where(:parent_id => 2, :state => EnumArrayScopeListMixin.states['active']).map(&:pos)
506
+ assert_equal [1], EnumArrayScopeListMixin.where(:parent_id => 2, :state => EnumArrayScopeListMixin.states['archived']).map(&:pos)
507
+ end
508
+ end
509
+ end
510
+
462
511
  class MultipleListsArrayScopeTest < ActsAsListTestCase
463
512
  def setup
464
513
  setup_db
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.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
@@ -10,34 +10,34 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-10-31 00:00:00.000000000 Z
13
+ date: 2014-12-24 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
17
17
  requirement: !ruby/object:Gem::Requirement
18
18
  requirements:
19
- - - '>='
19
+ - - ">="
20
20
  - !ruby/object:Gem::Version
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
- - - '>='
26
+ - - ">="
27
27
  - !ruby/object:Gem::Version
28
28
  version: '3.0'
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: bundler
31
31
  requirement: !ruby/object:Gem::Requirement
32
32
  requirements:
33
- - - '>='
33
+ - - ">="
34
34
  - !ruby/object:Gem::Version
35
35
  version: 1.0.0
36
36
  type: :development
37
37
  prerelease: false
38
38
  version_requirements: !ruby/object:Gem::Requirement
39
39
  requirements:
40
- - - '>='
40
+ - - ">="
41
41
  - !ruby/object:Gem::Version
42
42
  version: 1.0.0
43
43
  description: This "acts_as" extension provides the capabilities for sorting and reordering
@@ -49,9 +49,9 @@ executables: []
49
49
  extensions: []
50
50
  extra_rdoc_files: []
51
51
  files:
52
- - .gemtest
53
- - .gitignore
54
- - .travis.yml
52
+ - ".gemtest"
53
+ - ".gitignore"
54
+ - ".travis.yml"
55
55
  - Gemfile
56
56
  - MIT-LICENSE
57
57
  - README.md
@@ -82,17 +82,17 @@ require_paths:
82
82
  - lib
83
83
  required_ruby_version: !ruby/object:Gem::Requirement
84
84
  requirements:
85
- - - '>='
85
+ - - ">="
86
86
  - !ruby/object:Gem::Version
87
87
  version: 1.9.2
88
88
  required_rubygems_version: !ruby/object:Gem::Requirement
89
89
  requirements:
90
- - - '>='
90
+ - - ">="
91
91
  - !ruby/object:Gem::Version
92
92
  version: '0'
93
93
  requirements: []
94
94
  rubyforge_project: acts_as_list
95
- rubygems_version: 2.0.14
95
+ rubygems_version: 2.2.2
96
96
  signing_key:
97
97
  specification_version: 4
98
98
  summary: A gem allowing a active_record model to act_as_list.