sortable-by 0.13.3 → 0.14.1

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
  SHA256:
3
- metadata.gz: 739ba7b6c67825b04f539ac5f288617865cf98e0286b5504d561580e0b4a4da7
4
- data.tar.gz: 9790f0fb3506dbe02f83a5a1fb312d568e0f3b4bcdb7265da896604f7ff398b9
3
+ metadata.gz: dfd344cb151d2407e34c120d0905257920e620a4f27a0ac7f49f2006b9830c4b
4
+ data.tar.gz: 605ab535b5e08ec0fe6a04eac6fb1c0e74a1992e49733771895c534dcf21e5c4
5
5
  SHA512:
6
- metadata.gz: 265748bb3409aed86fa24dd6b46102967408b50bb663b682d2a834ca36cc0c5694ad2dcf266f6d7e2751c4667ab975c539831df2d829a8fab9c88c6483a1edf9
7
- data.tar.gz: a67635307cbafedaaf385d0a340e7745031a3e4439d5cd1f112400a6649f8762bf88e54f6655006550bf448b35a8ea60a47bee837f58e6a7168f0518c12c4bfd
6
+ metadata.gz: 9924d17cfb90c4c66266a37b949c4bbb6503dcd433d7b00c8abdc349a7519a342c41edb719004b83876e57b1ce5e5891bfac17ea0ae8d871b27acada5de157cf
7
+ data.tar.gz: 7119d31c02d3deed0db68c3d69852497049f27a5f79dc5d510be3cff716d4930a8485d08a00a4ca35b0a571af8c2557dde7a6c2f4112ce06f88f4116e2ac1853
@@ -11,12 +11,12 @@ jobs:
11
11
  runs-on: ubuntu-latest
12
12
  strategy:
13
13
  matrix:
14
- ruby-version: ["2.7", "3.0", "3.1"]
14
+ ruby-version: ["2.7", "3.0", "3.1", "3.2"]
15
15
  gemfiles: ["Gemfile", "Gemfile.rails6"]
16
16
  env:
17
17
  BUNDLE_GEMFILE: ${{ matrix.gemfile }}
18
18
  steps:
19
- - uses: actions/checkout@v2
19
+ - uses: actions/checkout@v3
20
20
  - uses: ruby/setup-ruby@v1
21
21
  with:
22
22
  ruby-version: ${{ matrix.ruby-version }}
data/.rubocop.yml CHANGED
@@ -13,3 +13,5 @@ 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 CHANGED
@@ -1,2 +1,7 @@
1
1
  source 'http://rubygems.org'
2
2
  gemspec
3
+
4
+ gem 'rake'
5
+ gem 'rspec'
6
+ gem 'rubocop-bsm'
7
+ gem 'sqlite3'
data/Gemfile.lock CHANGED
@@ -1,77 +1,84 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sortable-by (0.13.3)
4
+ sortable-by (0.14.1)
5
5
  activerecord
6
6
  activesupport
7
7
 
8
8
  GEM
9
9
  remote: http://rubygems.org/
10
10
  specs:
11
- activemodel (7.0.2.3)
12
- activesupport (= 7.0.2.3)
13
- activerecord (7.0.2.3)
14
- activemodel (= 7.0.2.3)
15
- activesupport (= 7.0.2.3)
16
- activesupport (7.0.2.3)
11
+ activemodel (7.0.4.2)
12
+ activesupport (= 7.0.4.2)
13
+ activerecord (7.0.4.2)
14
+ activemodel (= 7.0.4.2)
15
+ activesupport (= 7.0.4.2)
16
+ activesupport (7.0.4.2)
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
21
  ast (2.4.2)
22
- concurrent-ruby (1.1.9)
22
+ concurrent-ruby (1.2.0)
23
23
  diff-lcs (1.5.0)
24
- i18n (1.10.0)
24
+ i18n (1.12.0)
25
25
  concurrent-ruby (~> 1.0)
26
- minitest (5.15.0)
27
- parallel (1.21.0)
28
- parser (3.1.1.0)
26
+ json (2.6.3)
27
+ mini_portile2 (2.8.1)
28
+ minitest (5.17.0)
29
+ parallel (1.22.1)
30
+ parser (3.2.0.0)
29
31
  ast (~> 2.4.1)
30
32
  rainbow (3.1.1)
31
33
  rake (13.0.6)
32
- regexp_parser (2.2.1)
34
+ regexp_parser (2.6.2)
33
35
  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)
36
+ rspec (3.12.0)
37
+ rspec-core (~> 3.12.0)
38
+ rspec-expectations (~> 3.12.0)
39
+ rspec-mocks (~> 3.12.0)
40
+ rspec-core (3.12.0)
41
+ rspec-support (~> 3.12.0)
42
+ rspec-expectations (3.12.2)
41
43
  diff-lcs (>= 1.2.0, < 2.0)
