active_sort_order 0.9.0 → 0.9.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +18 -5
- data/README.md +91 -40
- data/Rakefile +5 -1
- data/lib/active_sort_order/concerns/sort_order_concern.rb +44 -23
- data/lib/active_sort_order/version.rb +1 -1
- data/test/dummy_app/config/application.rb +0 -5
- data/test/dummy_app/config/database.yml +12 -3
- data/test/dummy_app/config/environments/test.rb +1 -1
- data/test/dummy_app/db/migrate/20210128155312_set_up_test_tables.rb +7 -1
- data/test/dummy_app/db/test.sqlite3 +0 -0
- data/test/dummy_app/log/test.log +558 -9902
- data/test/test_helper.rb +16 -6
- data/test/unit/active_sort_order_test.rb +31 -3
- data/test/unit/errors_test.rb +56 -22
- metadata +27 -41
- data/test/dummy_app/app/models/test_helper.rb +0 -0
data/test/test_helper.rb
CHANGED
@@ -3,6 +3,16 @@ ENV["RAILS_ENV"] = "test"
|
|
3
3
|
|
4
4
|
require "active_sort_order"
|
5
5
|
|
6
|
+
begin
|
7
|
+
require 'warning'
|
8
|
+
|
9
|
+
Warning.ignore(
|
10
|
+
%r{mail/parsers/address_lists_parser}, ### Hide mail gem warnings
|
11
|
+
)
|
12
|
+
rescue LoadError
|
13
|
+
# Do nothing
|
14
|
+
end
|
15
|
+
|
6
16
|
### Instantiates Rails
|
7
17
|
require File.expand_path("../dummy_app/config/environment.rb", __FILE__)
|
8
18
|
|
@@ -34,12 +44,12 @@ else
|
|
34
44
|
end
|
35
45
|
|
36
46
|
[Post].each do |klass|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
47
|
+
if defined?(SQLite3)
|
48
|
+
ActiveRecord::Base.connection.execute("DELETE FROM #{klass.table_name};")
|
49
|
+
ActiveRecord::Base.connection.execute("UPDATE `sqlite_sequence` SET `seq` = 0 WHERE `name` = '#{klass.table_name}';")
|
50
|
+
else
|
51
|
+
ActiveRecord::Base.connection.execute("TRUNCATE TABLE #{klass.table_name}")
|
52
|
+
end
|
43
53
|
end
|
44
54
|
|
45
55
|
DATA = {}.with_indifferent_access
|
@@ -2,10 +2,10 @@ require "test_helper"
|
|
2
2
|
|
3
3
|
class ActiveSortOrderTest < ActiveSupport::TestCase
|
4
4
|
|
5
|
-
|
5
|
+
setup do
|
6
6
|
end
|
7
7
|
|
8
|
-
|
8
|
+
teardown do
|
9
9
|
end
|
10
10
|
|
11
11
|
def test_exposes_main_module
|
@@ -16,6 +16,14 @@ class ActiveSortOrderTest < ActiveSupport::TestCase
|
|
16
16
|
assert ActiveSortOrder::VERSION
|
17
17
|
end
|
18
18
|
|
19
|
+
def test_base_sort_order_default_value
|
20
|
+
klass = PostWithBaseOrderA
|
21
|
+
|
22
|
+
assert PostWithBaseOrderA.unscoped.sort_order.to_sql.include?("ORDER BY #{klass.base_sort_order}")
|
23
|
+
|
24
|
+
assert PostWithBaseOrderA.unscoped.sort_order(base_sort_order: true).to_sql.include?("ORDER BY #{klass.base_sort_order}")
|
25
|
+
end
|
26
|
+
|
19
27
|
def test_class_base_sort_order_only
|
20
28
|
assert_equal Post.all.count, DATA[:posts].count
|
21
29
|
|
@@ -67,7 +75,7 @@ class ActiveSortOrderTest < ActiveSupport::TestCase
|
|
67
75
|
|
68
76
|
### NIL & FALSE
|
69
77
|
[nil, false].each do |v|
|
70
|
-
sorted = PostWithBaseOrderA.order(
|
78
|
+
sorted = PostWithBaseOrderA.order(id: :asc).sort_order(base_sort_order: v)
|
71
79
|
|
72
80
|
sorted.each_with_index do |item, i|
|
73
81
|
assert_equal expected[i].id, item.id
|
@@ -113,4 +121,24 @@ class ActiveSortOrderTest < ActiveSupport::TestCase
|
|
113
121
|
end
|
114
122
|
end
|
115
123
|
|
124
|
+
def test_sort_on_multiple_fields
|
125
|
+
assert_equal Post.all.count, DATA[:posts].count
|
126
|
+
|
127
|
+
expected = DATA[:posts].sort_by{|item| [item.b, item.a] }
|
128
|
+
|
129
|
+
sorted = PostWithBaseOrderA.all.sort_order([:b, :a], :asc)
|
130
|
+
|
131
|
+
sorted.each_with_index do |item, i|
|
132
|
+
assert_equal expected[i].id, item.id
|
133
|
+
end
|
134
|
+
|
135
|
+
expected = DATA[:posts].sort_by{|item| [item.b, item.a] }.reverse
|
136
|
+
|
137
|
+
sorted = PostWithBaseOrderA.all.sort_order([:b, :a], :desc)
|
138
|
+
|
139
|
+
sorted.each_with_index do |item, i|
|
140
|
+
assert_equal expected[i].id, item.id
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
116
144
|
end
|
data/test/unit/errors_test.rb
CHANGED
@@ -2,10 +2,10 @@ require "test_helper"
|
|
2
2
|
|
3
3
|
class ActiveSortOrderTest < ActiveSupport::TestCase
|
4
4
|
|
5
|
-
|
5
|
+
setup do
|
6
6
|
end
|
7
7
|
|
8
|
-
|
8
|
+
teardown do
|
9
9
|
end
|
10
10
|
|
11
11
|
def test_sort_str_errors
|
@@ -15,6 +15,8 @@ class ActiveSortOrderTest < ActiveSupport::TestCase
|
|
15
15
|
:foo,
|
16
16
|
nil,
|
17
17
|
"",
|
18
|
+
[],
|
19
|
+
[:foo],
|
18
20
|
]
|
19
21
|
|
20
22
|
valid.each do |v|
|
@@ -25,10 +27,13 @@ class ActiveSortOrderTest < ActiveSupport::TestCase
|
|
25
27
|
invalid = [
|
26
28
|
true,
|
27
29
|
false,
|
28
|
-
[],
|
29
30
|
Object.new,
|
30
31
|
]
|
31
32
|
|
33
|
+
if RUBY_VERSION.to_f >= 3.0
|
34
|
+
invalid << {}
|
35
|
+
end
|
36
|
+
|
32
37
|
invalid.each do |v|
|
33
38
|
assert_raise ArgumentError do
|
34
39
|
Post.sort_order(v, :asc).limit(1)
|
@@ -41,16 +46,18 @@ class ActiveSortOrderTest < ActiveSupport::TestCase
|
|
41
46
|
|
42
47
|
### TEST UNIQUE CASES
|
43
48
|
|
44
|
-
|
45
|
-
|
49
|
+
if RUBY_VERSION.to_f < 3.0
|
50
|
+
### HASH - this is allowed because its treated as keyword arguments
|
51
|
+
Post.sort_order({}).limit(1)
|
46
52
|
|
47
|
-
|
48
|
-
|
53
|
+
assert_raise do
|
54
|
+
Post.sort_order({}, :desc).limit(1)
|
55
|
+
end
|
49
56
|
end
|
50
57
|
end
|
51
58
|
|
52
59
|
def test_sort_direction_errors
|
53
|
-
|
60
|
+
valid = [
|
54
61
|
"ASC",
|
55
62
|
"DESC",
|
56
63
|
"ASC NULLS FIRST",
|
@@ -67,7 +74,7 @@ class ActiveSortOrderTest < ActiveSupport::TestCase
|
|
67
74
|
"ASC\tNULLS\tFirst",
|
68
75
|
].freeze
|
69
76
|
|
70
|
-
|
77
|
+
valid.each do |direction|
|
71
78
|
PostWithBaseOrderA.sort_order("x", direction).limit(1)
|
72
79
|
|
73
80
|
if direction
|
@@ -81,15 +88,19 @@ class ActiveSortOrderTest < ActiveSupport::TestCase
|
|
81
88
|
end
|
82
89
|
end
|
83
90
|
|
84
|
-
|
91
|
+
invalid = [
|
85
92
|
false,
|
86
93
|
true,
|
87
94
|
Object.new,
|
88
95
|
[],
|
89
96
|
'ASCC',
|
90
|
-
]
|
97
|
+
]
|
98
|
+
|
99
|
+
if RUBY_VERSION.to_f >= 3.0
|
100
|
+
invalid << {}
|
101
|
+
end
|
91
102
|
|
92
|
-
|
103
|
+
invalid.each do |direction|
|
93
104
|
assert_raise ArgumentError do
|
94
105
|
PostWithBaseOrderA.sort_order("foobar", direction).limit(1)
|
95
106
|
end
|
@@ -97,17 +108,22 @@ class ActiveSortOrderTest < ActiveSupport::TestCase
|
|
97
108
|
|
98
109
|
### TEST UNIQUE CASES
|
99
110
|
|
100
|
-
|
101
|
-
|
111
|
+
if RUBY_VERSION.to_f < 3.0
|
112
|
+
### HASH - this is allowed because its treated as keyword arguments
|
113
|
+
Post.sort_order("foobar", {}).limit(1).to_sql.include?("foobar ASC")
|
102
114
|
|
103
|
-
|
104
|
-
|
115
|
+
assert_raise do
|
116
|
+
Post.sort_order("foobar", {}, {}).limit(1)
|
117
|
+
end
|
105
118
|
end
|
106
119
|
end
|
107
120
|
|
108
|
-
def
|
121
|
+
def test_argument_base_sort_order_errors
|
122
|
+
assert_not Post.respond_to?(:base_sort_order)
|
123
|
+
|
109
124
|
valid = [
|
110
125
|
nil,
|
126
|
+
true,
|
111
127
|
false,
|
112
128
|
"",
|
113
129
|
"foobar",
|
@@ -129,8 +145,13 @@ class ActiveSortOrderTest < ActiveSupport::TestCase
|
|
129
145
|
Post.sort_order(base_sort_order: v).limit(1)
|
130
146
|
end
|
131
147
|
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_class_method_base_sort_order_errors
|
151
|
+
klass = PostWithVolatileBaseOrder
|
152
|
+
|
153
|
+
assert klass.respond_to?(:base_sort_order)
|
132
154
|
|
133
|
-
### INVALID BASE_SORT_ORDER CLASS
|
134
155
|
valid = [
|
135
156
|
nil,
|
136
157
|
false,
|
@@ -140,15 +161,22 @@ class ActiveSortOrderTest < ActiveSupport::TestCase
|
|
140
161
|
|
141
162
|
valid.each do |v|
|
142
163
|
silence_warnings do
|
143
|
-
|
164
|
+
klass.define_singleton_method :base_sort_order do
|
144
165
|
v
|
145
166
|
end
|
146
167
|
end
|
147
168
|
|
148
|
-
|
169
|
+
if v.nil?
|
170
|
+
assert_nil klass.base_sort_order
|
171
|
+
else
|
172
|
+
assert_equal v, klass.base_sort_order
|
173
|
+
end
|
174
|
+
|
175
|
+
klass.sort_order.limit(1)
|
149
176
|
end
|
150
177
|
|
151
178
|
invalid = [
|
179
|
+
true,
|
152
180
|
:foobar,
|
153
181
|
[],
|
154
182
|
{},
|
@@ -157,13 +185,19 @@ class ActiveSortOrderTest < ActiveSupport::TestCase
|
|
157
185
|
|
158
186
|
invalid.each do |v|
|
159
187
|
silence_warnings do
|
160
|
-
|
188
|
+
klass.define_singleton_method :base_sort_order do
|
161
189
|
v
|
162
190
|
end
|
163
191
|
end
|
164
192
|
|
193
|
+
if v.nil?
|
194
|
+
assert_nil klass.base_sort_order
|
195
|
+
else
|
196
|
+
assert_equal v, klass.base_sort_order
|
197
|
+
end
|
198
|
+
|
165
199
|
assert_raise ArgumentError do
|
166
|
-
|
200
|
+
klass.sort_order.limit(1)
|
167
201
|
end
|
168
202
|
end
|
169
203
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_sort_order
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Weston Ganger
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-12-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,29 +16,16 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '5'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
27
|
-
-
|
28
|
-
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
34
|
-
type: :runtime
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ">="
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
41
|
-
description: Dead simple, fully customizable sorting pattern for ActiveRecord.
|
26
|
+
version: '5'
|
27
|
+
description: The "easy-peasy" dynamic sorting pattern for ActiveRecord that your Rails
|
28
|
+
apps deserve.
|
42
29
|
email:
|
43
30
|
- weston@westonganger.com
|
44
31
|
executables: []
|
@@ -64,7 +51,6 @@ files:
|
|
64
51
|
- test/dummy_app/app/models/post_with_base_order_b.rb
|
65
52
|
- test/dummy_app/app/models/post_with_base_order_b_and_a.rb
|
66
53
|
- test/dummy_app/app/models/post_with_volatile_base_order.rb
|
67
|
-
- test/dummy_app/app/models/test_helper.rb
|
68
54
|
- test/dummy_app/app/views/layouts/application.html.erb
|
69
55
|
- test/dummy_app/config.ru
|
70
56
|
- test/dummy_app/config/application.rb
|
@@ -104,53 +90,53 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
104
90
|
requirements:
|
105
91
|
- - ">="
|
106
92
|
- !ruby/object:Gem::Version
|
107
|
-
version:
|
93
|
+
version: '0'
|
108
94
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
95
|
requirements:
|
110
96
|
- - ">="
|
111
97
|
- !ruby/object:Gem::Version
|
112
98
|
version: '0'
|
113
99
|
requirements: []
|
114
|
-
rubygems_version: 3.1.
|
100
|
+
rubygems_version: 3.1.4
|
115
101
|
signing_key:
|
116
102
|
specification_version: 4
|
117
|
-
summary:
|
103
|
+
summary: The "easy-peasy" dynamic sorting pattern for ActiveRecord that your Rails
|
104
|
+
apps deserve.
|
118
105
|
test_files:
|
119
|
-
- test/test_helper.rb
|
120
106
|
- test/unit/active_sort_order_test.rb
|
121
107
|
- test/unit/errors_test.rb
|
122
|
-
- test/
|
123
|
-
- test/dummy_app/app/models/post_with_volatile_base_order.rb
|
124
|
-
- test/dummy_app/app/models/post_with_base_order_a_and_b.rb
|
125
|
-
- test/dummy_app/app/models/application_record.rb
|
108
|
+
- test/test_helper.rb
|
126
109
|
- test/dummy_app/app/models/post_with_base_order_b_and_a.rb
|
127
110
|
- test/dummy_app/app/models/post_with_base_order_a.rb
|
111
|
+
- test/dummy_app/app/models/post_with_base_order_a_and_b.rb
|
112
|
+
- test/dummy_app/app/models/post_with_volatile_base_order.rb
|
128
113
|
- test/dummy_app/app/models/post_with_base_order_b.rb
|
114
|
+
- test/dummy_app/app/models/application_record.rb
|
129
115
|
- test/dummy_app/app/models/post.rb
|
116
|
+
- test/dummy_app/app/controllers/application_controller.rb
|
130
117
|
- test/dummy_app/app/views/layouts/application.html.erb
|
131
|
-
- test/dummy_app/app/assets/stylesheets/application.css
|
132
118
|
- test/dummy_app/app/assets/config/manifest.js
|
133
119
|
- test/dummy_app/app/assets/javascripts/application.js
|
134
|
-
- test/dummy_app/app/
|
135
|
-
- test/dummy_app/config.
|
136
|
-
- test/dummy_app/
|
137
|
-
- test/dummy_app/config/
|
120
|
+
- test/dummy_app/app/assets/stylesheets/application.css
|
121
|
+
- test/dummy_app/config/secrets.yml
|
122
|
+
- test/dummy_app/config/routes.rb
|
123
|
+
- test/dummy_app/config/locales/en.yml
|
138
124
|
- test/dummy_app/config/environments/production.rb
|
139
|
-
- test/dummy_app/config/environments/test.rb
|
140
125
|
- test/dummy_app/config/environments/development.rb
|
141
|
-
- test/dummy_app/config/
|
126
|
+
- test/dummy_app/config/environments/test.rb
|
142
127
|
- test/dummy_app/config/environment.rb
|
143
|
-
- test/dummy_app/config/
|
128
|
+
- test/dummy_app/config/application.rb
|
129
|
+
- test/dummy_app/config/database.yml
|
144
130
|
- test/dummy_app/config/boot.rb
|
145
|
-
- test/dummy_app/config/initializers/
|
146
|
-
- test/dummy_app/config/initializers/inflections.rb
|
131
|
+
- test/dummy_app/config/initializers/backtrace_silencers.rb
|
147
132
|
- test/dummy_app/config/initializers/mime_types.rb
|
148
133
|
- test/dummy_app/config/initializers/session_store.rb
|
149
134
|
- test/dummy_app/config/initializers/wrap_parameters.rb
|
150
|
-
- test/dummy_app/config/initializers/
|
151
|
-
- test/dummy_app/config/
|
152
|
-
- test/dummy_app/config
|
135
|
+
- test/dummy_app/config/initializers/secret_token.rb
|
136
|
+
- test/dummy_app/config/initializers/inflections.rb
|
137
|
+
- test/dummy_app/config.ru
|
153
138
|
- test/dummy_app/Rakefile
|
154
139
|
- test/dummy_app/db/schema.rb
|
155
140
|
- test/dummy_app/db/test.sqlite3
|
156
141
|
- test/dummy_app/db/migrate/20210128155312_set_up_test_tables.rb
|
142
|
+
- test/dummy_app/log/test.log
|
File without changes
|