sortable-by 0.13.1 → 0.14.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/{ruby.yml → test.yml} +7 -4
- data/.rubocop.yml +3 -1
- data/Gemfile.lock +43 -52
- data/Gemfile.rails6 +4 -0
- data/Gemfile.rails6.lock +91 -0
- data/README.md +7 -5
- data/lib/sortable_by.rb +6 -2
- data/sortable-by.gemspec +3 -3
- metadata +10 -11
- data/spec/active_record/sortable_by_spec.rb +0 -86
- data/spec/spec_helper.rb +0 -72
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8ecdfdde420426271df2fbc5ed0c8360f2d3397ef8def31a94847f5dd78916a9
|
4
|
+
data.tar.gz: 4abb2f7fae1f56d461ac2c834db694182aa3c8e61c24a9af05618536243abb52
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8da947750cfc43e55b56b9040cd251368d93ac48efdeeee853da1294d2f5e69103f13271fe0da998cd1f4cb4e32e04e5a1643d406d8e057b19637cca152c7e94
|
7
|
+
data.tar.gz: e2a0a08467f48a5357e12e33cf98069e8b1026e950fca8512ae0a278484dab3ff3022f9ea0ff1bdfbd650996831aa8190705dbb91e320332236da1d3f8339286
|
@@ -1,4 +1,4 @@
|
|
1
|
-
name:
|
1
|
+
name: Test
|
2
2
|
|
3
3
|
on:
|
4
4
|
push:
|
@@ -7,13 +7,16 @@ on:
|
|
7
7
|
branches: [main]
|
8
8
|
|
9
9
|
jobs:
|
10
|
-
|
10
|
+
ruby:
|
11
11
|
runs-on: ubuntu-latest
|
12
12
|
strategy:
|
13
13
|
matrix:
|
14
|
-
ruby-version: ["2.
|
14
|
+
ruby-version: ["2.7", "3.0", "3.1"]
|
15
|
+
gemfiles: ["Gemfile", "Gemfile.rails6"]
|
16
|
+
env:
|
17
|
+
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
|
15
18
|
steps:
|
16
|
-
- uses: actions/checkout@
|
19
|
+
- uses: actions/checkout@v3
|
17
20
|
- uses: ruby/setup-ruby@v1
|
18
21
|
with:
|
19
22
|
ruby-version: ${{ matrix.ruby-version }}
|
data/.rubocop.yml
CHANGED
@@ -6,10 +6,12 @@ inherit_mode:
|
|
6
6
|
- Exclude
|
7
7
|
|
8
8
|
AllCops:
|
9
|
-
TargetRubyVersion: 2.
|
9
|
+
TargetRubyVersion: 2.7
|
10
10
|
Naming/MethodParameterName:
|
11
11
|
MinNameLength: 2
|
12
12
|
Naming/MemoizedInstanceVariableName:
|
13
13
|
Enabled: false
|
14
14
|
Naming/FileName:
|
15
15
|
Exclude: [lib/sortable-by.rb]
|
16
|
+
Metrics/ParameterLists:
|
17
|
+
Max: 10
|
data/Gemfile.lock
CHANGED
@@ -1,86 +1,77 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
sortable-by (0.
|
4
|
+
sortable-by (0.14.0)
|
5
5
|
activerecord
|
6
6
|
activesupport
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: http://rubygems.org/
|
10
10
|
specs:
|
11
|
-
activemodel (
|
12
|
-
activesupport (=
|
13
|
-
activerecord (
|
14
|
-
activemodel (=
|
15
|
-
activesupport (=
|
16
|
-
activesupport (
|
11
|
+
activemodel (7.0.3)
|
12
|
+
activesupport (= 7.0.3)
|
13
|
+
activerecord (7.0.3)
|
14
|
+
activemodel (= 7.0.3)
|
15
|
+
activesupport (= 7.0.3)
|
16
|
+
activesupport (7.0.3)
|
17
17
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
18
18
|
i18n (>= 1.6, < 2)
|
19
19
|
minitest (>= 5.1)
|
20
20
|
tzinfo (~> 2.0)
|
21
|
-
zeitwerk (~> 2.3)
|
22
21
|
ast (2.4.2)
|
23
|
-
concurrent-ruby (1.1.
|
24
|
-
diff-lcs (1.
|
25
|
-
i18n (1.
|
22
|
+
concurrent-ruby (1.1.10)
|
23
|
+
diff-lcs (1.5.0)
|
24
|
+
i18n (1.10.0)
|
26
25
|
concurrent-ruby (~> 1.0)
|
27
|
-
minitest (5.
|
28
|
-
parallel (1.
|
29
|
-
parser (3.
|
26
|
+
minitest (5.16.1)
|
27
|
+
parallel (1.22.1)
|
28
|
+
parser (3.1.2.0)
|
30
29
|
ast (~> 2.4.1)
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
rspec-
|
38
|
-
rspec-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
rspec-expectations (3.10.1)
|
30
|
+
rainbow (3.1.1)
|
31
|
+
rake (13.0.6)
|
32
|
+
regexp_parser (2.5.0)
|
33
|
+
rexml (3.2.5)
|
34
|
+
rspec (3.11.0)
|
35
|
+
rspec-core (~> 3.11.0)
|
36
|
+
rspec-expectations (~> 3.11.0)
|
37
|
+
rspec-mocks (~> 3.11.0)
|
38
|
+
rspec-core (3.11.0)
|
39
|
+
rspec-support (~> 3.11.0)
|
40
|
+
rspec-expectations (3.11.0)
|
43
41
|
diff-lcs (>= 1.2.0, < 2.0)
|
44
|
-
rspec-support (~> 3.
|
45
|
-
rspec-mocks (3.
|
42
|
+
rspec-support (~> 3.11.0)
|
43
|
+
rspec-mocks (3.11.1)
|
46
44
|
diff-lcs (>= 1.2.0, < 2.0)
|
47
|
-
rspec-support (~> 3.
|
48
|
-
rspec-support (3.
|
49
|
-
rubocop (1.
|
45
|
+
rspec-support (~> 3.11.0)
|
46
|
+
rspec-support (3.11.0)
|
47
|
+
rubocop (1.30.1)
|
50
48
|
parallel (~> 1.10)
|
51
|
-
parser (>= 3.
|
49
|
+
parser (>= 3.1.0.0)
|
52
50
|
rainbow (>= 2.2.2, < 4.0)
|
53
51
|
regexp_parser (>= 1.8, < 3.0)
|
54
|
-
rexml
|
55
|
-
rubocop-ast (>= 1.
|
52
|
+
rexml (>= 3.2.5, < 4.0)
|
53
|
+
rubocop-ast (>= 1.18.0, < 2.0)
|
56
54
|
ruby-progressbar (~> 1.7)
|
57
55
|
unicode-display_width (>= 1.4.0, < 3.0)
|
58
|
-
rubocop-ast (1.
|
59
|
-
parser (>=
|
60
|
-
rubocop-bsm (0.
|
56
|
+
rubocop-ast (1.18.0)
|
57
|
+
parser (>= 3.1.1.0)
|
58
|
+
rubocop-bsm (0.6.0)
|
61
59
|
rubocop (~> 1.0)
|
62
60
|
rubocop-performance
|
63
|
-
rubocop-rails
|
64
61
|
rubocop-rake
|
65
62
|
rubocop-rspec
|
66
|
-
rubocop-performance (1.
|
67
|
-
rubocop (>=
|
63
|
+
rubocop-performance (1.14.2)
|
64
|
+
rubocop (>= 1.7.0, < 2.0)
|
68
65
|
rubocop-ast (>= 0.4.0)
|
69
|
-
rubocop-
|
70
|
-
activesupport (>= 4.2.0)
|
71
|
-
rack (>= 1.1)
|
72
|
-
rubocop (>= 0.90.0, < 2.0)
|
73
|
-
rubocop-rake (0.5.1)
|
74
|
-
rubocop
|
75
|
-
rubocop-rspec (2.2.0)
|
66
|
+
rubocop-rake (0.6.0)
|
76
67
|
rubocop (~> 1.0)
|
77
|
-
|
68
|
+
rubocop-rspec (2.11.1)
|
69
|
+
rubocop (~> 1.19)
|
78
70
|
ruby-progressbar (1.11.0)
|
79
|
-
sqlite3 (1.4.
|
71
|
+
sqlite3 (1.4.4)
|
80
72
|
tzinfo (2.0.4)
|
81
73
|
concurrent-ruby (~> 1.0)
|
82
|
-
unicode-display_width (2.
|
83
|
-
zeitwerk (2.4.2)
|
74
|
+
unicode-display_width (2.2.0)
|
84
75
|
|
85
76
|
PLATFORMS
|
86
77
|
ruby
|
@@ -94,4 +85,4 @@ DEPENDENCIES
|
|
94
85
|
sqlite3
|
95
86
|
|
96
87
|
BUNDLED WITH
|
97
|
-
2.
|
88
|
+
2.3.9
|
data/Gemfile.rails6
ADDED
data/Gemfile.rails6.lock
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
sortable-by (0.14.0)
|
5
|
+
activerecord
|
6
|
+
activesupport
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activemodel (6.1.6)
|
12
|
+
activesupport (= 6.1.6)
|
13
|
+
activerecord (6.1.6)
|
14
|
+
activemodel (= 6.1.6)
|
15
|
+
activesupport (= 6.1.6)
|
16
|
+
activesupport (6.1.6)
|
17
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
18
|
+
i18n (>= 1.6, < 2)
|
19
|
+
minitest (>= 5.1)
|
20
|
+
tzinfo (~> 2.0)
|
21
|
+
zeitwerk (~> 2.3)
|
22
|
+
ast (2.4.2)
|
23
|
+
concurrent-ruby (1.1.10)
|
24
|
+
diff-lcs (1.5.0)
|
25
|
+
i18n (1.10.0)
|
26
|
+
concurrent-ruby (~> 1.0)
|
27
|
+
minitest (5.16.1)
|
28
|
+
parallel (1.22.1)
|
29
|
+
parser (3.1.2.0)
|
30
|
+
ast (~> 2.4.1)
|
31
|
+
rainbow (3.1.1)
|
32
|
+
rake (13.0.6)
|
33
|
+
regexp_parser (2.5.0)
|
34
|
+
rexml (3.2.5)
|
35
|
+
rspec (3.11.0)
|
36
|
+
rspec-core (~> 3.11.0)
|
37
|
+
rspec-expectations (~> 3.11.0)
|
38
|
+
rspec-mocks (~> 3.11.0)
|
39
|
+
rspec-core (3.11.0)
|
40
|
+
rspec-support (~> 3.11.0)
|
41
|
+
rspec-expectations (3.11.0)
|
42
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
+
rspec-support (~> 3.11.0)
|
44
|
+
rspec-mocks (3.11.1)
|
45
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
46
|
+
rspec-support (~> 3.11.0)
|
47
|
+
rspec-support (3.11.0)
|
48
|
+
rubocop (1.30.1)
|
49
|
+
parallel (~> 1.10)
|
50
|
+
parser (>= 3.1.0.0)
|
51
|
+
rainbow (>= 2.2.2, < 4.0)
|
52
|
+
regexp_parser (>= 1.8, < 3.0)
|
53
|
+
rexml (>= 3.2.5, < 4.0)
|
54
|
+
rubocop-ast (>= 1.18.0, < 2.0)
|
55
|
+
ruby-progressbar (~> 1.7)
|
56
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
57
|
+
rubocop-ast (1.18.0)
|
58
|
+
parser (>= 3.1.1.0)
|
59
|
+
rubocop-bsm (0.6.0)
|
60
|
+
rubocop (~> 1.0)
|
61
|
+
rubocop-performance
|
62
|
+
rubocop-rake
|
63
|
+
rubocop-rspec
|
64
|
+
rubocop-performance (1.14.2)
|
65
|
+
rubocop (>= 1.7.0, < 2.0)
|
66
|
+
rubocop-ast (>= 0.4.0)
|
67
|
+
rubocop-rake (0.6.0)
|
68
|
+
rubocop (~> 1.0)
|
69
|
+
rubocop-rspec (2.11.1)
|
70
|
+
rubocop (~> 1.19)
|
71
|
+
ruby-progressbar (1.11.0)
|
72
|
+
sqlite3 (1.4.4)
|
73
|
+
tzinfo (2.0.4)
|
74
|
+
concurrent-ruby (~> 1.0)
|
75
|
+
unicode-display_width (2.2.0)
|
76
|
+
zeitwerk (2.6.0)
|
77
|
+
|
78
|
+
PLATFORMS
|
79
|
+
x86_64-linux
|
80
|
+
|
81
|
+
DEPENDENCIES
|
82
|
+
activerecord (~> 6.1)
|
83
|
+
bundler
|
84
|
+
rake
|
85
|
+
rspec
|
86
|
+
rubocop-bsm
|
87
|
+
sortable-by!
|
88
|
+
sqlite3
|
89
|
+
|
90
|
+
BUNDLED WITH
|
91
|
+
2.3.9
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Sortable By
|
2
2
|
|
3
|
-
[![
|
3
|
+
[![Test](https://github.com/bsm/sortable-by/actions/workflows/test.yml/badge.svg)](https://github.com/bsm/sortable-by/actions/workflows/test.yml)
|
4
4
|
[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
|
5
5
|
|
6
6
|
ActiveRecord plugin to parse the sort order from a query parameter, match against a white-list and generate a scope. Useful for [JSON-API][jsonapi] compatibility.
|
@@ -20,10 +20,12 @@ class Post < ActiveRecord::Base
|
|
20
20
|
sortable_by :title, :id
|
21
21
|
end
|
22
22
|
|
23
|
-
Post.sorted_by('title')
|
24
|
-
Post.sorted_by('-title')
|
25
|
-
Post.sorted_by('
|
26
|
-
Post.sorted_by(
|
23
|
+
Post.sorted_by('title') # => ORDER BY LOWER(posts.title) ASC
|
24
|
+
Post.sorted_by('-title') # => ORDER BY LOWER(posts.title) DESC
|
25
|
+
Post.sorted_by('title,-id') # => ORDER BY LOWER(posts.title) ASC, id DESC
|
26
|
+
Post.sorted_by(['title', '-id']) # => ORDER BY LOWER(posts.title) ASC, id DESC
|
27
|
+
Post.sorted_by('bad,title') # => ORDER BY LOWER(posts.title) ASC
|
28
|
+
Post.sorted_by(nil) # => ORDER BY LOWER(posts.title) ASC
|
27
29
|
```
|
28
30
|
|
29
31
|
Case-sensitive:
|
data/lib/sortable_by.rb
CHANGED
@@ -38,7 +38,7 @@ module ActiveRecord # :nodoc:
|
|
38
38
|
|
39
39
|
def order(relation, expr, fallback: true)
|
40
40
|
matched = false
|
41
|
-
expr.to_s.split(',').each do |name|
|
41
|
+
Array.wrap(expr).flat_map {|v| v.to_s.split(',') }.each do |name|
|
42
42
|
name.strip!
|
43
43
|
|
44
44
|
rank = :asc
|
@@ -65,6 +65,10 @@ module ActiveRecord # :nodoc:
|
|
65
65
|
def initialize(name, as: nil, scope: nil, eager_load: nil, case_sensitive: false)
|
66
66
|
@cols = Array.wrap(as)
|
67
67
|
@eager_load = Array.wrap(eager_load).presence
|
68
|
+
if @eager_load
|
69
|
+
inner = @eager_load.inspect
|
70
|
+
ActiveSupport::Deprecation.warn("using :eager_load in sortable_by blocks is deprecated. Please pass scope: -> { includes(#{inner}) } instead.")
|
71
|
+
end
|
68
72
|
@case_sensitive = case_sensitive == true
|
69
73
|
|
70
74
|
# validate custom_scope
|
@@ -101,7 +105,7 @@ module ActiveRecord # :nodoc:
|
|
101
105
|
end
|
102
106
|
|
103
107
|
relation = relation.eager_load(*@eager_load) if @eager_load
|
104
|
-
relation = relation.
|
108
|
+
relation = relation.instance_exec(&@custom_scope) if @custom_scope
|
105
109
|
relation
|
106
110
|
end
|
107
111
|
end
|
data/sortable-by.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'sortable-by'
|
3
|
-
s.version = '0.
|
3
|
+
s.version = '0.14.0'
|
4
4
|
s.authors = ['Dimitrij Denissenko']
|
5
5
|
s.email = ['dimitrij@blacksquaremedia.com']
|
6
6
|
s.summary = 'Generate white-listed sort scopes from URL parameter values'
|
@@ -9,9 +9,8 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.license = 'MIT'
|
10
10
|
|
11
11
|
s.files = `git ls-files -z`.split("\x0").reject {|f| f.match(%r{^spec/}) }
|
12
|
-
s.test_files = `git ls-files -z -- spec/*`.split("\x0")
|
13
12
|
s.require_paths = ['lib']
|
14
|
-
s.required_ruby_version = '>= 2.
|
13
|
+
s.required_ruby_version = '>= 2.7'
|
15
14
|
|
16
15
|
s.add_dependency 'activerecord'
|
17
16
|
s.add_dependency 'activesupport'
|
@@ -21,4 +20,5 @@ Gem::Specification.new do |s|
|
|
21
20
|
s.add_development_dependency 'rspec'
|
22
21
|
s.add_development_dependency 'rubocop-bsm'
|
23
22
|
s.add_development_dependency 'sqlite3'
|
23
|
+
s.metadata['rubygems_mfa_required'] = 'true'
|
24
24
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sortable-by
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.14.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dimitrij Denissenko
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -115,23 +115,24 @@ executables: []
|
|
115
115
|
extensions: []
|
116
116
|
extra_rdoc_files: []
|
117
117
|
files:
|
118
|
-
- ".github/workflows/
|
118
|
+
- ".github/workflows/test.yml"
|
119
119
|
- ".gitignore"
|
120
120
|
- ".rubocop.yml"
|
121
121
|
- Gemfile
|
122
122
|
- Gemfile.lock
|
123
|
+
- Gemfile.rails6
|
124
|
+
- Gemfile.rails6.lock
|
123
125
|
- LICENSE
|
124
126
|
- README.md
|
125
127
|
- Rakefile
|
126
128
|
- lib/sortable-by.rb
|
127
129
|
- lib/sortable_by.rb
|
128
130
|
- sortable-by.gemspec
|
129
|
-
- spec/active_record/sortable_by_spec.rb
|
130
|
-
- spec/spec_helper.rb
|
131
131
|
homepage: https://github.com/bsm/sortable-by
|
132
132
|
licenses:
|
133
133
|
- MIT
|
134
|
-
metadata:
|
134
|
+
metadata:
|
135
|
+
rubygems_mfa_required: 'true'
|
135
136
|
post_install_message:
|
136
137
|
rdoc_options: []
|
137
138
|
require_paths:
|
@@ -140,17 +141,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
140
141
|
requirements:
|
141
142
|
- - ">="
|
142
143
|
- !ruby/object:Gem::Version
|
143
|
-
version: '2.
|
144
|
+
version: '2.7'
|
144
145
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
145
146
|
requirements:
|
146
147
|
- - ">="
|
147
148
|
- !ruby/object:Gem::Version
|
148
149
|
version: '0'
|
149
150
|
requirements: []
|
150
|
-
rubygems_version: 3.
|
151
|
+
rubygems_version: 3.3.3
|
151
152
|
signing_key:
|
152
153
|
specification_version: 4
|
153
154
|
summary: Generate white-listed sort scopes from URL parameter values
|
154
|
-
test_files:
|
155
|
-
- spec/active_record/sortable_by_spec.rb
|
156
|
-
- spec/spec_helper.rb
|
155
|
+
test_files: []
|
@@ -1,86 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe ActiveRecord::SortableBy do
|
4
|
-
before do
|
5
|
-
c = ActiveRecord::Base.connection
|
6
|
-
c.tables.each do |t|
|
7
|
-
c.update "DELETE FROM #{t}"
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
it 'has config' do
|
12
|
-
expect(Post._sortable_by_config._fields.keys).to match_array(%w[title created])
|
13
|
-
expect(Post._sortable_by_config._default).to eq('-created')
|
14
|
-
expect(SubPost._sortable_by_config._fields.keys).to match_array(%w[title created])
|
15
|
-
expect(SubPost._sortable_by_config._default).to eq('-created')
|
16
|
-
|
17
|
-
expect(App._sortable_by_config._fields.keys).to match_array(%w[name version])
|
18
|
-
expect(App._sortable_by_config._default).to eq('name')
|
19
|
-
|
20
|
-
expect(Shop._sortable_by_config._fields.keys).to be_empty
|
21
|
-
expect(Shop._sortable_by_config._default).to be_nil
|
22
|
-
|
23
|
-
expect(Product._sortable_by_config._fields.keys).to match_array(%w[name shop])
|
24
|
-
expect(Product._sortable_by_config._default).to eq('shop,name')
|
25
|
-
end
|
26
|
-
|
27
|
-
it 'simplies order' do
|
28
|
-
Post.create! title: 'A', created_at: Time.at(1515151500)
|
29
|
-
Post.create! title: 'b', created_at: Time.at(1515151600)
|
30
|
-
Post.create! title: 'C', created_at: Time.at(1515151400)
|
31
|
-
|
32
|
-
expect(Post.sorted_by(nil).pluck(:title)).to eq(%w[b A C])
|
33
|
-
expect(Post.sorted_by('').pluck(:title)).to eq(%w[b A C])
|
34
|
-
expect(Post.sorted_by('invalid').pluck(:title)).to eq(%w[b A C])
|
35
|
-
|
36
|
-
expect(Post.sorted_by('-created').pluck(:title)).to eq(%w[b A C])
|
37
|
-
expect(Post.sorted_by('created').pluck(:title)).to eq(%w[C A b])
|
38
|
-
|
39
|
-
expect(Post.sorted_by('title').pluck(:title)).to eq(%w[A C b])
|
40
|
-
expect(Post.sorted_by('-title').pluck(:title)).to eq(%w[b C A])
|
41
|
-
expect(Post.sorted_by(' title ').pluck(:title)).to eq(%w[A C b])
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'supports STI inheritance and overrides' do
|
45
|
-
SubPost.create! title: 'A', created_at: Time.at(1515151700)
|
46
|
-
SubPost.create! title: 'b', created_at: Time.at(1515151600)
|
47
|
-
Post.create! title: 'C', created_at: Time.at(1515151400)
|
48
|
-
SubPost.create! title: 'D', created_at: Time.at(1515151500)
|
49
|
-
|
50
|
-
expect(Post.sorted_by(nil).pluck(:title)).to eq(%w[A b D C])
|
51
|
-
expect(SubPost.sorted_by(nil).pluck(:title)).to eq(%w[A b D])
|
52
|
-
expect(SubPost.sorted_by('-created').pluck(:title)).to eq(%w[A b D])
|
53
|
-
expect(SubPost.sorted_by('created').pluck(:title)).to eq(%w[D b A])
|
54
|
-
|
55
|
-
expect(Post.sorted_by('title').pluck(:title)).to eq(%w[A C D b])
|
56
|
-
expect(SubPost.sorted_by('title').pluck(:title)).to eq(%w[A b D])
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'supports composition' do
|
60
|
-
App.create! name: 'E', major: 0, minor: 9, patch: 2
|
61
|
-
App.create! name: 'A', major: 1, minor: 0, patch: 1
|
62
|
-
App.create! name: 'D', major: 1, minor: 0, patch: 6
|
63
|
-
App.create! name: 'C', major: 1, minor: 1, patch: 0
|
64
|
-
App.create! name: 'B', major: 2, minor: 2, patch: 0
|
65
|
-
|
66
|
-
expect(App.sorted_by(nil).pluck(:name)).to eq(%w[A B C D E])
|
67
|
-
expect(App.sorted_by('version').pluck(:name)).to eq(%w[E A D C B])
|
68
|
-
expect(App.sorted_by('-version').pluck(:name)).to eq(%w[B C D A E])
|
69
|
-
end
|
70
|
-
|
71
|
-
it 'supports associations' do
|
72
|
-
y = Shop.create! name: 'Y'
|
73
|
-
x = Shop.create! name: 'X'
|
74
|
-
|
75
|
-
Product.create! name: 'a', shop_id: y.id
|
76
|
-
Product.create! name: 'B', shop_id: y.id, active: false
|
77
|
-
Product.create! name: 'c', shop_id: x.id
|
78
|
-
Product.create! name: 'D', shop_id: y.id
|
79
|
-
Product.create! name: 'e', shop_id: x.id
|
80
|
-
Product.create! name: 'f', shop_id: x.id, active: false
|
81
|
-
|
82
|
-
expect(Product.sorted_by(nil).pluck(:name)).to eq(%w[c e f a B D])
|
83
|
-
expect(Product.where(active: true).sorted_by(nil).pluck(:name)).to eq(%w[c e a D])
|
84
|
-
expect(Product.sorted_by('name').pluck(:name)).to eq(%w[a B c D e f])
|
85
|
-
end
|
86
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,72 +0,0 @@
|
|
1
|
-
ENV['RACK_ENV'] ||= 'test'
|
2
|
-
require 'sortable-by'
|
3
|
-
require 'rspec'
|
4
|
-
|
5
|
-
ActiveRecord::Base.configurations = {
|
6
|
-
'test' => {
|
7
|
-
'adapter' => 'sqlite3',
|
8
|
-
'database' => ':memory:',
|
9
|
-
},
|
10
|
-
}
|
11
|
-
ActiveRecord::Base.establish_connection :test
|
12
|
-
|
13
|
-
ActiveRecord::Base.connection.create_table :posts do |t|
|
14
|
-
t.string :type
|
15
|
-
t.string :title, null: false
|
16
|
-
t.timestamp :created_at, null: false
|
17
|
-
end
|
18
|
-
|
19
|
-
class Post < ActiveRecord::Base
|
20
|
-
sortable_by :title, default: '-created', case_sensitive: true do |s|
|
21
|
-
s.field :created, as: arel_table[:created_at]
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
class SubPost < Post
|
26
|
-
sortable_by do |s|
|
27
|
-
s.field :title
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
# ---------------------------------------------------------------------
|
32
|
-
|
33
|
-
ActiveRecord::Base.connection.create_table :apps do |t|
|
34
|
-
t.string :name, null: false
|
35
|
-
t.integer :major, null: false
|
36
|
-
t.integer :minor, null: false
|
37
|
-
t.integer :patch, null: false
|
38
|
-
end
|
39
|
-
|
40
|
-
class App < ActiveRecord::Base
|
41
|
-
sortable_by :name do |s|
|
42
|
-
s.field :version, as: %i[major minor patch]
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
# ---------------------------------------------------------------------
|
47
|
-
|
48
|
-
ActiveRecord::Base.connection.create_table :shops do |t|
|
49
|
-
t.string :name, null: false
|
50
|
-
end
|
51
|
-
|
52
|
-
class Shop < ActiveRecord::Base
|
53
|
-
end
|
54
|
-
|
55
|
-
# ---------------------------------------------------------------------
|
56
|
-
|
57
|
-
ActiveRecord::Base.connection.create_table :products do |t|
|
58
|
-
t.string :name, null: false
|
59
|
-
t.integer :shop_id, null: false
|
60
|
-
t.boolean :active, null: false, default: true
|
61
|
-
t.foreign_key :shops
|
62
|
-
end
|
63
|
-
|
64
|
-
class Product < ActiveRecord::Base
|
65
|
-
belongs_to :shop
|
66
|
-
|
67
|
-
sortable_by do |s|
|
68
|
-
s.field :name
|
69
|
-
s.field :shop, as: Shop.arel_table[:name], eager_load: :shop
|
70
|
-
s.default 'shop,name'
|
71
|
-
end
|
72
|
-
end
|