42
- rspec-support (~> 3.11.0)
43
- rspec-mocks (3.11.0)
44
+ rspec-support (~> 3.12.0)
45
+ rspec-mocks (3.12.3)
44
46
  diff-lcs (>= 1.2.0, < 2.0)
45
- rspec-support (~> 3.11.0)
46
- rspec-support (3.11.0)
47
- rubocop (1.26.0)
47
+ rspec-support (~> 3.12.0)
48
+ rspec-support (3.12.0)
49
+ rubocop (1.44.1)
50
+ json (~> 2.3)
48
51
  parallel (~> 1.10)
49
- parser (>= 3.1.0.0)
52
+ parser (>= 3.2.0.0)
50
53
  rainbow (>= 2.2.2, < 4.0)
51
54
  regexp_parser (>= 1.8, < 3.0)
52
- rexml
53
- rubocop-ast (>= 1.16.0, < 2.0)
55
+ rexml (>= 3.2.5, < 4.0)
56
+ rubocop-ast (>= 1.24.1, < 2.0)
54
57
  ruby-progressbar (~> 1.7)
55
- unicode-display_width (>= 1.4.0, < 3.0)
56
- rubocop-ast (1.16.0)
58
+ unicode-display_width (>= 2.4.0, < 3.0)
59
+ rubocop-ast (1.24.1)
57
60
  parser (>= 3.1.1.0)
58
- rubocop-bsm (0.6.0)
61
+ rubocop-bsm (0.6.1)
59
62
  rubocop (~> 1.0)
60
63
  rubocop-performance
61
64
  rubocop-rake
62
65
  rubocop-rspec
63
- rubocop-performance (1.13.3)
66
+ rubocop-capybara (2.17.0)
67
+ rubocop (~> 1.41)
68
+ rubocop-performance (1.15.2)
64
69
  rubocop (>= 1.7.0, < 2.0)
65
70
  rubocop-ast (>= 0.4.0)
66
71
  rubocop-rake (0.6.0)
67
72
  rubocop (~> 1.0)
68
- rubocop-rspec (2.9.0)
69
- rubocop (~> 1.19)
73
+ rubocop-rspec (2.18.1)
74
+ rubocop (~> 1.33)
75
+ rubocop-capybara (~> 2.17)
70
76
  ruby-progressbar (1.11.0)
71
- sqlite3 (1.4.2)
72
- tzinfo (2.0.4)
77
+ sqlite3 (1.6.0)
78
+ mini_portile2 (~> 2.8.0)
79
+ tzinfo (2.0.6)
73
80
  concurrent-ruby (~> 1.0)
74
- unicode-display_width (2.1.0)
81
+ unicode-display_width (2.4.2)
75
82
 
76
83
  PLATFORMS
77
84
  ruby
data/Gemfile.rails6 CHANGED
@@ -2,3 +2,7 @@ source 'https://rubygems.org'
2
2
  gemspec
3
3
 
4
4
  gem 'activerecord', '~> 6.1'
5
+ gem 'rake'
6
+ gem 'rspec'
7
+ gem 'rubocop-bsm'
8
+ gem 'sqlite3'
data/Gemfile.rails6.lock CHANGED
@@ -1,79 +1,84 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sortable-by (0.13.3)
4
+ sortable-by (0.14.1)
5
5
  activerecord
6
6
  activesupport
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- activemodel (6.1.4.7)
12
- activesupport (= 6.1.4.7)
13
- activerecord (6.1.4.7)
14
- activemodel (= 6.1.4.7)
15
- activesupport (= 6.1.4.7)
16
- activesupport (6.1.4.7)
11
+ activemodel (6.1.7.2)
12
+ activesupport (= 6.1.7.2)
13
+ activerecord (6.1.7.2)
14
+ activemodel (= 6.1.7.2)
15
+ activesupport (= 6.1.7.2)
16
+ activesupport (6.1.7.2)
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
21
  zeitwerk (~> 2.3)
22
22
  ast (2.4.2)
23
- concurrent-ruby (1.1.9)
23
+ concurrent-ruby (1.2.0)
24
24
  diff-lcs (1.5.0)
25
- i18n (1.10.0)
25
+ i18n (1.12.0)
26
26
  concurrent-ruby (~> 1.0)
27
- minitest (5.15.0)
28
- parallel (1.21.0)
29
- parser (3.1.1.0)
27
+ json (2.6.3)
28
+ minitest (5.17.0)
29
+ parallel (1.22.1)
30
+ parser (3.2.0.0)
30
31
  ast (~> 2.4.1)
