filterable-by 0.4.6 → 0.4.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Gemfile.lock +48 -34
- data/README.md +0 -25
- data/Rakefile +3 -1
- data/filterable-by.gemspec +5 -4
- data/lib/filterable_by.rb +15 -14
- data/spec/filterable_by_spec.rb +12 -14
- data/spec/spec_helper.rb +3 -3
- metadata +20 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7c170888a73dbeca47de324efe3056b63112936dd4b0ebd4b5952b44a48727a0
|
4
|
+
data.tar.gz: d63ac1513cb002bc12ba106abe5f99c2e90f91c9d5a8696397a52e8f5bf7c732
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01e287700b6bec04c0a53319de330b113cad3466d9a00288206ed0e60c9fbc35de8cb6089d976590dfe808b7b9e06e344b0d1378a1f60a7fb232de8f9430019b
|
7
|
+
data.tar.gz: 447d4907f3e7876d878d4fdb8a070214cd6804dfa40c67277639f172ba8a17b067d48f7ae96d1c43d90734d60e0dd8287aab5d1fb52e73de2779f9d4bba72a46
|
data/Gemfile.lock
CHANGED
@@ -1,50 +1,63 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
filterable-by (0.4.
|
4
|
+
filterable-by (0.4.7)
|
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
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
i18n (
|
20
|
-
json (~> 1.7, >= 1.7.7)
|
11
|
+
activemodel (5.2.0)
|
12
|
+
activesupport (= 5.2.0)
|
13
|
+
activerecord (5.2.0)
|
14
|
+
activemodel (= 5.2.0)
|
15
|
+
activesupport (= 5.2.0)
|
16
|
+
arel (>= 9.0)
|
17
|
+
activesupport (5.2.0)
|
18
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
19
|
+
i18n (>= 0.7, < 2)
|
21
20
|
minitest (~> 5.1)
|
22
|
-
thread_safe (~> 0.3, >= 0.3.4)
|
23
21
|
tzinfo (~> 1.1)
|
24
|
-
arel (
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
rspec
|
22
|
+
arel (9.0.0)
|
23
|
+
ast (2.4.0)
|
24
|
+
concurrent-ruby (1.0.5)
|
25
|
+
diff-lcs (1.3)
|
26
|
+
i18n (1.0.1)
|
27
|
+
concurrent-ruby (~> 1.0)
|
28
|
+
minitest (5.11.3)
|
29
|
+
parallel (1.12.1)
|
30
|
+
parser (2.5.1.0)
|
31
|
+
ast (~> 2.4.0)
|
32
|
+
powerpack (0.1.1)
|
33
|
+
rainbow (3.0.0)
|
34
|
+
rake (12.3.1)
|
35
|
+
rspec (3.7.0)
|
36
|
+
rspec-core (~> 3.7.0)
|
37
|
+
rspec-expectations (~> 3.7.0)
|
38
|
+
rspec-mocks (~> 3.7.0)
|
39
|
+
rspec-core (3.7.1)
|
40
|
+
rspec-support (~> 3.7.0)
|
41
|
+
rspec-expectations (3.7.0)
|
38
42
|
diff-lcs (>= 1.2.0, < 2.0)
|
39
|
-
rspec-support (~> 3.
|
40
|
-
rspec-mocks (3.
|
43
|
+
rspec-support (~> 3.7.0)
|
44
|
+
rspec-mocks (3.7.0)
|
41
45
|
diff-lcs (>= 1.2.0, < 2.0)
|
42
|
-
rspec-support (~> 3.
|
43
|
-
rspec-support (3.
|
44
|
-
|
45
|
-
|
46
|
-
|
46
|
+
rspec-support (~> 3.7.0)
|
47
|
+
rspec-support (3.7.1)
|
48
|
+
rubocop (0.55.0)
|
49
|
+
parallel (~> 1.10)
|
50
|
+
parser (>= 2.5)
|
51
|
+
powerpack (~> 0.1)
|
52
|
+
rainbow (>= 2.2.2, < 4.0)
|
53
|
+
ruby-progressbar (~> 1.7)
|
54
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
55
|
+
ruby-progressbar (1.9.0)
|
56
|
+
sqlite3 (1.3.13)
|
57
|
+
thread_safe (0.3.6)
|
58
|
+
tzinfo (1.2.5)
|
47
59
|
thread_safe (~> 0.1)
|
60
|
+
unicode-display_width (1.3.2)
|
48
61
|
|
49
62
|
PLATFORMS
|
50
63
|
ruby
|
@@ -54,7 +67,8 @@ DEPENDENCIES
|
|
54
67
|
filterable-by!
|
55
68
|
rake
|
56
69
|
rspec
|
70
|
+
rubocop
|
57
71
|
sqlite3
|
58
72
|
|
59
73
|
BUNDLED WITH
|
60
|
-
1.
|
74
|
+
1.16.1
|
data/README.md
CHANGED
@@ -35,28 +35,3 @@ Comment.filter_by({ "user_id" => "2", "ignored" => "3" })
|
|
35
35
|
Comment.filter_by({ "post_author_id" => "5" })
|
36
36
|
# => JOINS posts ON posts.id = comments.post_id WHERE posts.author_id = 5
|
37
37
|
```
|
38
|
-
|
39
|
-
## LICENCE
|
40
|
-
|
41
|
-
```
|
42
|
-
Copyright (c) 2015 Black Square Media
|
43
|
-
|
44
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
45
|
-
a copy of this software and associated documentation files (the
|
46
|
-
"Software"), to deal in the Software without restriction, including
|
47
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
48
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
49
|
-
permit persons to whom the Software is furnished to do so, subject to
|
50
|
-
the following conditions:
|
51
|
-
|
52
|
-
The above copyright notice and this permission notice shall be
|
53
|
-
included in all copies or substantial portions of the Software.
|
54
|
-
|
55
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
56
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
57
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
58
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
59
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
60
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
61
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
62
|
-
```
|
data/Rakefile
CHANGED
data/filterable-by.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
|
1
|
+
|
2
2
|
Gem::Specification.new do |s|
|
3
3
|
s.name = 'filterable-by'
|
4
|
-
s.version = '0.4.
|
4
|
+
s.version = '0.4.7'
|
5
5
|
s.authors = ['Dimitrij Denissenko']
|
6
6
|
s.email = ['dimitrij@blacksquaremedia.com']
|
7
7
|
s.summary = 'Generate white-listed filter scopes from URL parameter values'
|
@@ -12,13 +12,14 @@ Gem::Specification.new do |s|
|
|
12
12
|
s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^spec/}) }
|
13
13
|
s.test_files = `git ls-files -z -- spec/*`.split("\x0")
|
14
14
|
s.require_paths = ['lib']
|
15
|
-
s.required_ruby_version = '>=
|
15
|
+
s.required_ruby_version = '>= 2.2'
|
16
16
|
|
17
|
-
s.add_dependency 'activesupport'
|
18
17
|
s.add_dependency 'activerecord'
|
18
|
+
s.add_dependency 'activesupport'
|
19
19
|
|
20
20
|
s.add_development_dependency 'bundler'
|
21
21
|
s.add_development_dependency 'rake'
|
22
22
|
s.add_development_dependency 'rspec'
|
23
|
+
s.add_development_dependency 'rubocop'
|
23
24
|
s.add_development_dependency 'sqlite3'
|
24
25
|
end
|
data/lib/filterable_by.rb
CHANGED
@@ -3,9 +3,7 @@ require 'active_support/concern'
|
|
3
3
|
require 'set'
|
4
4
|
|
5
5
|
module ActiveRecord
|
6
|
-
module
|
7
|
-
extend ActiveSupport::Concern
|
8
|
-
|
6
|
+
module FilterableBy
|
9
7
|
def self.normalize(value)
|
10
8
|
case value
|
11
9
|
when String, Numeric
|
@@ -16,10 +14,20 @@ module ActiveRecord
|
|
16
14
|
end
|
17
15
|
|
18
16
|
module ClassMethods
|
17
|
+
def self.extended(base) # :nodoc:
|
18
|
+
base.class_attribute :_filterable_by_config, instance_accessor: false, instance_predicate: false
|
19
|
+
base._filterable_by_config = {}
|
20
|
+
super
|
21
|
+
end
|
22
|
+
|
23
|
+
def inherited(base) # :nodoc:
|
24
|
+
base._filterable_by_config = _filterable_by_config.deep_dup
|
25
|
+
super
|
26
|
+
end
|
19
27
|
|
20
28
|
def filterable_by(*names, &block)
|
21
29
|
names.each do |name|
|
22
|
-
|
30
|
+
_filterable_by_config[name.to_s] = block || ->(scope, v) { scope.where(name.to_sym => v) }
|
23
31
|
end
|
24
32
|
end
|
25
33
|
|
@@ -29,27 +37,20 @@ module ActiveRecord
|
|
29
37
|
scope = all
|
30
38
|
return scope unless hash.is_a?(Hash)
|
31
39
|
|
32
|
-
|
40
|
+
_filterable_by_config.each do |name, block|
|
33
41
|
next unless hash.key?(name)
|
34
42
|
|
35
|
-
value =
|
43
|
+
value = FilterableBy.normalize(hash[name])
|
36
44
|
next if value.blank?
|
37
45
|
|
38
46
|
scope = block.call(scope, value)
|
39
47
|
end
|
40
48
|
scope
|
41
49
|
end
|
42
|
-
|
43
|
-
protected
|
44
|
-
|
45
|
-
def _filterable_by_scope_options
|
46
|
-
@_filterable_by_scope_options ||= superclass == Object ? {} : superclass.send(:_filterable_by_scope_options).dup
|
47
|
-
end
|
48
|
-
|
49
50
|
end
|
50
51
|
end
|
51
52
|
|
52
53
|
class Base
|
53
|
-
|
54
|
+
extend FilterableBy::ClassMethods
|
54
55
|
end
|
55
56
|
end
|
data/spec/filterable_by_spec.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/spec_helper'
|
2
2
|
|
3
|
-
describe ActiveRecord::
|
4
|
-
|
3
|
+
describe ActiveRecord::FilterableBy do
|
5
4
|
let(:alice) { AUTHORS[:alice] }
|
6
5
|
let(:bob) { AUTHORS[:bob] }
|
7
6
|
|
@@ -9,9 +8,9 @@ describe ActiveRecord::FilterableByHelper do
|
|
9
8
|
let(:bpost) { POSTS[:bobs] }
|
10
9
|
|
11
10
|
it 'should have config' do
|
12
|
-
expect(Comment.send(:
|
13
|
-
expect(Rating.send(:
|
14
|
-
expect(Post.send(:
|
11
|
+
expect(Comment.send(:_filterable_by_config).count).to eq(3)
|
12
|
+
expect(Rating.send(:_filterable_by_config).count).to eq(2)
|
13
|
+
expect(Post.send(:_filterable_by_config).count).to eq(1)
|
15
14
|
end
|
16
15
|
|
17
16
|
it 'should ignore bad inputs' do
|
@@ -23,15 +22,15 @@ describe ActiveRecord::FilterableByHelper do
|
|
23
22
|
end
|
24
23
|
|
25
24
|
it 'should generate simple scopes' do
|
26
|
-
expect(Comment.filter_by('author_id' => alice.id).pluck(:title)).to match_array([
|
27
|
-
expect(Comment.filter_by('author_id' => bob.id).pluck(:title)).to match_array([
|
28
|
-
expect(Comment.filter_by('author_id' => [alice.id, '']).pluck(:title)).to match_array([
|
25
|
+
expect(Comment.filter_by('author_id' => alice.id).pluck(:title)).to match_array(%w[AA AB])
|
26
|
+
expect(Comment.filter_by('author_id' => bob.id).pluck(:title)).to match_array(%w[BA BB])
|
27
|
+
expect(Comment.filter_by('author_id' => [alice.id, '']).pluck(:title)).to match_array(%w[AA AB])
|
29
28
|
|
30
|
-
expect(Comment.filter_by('post_id' => apost.id).pluck(:title)).to match_array([
|
31
|
-
expect(Comment.filter_by('post_id' => bpost.id).pluck(:title)).to match_array([
|
29
|
+
expect(Comment.filter_by('post_id' => apost.id).pluck(:title)).to match_array(%w[AA BA])
|
30
|
+
expect(Comment.filter_by('post_id' => bpost.id).pluck(:title)).to match_array(%w[AB BB])
|
32
31
|
|
33
|
-
expect(Comment.filter_by('post_author_id' => alice.id).pluck(:title)).to match_array([
|
34
|
-
expect(Comment.filter_by('post_author_id' => bob.id).pluck(:title)).to match_array([
|
32
|
+
expect(Comment.filter_by('post_author_id' => alice.id).pluck(:title)).to match_array(%w[AA BA])
|
33
|
+
expect(Comment.filter_by('post_author_id' => bob.id).pluck(:title)).to match_array(%w[AB BB])
|
35
34
|
|
36
35
|
expect(Rating.filter_by('author_id' => alice.id).count).to eq(0)
|
37
36
|
expect(Rating.filter_by('author_id' => bob.id).count).to eq(1)
|
@@ -46,7 +45,7 @@ describe ActiveRecord::FilterableByHelper do
|
|
46
45
|
expect(Comment.filter_by('author_id' => bob.id, 'post_id' => bpost.id).pluck(:title)).to match_array(['BB'])
|
47
46
|
|
48
47
|
scope = Comment.filter_by('author_id' => [alice.id, bob.id], 'post_id' => bpost.id)
|
49
|
-
expect(scope.pluck(:title)).to match_array([
|
48
|
+
expect(scope.pluck(:title)).to match_array(%w[AB BB])
|
50
49
|
end
|
51
50
|
|
52
51
|
it 'should combine with other scopes' do
|
@@ -59,5 +58,4 @@ describe ActiveRecord::FilterableByHelper do
|
|
59
58
|
expect(Post.filter_by('post_id' => bpost.id).count).to eq(2)
|
60
59
|
expect(Rating.filter_by('post_author_id' => bob.id).count).to eq(1)
|
61
60
|
end
|
62
|
-
|
63
61
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -48,12 +48,12 @@ end
|
|
48
48
|
AUTHORS = {
|
49
49
|
alice: Author.create!,
|
50
50
|
bob: Author.create!,
|
51
|
-
}
|
51
|
+
}.freeze
|
52
52
|
|
53
53
|
POSTS = {
|
54
54
|
alices: Post.create!(author_id: AUTHORS[:alice].id),
|
55
55
|
bobs: Post.create!(author_id: AUTHORS[:bob].id),
|
56
|
-
}
|
56
|
+
}.freeze
|
57
57
|
|
58
58
|
COMMENTS = {
|
59
59
|
alice_on_alice: Comment.create!(title: 'AA', post_id: POSTS[:alices].id, author_id: AUTHORS[:alice].id),
|
@@ -61,4 +61,4 @@ COMMENTS = {
|
|
61
61
|
alice_on_bob: Comment.create!(title: 'AB', post_id: POSTS[:bobs].id, author_id: AUTHORS[:alice].id),
|
62
62
|
bob_on_bob: Comment.create!(title: 'BB', post_id: POSTS[:bobs].id, author_id: AUTHORS[:bob].id),
|
63
63
|
boa_rating: Rating.create!(stars: 5, post_id: POSTS[:alices].id, author_id: AUTHORS[:bob].id),
|
64
|
-
}
|
64
|
+
}.freeze
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: filterable-by
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.7
|
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: 2018-05-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
@@ -25,7 +25,7 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: activesupport
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
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'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: sqlite3
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -124,7 +138,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
124
138
|
requirements:
|
125
139
|
- - ">="
|
126
140
|
- !ruby/object:Gem::Version
|
127
|
-
version:
|
141
|
+
version: '2.2'
|
128
142
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
143
|
requirements:
|
130
144
|
- - ">="
|
@@ -132,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
132
146
|
version: '0'
|
133
147
|
requirements: []
|
134
148
|
rubyforge_project:
|
135
|
-
rubygems_version: 2.
|
149
|
+
rubygems_version: 2.7.3
|
136
150
|
signing_key:
|
137
151
|
specification_version: 4
|
138
152
|
summary: Generate white-listed filter scopes from URL parameter values
|