sortifiable 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +8 -0
- data/README.md +1 -0
- data/Rakefile +31 -1
- data/lib/sortifiable.rb +7 -2
- data/lib/sortifiable/version.rb +1 -1
- data/sortifiable.gemspec +4 -1
- data/test/sortifiable_test.rb +10 -115
- metadata +56 -8
data/CHANGELOG
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
*0.2.4 (June 18th, 2011)
|
2
|
+
|
3
|
+
* Typecast the list of primary keys return by lock_list! correctly as
|
4
|
+
the postgresql and mysql adapters return strings by default. [Reinier de Lange]
|
5
|
+
|
6
|
+
* Check for existence of foreign key attribute with symbol scope option
|
7
|
+
|
8
|
+
|
1
9
|
*0.2.3 (May 3rd, 2011)
|
2
10
|
|
3
11
|
* Rails 3.1 has added Model.foreign_{key,type} to association reflections
|
data/README.md
CHANGED
data/Rakefile
CHANGED
@@ -10,7 +10,7 @@ task :default => :test
|
|
10
10
|
|
11
11
|
desc 'Test the sortifiable gem.'
|
12
12
|
Rake::TestTask.new(:test) do |t|
|
13
|
-
t.libs
|
13
|
+
t.libs += %w[lib test]
|
14
14
|
t.pattern = 'test/**/*_test.rb'
|
15
15
|
t.verbose = true
|
16
16
|
end
|
@@ -22,3 +22,33 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
22
22
|
rdoc.options << '--line-numbers' << '--inline-source'
|
23
23
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
24
24
|
end
|
25
|
+
|
26
|
+
namespace :test do
|
27
|
+
desc 'Test using the mysql connection adapter'
|
28
|
+
task :mysql do
|
29
|
+
sh 'rake test DB=mysql'
|
30
|
+
end
|
31
|
+
|
32
|
+
desc 'Test using the mysql2 connection adapter'
|
33
|
+
task :mysql2 do
|
34
|
+
sh 'rake test DB=mysql2'
|
35
|
+
end
|
36
|
+
|
37
|
+
desc 'Test using the postgresql connection adapter'
|
38
|
+
task :postgresql do
|
39
|
+
sh 'rake test DB=postgresql'
|
40
|
+
end
|
41
|
+
|
42
|
+
desc 'Test using the sqlite3 connection adapter'
|
43
|
+
task :sqlite3 do
|
44
|
+
sh 'rake test DB=sqlite3'
|
45
|
+
end
|
46
|
+
|
47
|
+
desc 'Test all connection adapters'
|
48
|
+
task :all do
|
49
|
+
sh 'rake test DB=mysql'
|
50
|
+
sh 'rake test DB=mysql2'
|
51
|
+
sh 'rake test DB=postgresql'
|
52
|
+
sh 'rake test DB=sqlite3'
|
53
|
+
end
|
54
|
+
end
|
data/lib/sortifiable.rb
CHANGED
@@ -58,7 +58,8 @@ module Sortifiable
|
|
58
58
|
raise ArgumentError, "Only belongs_to associations can be used as a scope"
|
59
59
|
end
|
60
60
|
elsif options[:scope].to_s !~ /_id$/
|
61
|
-
|
61
|
+
scope_name = "#{options[:scope]}_id"
|
62
|
+
options[:scope] = scope_name.to_sym if column_names.include?(scope_name)
|
62
63
|
end
|
63
64
|
end
|
64
65
|
|
@@ -438,7 +439,11 @@ module Sortifiable
|
|
438
439
|
end
|
439
440
|
|
440
441
|
def lock_list! #:nodoc:
|
441
|
-
|
442
|
+
sql = list_scope.select(list_class.primary_key).lock(true).to_sql
|
443
|
+
column = list_class.columns_hash[list_class.primary_key]
|
444
|
+
connection.select_values(sql).map do |value|
|
445
|
+
column.type_cast(value)
|
446
|
+
end
|
442
447
|
end
|
443
448
|
|
444
449
|
def lower_scope(position) #:nodoc:
|
data/lib/sortifiable/version.rb
CHANGED
data/sortifiable.gemspec
CHANGED
@@ -38,5 +38,8 @@ EOF
|
|
38
38
|
s.add_dependency "activesupport", ">= 3.0"
|
39
39
|
s.add_dependency "activerecord", ">= 3.0"
|
40
40
|
s.add_development_dependency "bundler", ">= 1.0.10"
|
41
|
-
s.add_development_dependency "
|
41
|
+
s.add_development_dependency "mysql", "~> 2.8.1"
|
42
|
+
s.add_development_dependency "mysql2", "~> 0.2.7"
|
43
|
+
s.add_development_dependency "pg", "~> 0.9.0"
|
44
|
+
s.add_development_dependency "sqlite3", "~> 1.3.3"
|
42
45
|
end
|
data/test/sortifiable_test.rb
CHANGED
@@ -1,86 +1,6 @@
|
|
1
|
-
require '
|
2
|
-
require 'rubygems'
|
3
|
-
require 'active_record'
|
4
|
-
require 'active_support/core_ext/kernel/reporting'
|
5
|
-
require 'sortifiable'
|
6
|
-
|
7
|
-
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
8
|
-
|
9
|
-
def setup_db
|
10
|
-
silence_stream(STDOUT) do
|
11
|
-
ActiveRecord::Schema.define(:version => 1) do
|
12
|
-
create_table :mixins do |t|
|
13
|
-
t.column :type, :string
|
14
|
-
t.column :pos, :integer
|
15
|
-
t.column :parent_id, :integer
|
16
|
-
t.column :parent_type, :string
|
17
|
-
t.column :created_at, :datetime
|
18
|
-
t.column :updated_at, :datetime
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
1
|
+
require 'test_helper'
|
23
2
|
|
24
|
-
|
25
|
-
ActiveRecord::Base.connection.tables.each do |table|
|
26
|
-
ActiveRecord::Base.connection.drop_table(table)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
setup_db
|
31
|
-
|
32
|
-
class Mixin < ActiveRecord::Base
|
33
|
-
end
|
34
|
-
|
35
|
-
class ListMixin < ActiveRecord::Base
|
36
|
-
acts_as_list :column => "pos", :scope => :parent
|
37
|
-
set_table_name "mixins"
|
38
|
-
default_scope order(:pos)
|
39
|
-
end
|
40
|
-
|
41
|
-
class ListMixinSub1 < ListMixin
|
42
|
-
end
|
43
|
-
|
44
|
-
class ListMixinSub2 < ListMixin
|
45
|
-
end
|
46
|
-
|
47
|
-
class ListWithStringScopeMixin < ActiveRecord::Base
|
48
|
-
acts_as_list :column => "pos", :scope => 'parent_id = #{parent_id}'
|
49
|
-
set_table_name "mixins"
|
50
|
-
default_scope order(:pos)
|
51
|
-
end
|
52
|
-
|
53
|
-
class ArrayScopeListMixin < ActiveRecord::Base
|
54
|
-
acts_as_list :column => "pos", :scope => [:parent_id, :parent_type]
|
55
|
-
set_table_name "mixins"
|
56
|
-
default_scope order(:pos)
|
57
|
-
end
|
58
|
-
|
59
|
-
class AssociationScopeListMixin < ActiveRecord::Base
|
60
|
-
belongs_to :parent
|
61
|
-
acts_as_list :column => "pos", :scope => :parent
|
62
|
-
set_table_name "mixins"
|
63
|
-
default_scope order(:pos)
|
64
|
-
end
|
65
|
-
|
66
|
-
class PolymorphicAssociationScopeListMixin < ActiveRecord::Base
|
67
|
-
belongs_to :parent, :polymorphic => true
|
68
|
-
acts_as_list :column => "pos", :scope => :parent
|
69
|
-
set_table_name "mixins"
|
70
|
-
default_scope order(:pos)
|
71
|
-
end
|
72
|
-
|
73
|
-
teardown_db
|
74
|
-
|
75
|
-
class NonListTest < Test::Unit::TestCase
|
76
|
-
|
77
|
-
def setup
|
78
|
-
setup_db
|
79
|
-
end
|
80
|
-
|
81
|
-
def teardown
|
82
|
-
teardown_db
|
83
|
-
end
|
3
|
+
class NonListTest < ActiveSupport::TestCase
|
84
4
|
|
85
5
|
def test_callbacks_are_not_added_to_all_models
|
86
6
|
Mixin.create! :pos => 1, :parent_id => 5
|
@@ -97,10 +17,9 @@ class NonListTest < Test::Unit::TestCase
|
|
97
17
|
|
98
18
|
end
|
99
19
|
|
100
|
-
class ListTest <
|
20
|
+
class ListTest < ActiveSupport::TestCase
|
101
21
|
|
102
22
|
def setup
|
103
|
-
setup_db
|
104
23
|
[5, 6].each do |parent_id|
|
105
24
|
(1..4).each do |i|
|
106
25
|
ListMixin.create! :pos => i, :parent_id => parent_id
|
@@ -108,10 +27,6 @@ class ListTest < Test::Unit::TestCase
|
|
108
27
|
end
|
109
28
|
end
|
110
29
|
|
111
|
-
def teardown
|
112
|
-
teardown_db
|
113
|
-
end
|
114
|
-
|
115
30
|
def test_reordering
|
116
31
|
assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 5).map(&:id)
|
117
32
|
|
@@ -257,7 +172,7 @@ class ListTest < Test::Unit::TestCase
|
|
257
172
|
|
258
173
|
ListMixin.find(2).remove_from_list
|
259
174
|
|
260
|
-
assert_equal [
|
175
|
+
assert_equal [1, 3, 4], ListMixin.where(:parent_id => 5).where('pos IS NOT NULL').map(&:id)
|
261
176
|
|
262
177
|
assert_equal 1, ListMixin.find(1).pos
|
263
178
|
assert_equal nil, ListMixin.find(2).pos
|
@@ -378,10 +293,9 @@ class ListTest < Test::Unit::TestCase
|
|
378
293
|
|
379
294
|
end
|
380
295
|
|
381
|
-
class ListWithStringScopeTest <
|
296
|
+
class ListWithStringScopeTest < ActiveSupport::TestCase
|
382
297
|
|
383
298
|
def setup
|
384
|
-
setup_db
|
385
299
|
[5, 6].each do |parent_id|
|
386
300
|
(1..4).each do |i|
|
387
301
|
ListWithStringScopeMixin.create! :parent_id => parent_id
|
@@ -389,10 +303,6 @@ class ListWithStringScopeTest < Test::Unit::TestCase
|
|
389
303
|
end
|
390
304
|
end
|
391
305
|
|
392
|
-
def teardown
|
393
|
-
teardown_db
|
394
|
-
end
|
395
|
-
|
396
306
|
def test_insert
|
397
307
|
new = ListWithStringScopeMixin.create(:parent_id => 500)
|
398
308
|
assert_equal 1, new.pos
|
@@ -444,20 +354,15 @@ class ListWithStringScopeTest < Test::Unit::TestCase
|
|
444
354
|
|
445
355
|
end
|
446
356
|
|
447
|
-
class ListSubTest <
|
357
|
+
class ListSubTest < ActiveSupport::TestCase
|
448
358
|
|
449
359
|
def setup
|
450
|
-
setup_db
|
451
360
|
(1..4).each do |i|
|
452
361
|
klass = ((i % 2 == 1) ? ListMixinSub1 : ListMixinSub2)
|
453
362
|
klass.create! :pos => i, :parent_id => 5000
|
454
363
|
end
|
455
364
|
end
|
456
365
|
|
457
|
-
def teardown
|
458
|
-
teardown_db
|
459
|
-
end
|
460
|
-
|
461
366
|
def test_sti_class
|
462
367
|
assert_instance_of ListMixinSub1, ListMixin.find(1)
|
463
368
|
assert_instance_of ListMixinSub2, ListMixin.find(2)
|
@@ -588,10 +493,9 @@ class ListSubTest < Test::Unit::TestCase
|
|
588
493
|
|
589
494
|
end
|
590
495
|
|
591
|
-
class ArrayScopeListTest <
|
496
|
+
class ArrayScopeListTest < ActiveSupport::TestCase
|
592
497
|
|
593
498
|
def setup
|
594
|
-
setup_db
|
595
499
|
['ParentClass', 'bananas'].each do |parent_type|
|
596
500
|
[5, 6].each do |parent_id|
|
597
501
|
(1..4).each do |i|
|
@@ -605,10 +509,6 @@ class ArrayScopeListTest < Test::Unit::TestCase
|
|
605
509
|
end
|
606
510
|
end
|
607
511
|
|
608
|
-
def teardown
|
609
|
-
teardown_db
|
610
|
-
end
|
611
|
-
|
612
512
|
def conditions(options = {})
|
613
513
|
{ :parent_id => 5, :parent_type => 'ParentClass' }.merge(options)
|
614
514
|
end
|
@@ -807,7 +707,7 @@ class ArrayScopeListTest < Test::Unit::TestCase
|
|
807
707
|
end
|
808
708
|
|
809
709
|
def test_move_to_new_list_by_updating_complete_scope_should_append
|
810
|
-
assert_equal [
|
710
|
+
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(conditions).map(&:id)
|
811
711
|
assert_equal [13, 14, 15, 16], ArrayScopeListMixin.where(:parent_id => 6, :parent_type => 'bananas').map(&:id)
|
812
712
|
|
813
713
|
ArrayScopeListMixin.find(2).update_attributes! :parent_id => 6, :parent_type => 'bananas'
|
@@ -834,7 +734,7 @@ class ArrayScopeListTest < Test::Unit::TestCase
|
|
834
734
|
|
835
735
|
ArrayScopeListMixin.find(2).remove_from_list
|
836
736
|
|
837
|
-
assert_equal [
|
737
|
+
assert_equal [1, 3, 4], ArrayScopeListMixin.where(conditions).where('pos IS NOT NULL').map(&:id)
|
838
738
|
|
839
739
|
assert_equal 1, ArrayScopeListMixin.find(1).pos
|
840
740
|
assert_equal nil, ArrayScopeListMixin.find(2).pos
|
@@ -867,10 +767,9 @@ class ArrayScopeListTest < Test::Unit::TestCase
|
|
867
767
|
|
868
768
|
end
|
869
769
|
|
870
|
-
class AssociationScopeListTest <
|
770
|
+
class AssociationScopeListTest < ActiveSupport::TestCase
|
871
771
|
|
872
772
|
def setup
|
873
|
-
setup_db
|
874
773
|
(1..4).each do |i|
|
875
774
|
AssociationScopeListMixin.create!(
|
876
775
|
:pos => i,
|
@@ -887,10 +786,6 @@ class AssociationScopeListTest < Test::Unit::TestCase
|
|
887
786
|
end
|
888
787
|
end
|
889
788
|
|
890
|
-
def teardown
|
891
|
-
teardown_db
|
892
|
-
end
|
893
|
-
|
894
789
|
def test_association_scope_is_configured
|
895
790
|
assert_equal :parent_id,
|
896
791
|
AssociationScopeListMixin.acts_as_list_options[:scope]
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sortifiable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 4
|
10
|
+
version: 0.2.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Andrew White
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-06-18 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -65,12 +65,60 @@ dependencies:
|
|
65
65
|
type: :development
|
66
66
|
version_requirements: *id003
|
67
67
|
- !ruby/object:Gem::Dependency
|
68
|
-
name:
|
68
|
+
name: mysql
|
69
69
|
prerelease: false
|
70
70
|
requirement: &id004 !ruby/object:Gem::Requirement
|
71
71
|
none: false
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 45
|
76
|
+
segments:
|
77
|
+
- 2
|
78
|
+
- 8
|
79
|
+
- 1
|
80
|
+
version: 2.8.1
|
81
|
+
type: :development
|
82
|
+
version_requirements: *id004
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: mysql2
|
85
|
+
prerelease: false
|
86
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ~>
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
hash: 25
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
- 2
|
95
|
+
- 7
|
96
|
+
version: 0.2.7
|
97
|
+
type: :development
|
98
|
+
version_requirements: *id005
|
99
|
+
- !ruby/object:Gem::Dependency
|
100
|
+
name: pg
|
101
|
+
prerelease: false
|
102
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ~>
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
hash: 59
|
108
|
+
segments:
|
109
|
+
- 0
|
110
|
+
- 9
|
111
|
+
- 0
|
112
|
+
version: 0.9.0
|
113
|
+
type: :development
|
114
|
+
version_requirements: *id006
|
115
|
+
- !ruby/object:Gem::Dependency
|
116
|
+
name: sqlite3
|
117
|
+
prerelease: false
|
118
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
119
|
+
none: false
|
120
|
+
requirements:
|
121
|
+
- - ~>
|
74
122
|
- !ruby/object:Gem::Version
|
75
123
|
hash: 29
|
76
124
|
segments:
|
@@ -79,7 +127,7 @@ dependencies:
|
|
79
127
|
- 3
|
80
128
|
version: 1.3.3
|
81
129
|
type: :development
|
82
|
-
version_requirements: *
|
130
|
+
version_requirements: *id007
|
83
131
|
description: |
|
84
132
|
This gem provides an acts_as_list compatible capability for sorting
|
85
133
|
and reordering a number of objects in a list. The class that has this
|
@@ -137,7 +185,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
137
185
|
requirements: []
|
138
186
|
|
139
187
|
rubyforge_project:
|
140
|
-
rubygems_version: 1.
|
188
|
+
rubygems_version: 1.5.2
|
141
189
|
signing_key:
|
142
190
|
specification_version: 3
|
143
191
|
summary: Sort your models
|