31
32
  rainbow (3.1.1)
32
33
  rake (13.0.6)
33
- regexp_parser (2.2.1)
34
+ regexp_parser (2.6.2)
34
35
  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)
36
+ rspec (3.12.0)
37
+ rspec-core (~> 3.12.0)
38
+ rspec-expectations (~> 3.12.0)
39
+ rspec-mocks (~> 3.12.0)
40
+ rspec-core (3.12.0)
41
+ rspec-support (~> 3.12.0)
42
+ rspec-expectations (3.12.2)
42
43
  diff-lcs (>= 1.2.0, < 2.0)
43
- rspec-support (~> 3.11.0)
44
- rspec-mocks (3.11.0)
44
+ rspec-support (~> 3.12.0)
45
+ rspec-mocks (3.12.3)
45
46
  diff-lcs (>= 1.2.0, < 2.0)
46
- rspec-support (~> 3.11.0)
47
- rspec-support (3.11.0)
48
- rubocop (1.26.0)
47
+ rspec-support (~> 3.12.0)
48
+ rspec-support (3.12.0)
49
+ rubocop (1.44.1)
50
+ json (~> 2.3)
49
51
  parallel (~> 1.10)
50
- parser (>= 3.1.0.0)
52
+ parser (>= 3.2.0.0)
51
53
  rainbow (>= 2.2.2, < 4.0)
52
54
  regexp_parser (>= 1.8, < 3.0)
53
- rexml
54
- rubocop-ast (>= 1.16.0, < 2.0)
55
+ rexml (>= 3.2.5, < 4.0)
56
+ rubocop-ast (>= 1.24.1, < 2.0)
55
57
  ruby-progressbar (~> 1.7)
56
- unicode-display_width (>= 1.4.0, < 3.0)
57
- rubocop-ast (1.16.0)
58
+ unicode-display_width (>= 2.4.0, < 3.0)
59
+ rubocop-ast (1.24.1)
58
60
  parser (>= 3.1.1.0)
59
- rubocop-bsm (0.6.0)
61
+ rubocop-bsm (0.6.1)
60
62
  rubocop (~> 1.0)
61
63
  rubocop-performance
62
64
  rubocop-rake
63
65
  rubocop-rspec
64
- rubocop-performance (1.13.3)
66
+ rubocop-capybara (2.17.0)
67
+ rubocop (~> 1.41)
68
+ rubocop-performance (1.15.2)
65
69
  rubocop (>= 1.7.0, < 2.0)
66
70
  rubocop-ast (>= 0.4.0)
67
71
  rubocop-rake (0.6.0)
68
72
  rubocop (~> 1.0)
69
- rubocop-rspec (2.9.0)
70
- rubocop (~> 1.19)
73
+ rubocop-rspec (2.18.1)
74
+ rubocop (~> 1.33)
75
+ rubocop-capybara (~> 2.17)
71
76
  ruby-progressbar (1.11.0)
72
- sqlite3 (1.4.2)
73
- tzinfo (2.0.4)
77
+ sqlite3 (1.6.0-x86_64-linux)
78
+ tzinfo (2.0.6)
74
79
  concurrent-ruby (~> 1.0)
75
- unicode-display_width (2.1.0)
76
- zeitwerk (2.5.4)
80
+ unicode-display_width (2.4.2)
81
+ zeitwerk (2.6.6)
77
82
 
78
83
  PLATFORMS
79
84
  x86_64-linux
data/README.md CHANGED
@@ -64,19 +64,6 @@ App.sorted_by('version') # => ORDER BY apps.major ASC, apps.minor ASC, apps.patc
64
64
  App.sorted_by(nil) # => ORDER BY apps.major DESC, apps.minor DESC, apps.patch DESC
65
65
  ```
66
66
 
67
- Associations (eager load):
68
-
69
- ```ruby
70
- class Product < ActiveRecord::Base
71
- belongs_to :shop
72
- sortable_by do |x|
73
- x.field :name
74
- x.field :shop, as: Shop.arel_table[:name], eager_load: :shop
75
- x.default 'shop,name'
76
- end
77
- end
78
- ```
79
-
80
67
  Associations (custom scope):
81
68
 
82
69
  ```ruby
data/lib/sortable_by.rb CHANGED
@@ -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.instance_eval(&@custom_scope) if @custom_scope
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.13.3'
3
+ s.version = '0.14.1'
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,7 +9,6 @@ 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
13
  s.required_ruby_version = '>= 2.7'
15
14
 
@@ -17,9 +16,5 @@ Gem::Specification.new do |s|
17
16
  s.add_dependency 'activesupport'
18
17
 
19
18
  s.add_development_dependency 'bundler'
20
- s.add_development_dependency 'rake'
21
- s.add_development_dependency 'rspec'
22
- s.add_development_dependency 'rubocop-bsm'
23
- s.add_development_dependency 'sqlite3'
24
19
  s.metadata['rubygems_mfa_required'] = 'true'
25
20
  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.13.3
4
+ version: 0.14.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dimitrij Denissenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-11 00:00:00.000000000 Z
11
+ date: 2023-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -52,62 +52,6 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: rake
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: rspec
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: rubocop-bsm
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- version: '0'
97
- - !ruby/object:Gem::Dependency
98
- name: sqlite3
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: '0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: '0'
111
55
  description: ActiveRecord plugin
112
56
  email:
113
57
  - dimitrij@blacksquaremedia.com
@@ -128,8 +72,6 @@ files:
128
72
  - lib/sortable-by.rb
129
73
  - lib/sortable_by.rb
130
74
  - sortable-by.gemspec
131
- - spec/active_record/sortable_by_spec.rb
132
- - spec/spec_helper.rb
133
75
  homepage: https://github.com/bsm/sortable-by
134
76
  licenses:
135
77
  - MIT
@@ -154,6 +96,4 @@ rubygems_version: 3.3.3
154
96
  signing_key:
155
97
  specification_version: 4
156
98
  summary: Generate white-listed sort scopes from URL parameter values
157
- test_files:
158
- - spec/active_record/sortable_by_spec.rb
159
- - spec/spec_helper.rb
99
+ test_files: []
@@ -1,95 +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 'supports simple ordering' 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 array params' do
45
- Post.create! title: 'A', created_at: Time.at(1515151500)
46
- Post.create! title: 'C', created_at: Time.at(1515151400)
47
- Post.create! title: 'B', created_at: Time.at(1515151400)
48
-
49
- expect(Post.sorted_by(['created', '-title']).pluck(:title)).to eq(%w[C B A])
50
- expect(Post.sorted_by(['created , -title']).pluck(:title)).to eq(%w[C B A])
51
- end
52
-
53
- it 'supports STI inheritance and overrides' do
54
- SubPost.create! title: 'A', created_at: Time.at(1515151700)
55
- SubPost.create! title: 'b', created_at: Time.at(1515151600)
56
- Post.create! title: 'C', created_at: Time.at(1515151400)
57
- SubPost.create! title: 'D', created_at: Time.at(1515151500)
58
-
59
- expect(Post.sorted_by(nil).pluck(:title)).to eq(%w[A b D C])
60
- expect(SubPost.sorted_by(nil).pluck(:title)).to eq(%w[A b D])
61
- expect(SubPost.sorted_by('-created').pluck(:title)).to eq(%w[A b D])
62
- expect(SubPost.sorted_by('created').pluck(:title)).to eq(%w[D b A])
63
-
64
- expect(Post.sorted_by('title').pluck(:title)).to eq(%w[A C D b])
65
- expect(SubPost.sorted_by('title').pluck(:title)).to eq(%w[A b D])
66
- end
67
-
68
- it 'supports composition' do
69
- App.create! name: 'E', major: 0, minor: 9, patch: 2
70
- App.create! name: 'A', major: 1, minor: 0, patch: 1
71
- App.create! name: 'D', major: 1, minor: 0, patch: 6
72
- App.create! name: 'C', major: 1, minor: 1, patch: 0
73
- App.create! name: 'B', major: 2, minor: 2, patch: 0
74
-
75
- expect(App.sorted_by(nil).pluck(:name)).to eq(%w[A B C D E])
76
- expect(App.sorted_by('version').pluck(:name)).to eq(%w[E A D C B])
77
- expect(App.sorted_by('-version').pluck(:name)).to eq(%w[B C D A E])
78
- end
79
-
80
- it 'supports associations' do
81
- y = Shop.create! name: 'Y'
82
- x = Shop.create! name: 'X'
83
-
84
- Product.create! name: 'a', shop_id: y.id
85
- Product.create! name: 'B', shop_id: y.id, active: false
86
- Product.create! name: 'c', shop_id: x.id
87
- Product.create! name: 'D', shop_id: y.id
88
- Product.create! name: 'e', shop_id: x.id
89
- Product.create! name: 'f', shop_id: x.id, active: false
90
-
91
- expect(Product.sorted_by(nil).pluck(:name)).to eq(%w[c e f a B D])
92
- expect(Product.where(active: true).sorted_by(nil).pluck(:name)).to eq(%w[c e a D])
93
- expect(Product.sorted_by('name').pluck(:name)).to eq(%w[a B c D e f])
94
- end
95
- 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