filterable-by 0.4.0 → 0.4.6

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
  SHA1:
3
- metadata.gz: e533d4d75b6d63e5fb7618d137ea0d3c74b33556
4
- data.tar.gz: c2b26115a0d7c1a035230914d54aa47fa9d4de16
3
+ metadata.gz: 4e6e0dec80e50b96215a9ca532e623ce4607baa4
4
+ data.tar.gz: 550ac202cfcbe7796ec880a5e7be5b3b01295a25
5
5
  SHA512:
6
- metadata.gz: 5e3720c7264c8ca31db45f5393778f3f3795c655e5f440bc5e49cda15bdf7c50988e1bccf48ed7871b3fc80d286b4181bf8f3c0228a854acb987e76ef32b4655
7
- data.tar.gz: 78c319fb3bd03c79094f72268dfaa589930985809e5ff2bcd524420980e01f30ebd2fb9e10e2174e2dee93fec50dcf2c499e0d27bde165256ff044a0b451e0c4
6
+ metadata.gz: e14820b29652c048576fb26a937728005a33de161b19671214127166e024b08aa3b7878d1f16977966b246c35f8aa0cbd2eec9c7c9967579a06b7790dda70e36
7
+ data.tar.gz: 668d68342c9ae54b22b390a09749bc83a5269c9d527b327af407c7ffa8d45c2134be2964ccc6f316483b978a4264e52e495487c832a98fae35dc8db0dfaf171d
data/Gemfile.lock CHANGED
@@ -1,61 +1,47 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- filterable-by (0.4.0)
4
+ filterable-by (0.4.6)
5
5
  activerecord
6
6
  activesupport
7
7
 
8
8
  GEM
9
9
  remote: http://rubygems.org/
10
10
  specs:
11
- activemodel (4.2.4)
12
- activesupport (= 4.2.4)
11
+ activemodel (4.2.6)
12
+ activesupport (= 4.2.6)
13
13
  builder (~> 3.1)
14
- activerecord (4.2.4)
15
- activemodel (= 4.2.4)
16
- activesupport (= 4.2.4)
14
+ activerecord (4.2.6)
15
+ activemodel (= 4.2.6)
16
+ activesupport (= 4.2.6)
17
17
  arel (~> 6.0)
18
- activesupport (4.2.4)
18
+ activesupport (4.2.6)
19
19
  i18n (~> 0.7)
20
20
  json (~> 1.7, >= 1.7.7)
21
21
  minitest (~> 5.1)
22
22
  thread_safe (~> 0.3, >= 0.3.4)
23
23
  tzinfo (~> 1.1)
24
24
  arel (6.0.3)
25
- ast (2.0.0)
26
- astrolabe (1.3.0)
27
- parser (>= 2.2.0.pre.3, < 3.0)
28
25
  builder (3.2.2)
29
26
  diff-lcs (1.2.5)
30
27
  i18n (0.7.0)
31
28
  json (1.8.3)
32
- minitest (5.8.2)
33
- parser (2.2.2.5)
34
- ast (>= 1.1, < 3.0)
35
- powerpack (0.1.1)
36
- rainbow (2.0.0)
37
- rake (10.4.2)
38
- rspec (3.3.0)
39
- rspec-core (~> 3.3.0)
40
- rspec-expectations (~> 3.3.0)
41
- rspec-mocks (~> 3.3.0)
42
- rspec-core (3.3.2)
43
- rspec-support (~> 3.3.0)
44
- rspec-expectations (3.3.1)
29
+ minitest (5.9.0)
30
+ rake (11.1.2)
31
+ rspec (3.4.0)
32
+ rspec-core (~> 3.4.0)
33
+ rspec-expectations (~> 3.4.0)
34
+ rspec-mocks (~> 3.4.0)
35
+ rspec-core (3.4.4)
36
+ rspec-support (~> 3.4.0)
37
+ rspec-expectations (3.4.0)
45
38
  diff-lcs (>= 1.2.0, < 2.0)
46
- rspec-support (~> 3.3.0)
47
- rspec-mocks (3.3.2)
39
+ rspec-support (~> 3.4.0)
40
+ rspec-mocks (3.4.1)
48
41
  diff-lcs (>= 1.2.0, < 2.0)
49
- rspec-support (~> 3.3.0)
50
- rspec-support (3.3.0)
51
- rubocop (0.32.0)
52
- astrolabe (~> 1.3)
53
- parser (>= 2.2.2.5, < 3.0)
54
- powerpack (~> 0.1)
55
- rainbow (>= 1.99.1, < 3.0)
56
- ruby-progressbar (~> 1.4)
57
- ruby-progressbar (1.7.5)
58
- sqlite3 (1.3.10)
42
+ rspec-support (~> 3.4.0)
43
+ rspec-support (3.4.1)
44
+ sqlite3 (1.3.11)
59
45
  thread_safe (0.3.5)
60
46
  tzinfo (1.2.2)
61
47
  thread_safe (~> 0.1)
@@ -68,8 +54,7 @@ DEPENDENCIES
68
54
  filterable-by!
69
55
  rake
70
56
  rspec
71
- rubocop
72
57
  sqlite3
73
58
 
74
59
  BUNDLED WITH
75
- 1.10.6
60
+ 1.11.2
data/Rakefile CHANGED
@@ -1,9 +1,7 @@
1
1
  require 'bundler/setup'
2
2
  require 'bundler/gem_tasks'
3
3
  require 'rspec/core/rake_task'
4
- require 'rubocop/rake_task'
5
4
 
6
5
  RSpec::Core::RakeTask.new(:spec)
7
- RuboCop::RakeTask.new(:rubocop)
8
6
 
9
- task default: [:spec, :rubocop]
7
+ task default: [:spec]
@@ -1,7 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  Gem::Specification.new do |s|
3
3
  s.name = 'filterable-by'
4
- s.version = '0.4.0'
4
+ s.version = '0.4.6'
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'
@@ -20,6 +20,5 @@ Gem::Specification.new do |s|
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'
24
23
  s.add_development_dependency 'sqlite3'
25
24
  end
data/lib/filterable_by.rb CHANGED
@@ -6,9 +6,13 @@ module ActiveRecord
6
6
  module FilterableByHelper
7
7
  extend ActiveSupport::Concern
8
8
 
9
- included do
10
- class_attribute :_filterable_by_scope_options, instance_accessor: false
11
- self._filterable_by_scope_options = {}
9
+ def self.normalize(value)
10
+ case value
11
+ when String, Numeric
12
+ value
13
+ when Array
14
+ value.select { |v| normalize(v) }
15
+ end
12
16
  end
13
17
 
14
18
  module ClassMethods
@@ -28,14 +32,20 @@ module ActiveRecord
28
32
  _filterable_by_scope_options.each do |name, block|
29
33
  next unless hash.key?(name)
30
34
 
31
- value = hash[name]
32
- next unless (value.is_a?(String) && value.present?) || value.is_a?(Numeric)
35
+ value = FilterableByHelper.normalize(hash[name])
36
+ next if value.blank?
33
37
 
34
38
  scope = block.call(scope, value)
35
39
  end
36
40
  scope
37
41
  end
38
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
+
39
49
  end
40
50
  end
41
51
 
@@ -9,7 +9,9 @@ describe ActiveRecord::FilterableByHelper do
9
9
  let(:bpost) { POSTS[:bobs] }
10
10
 
11
11
  it 'should have config' do
12
- expect(Comment._filterable_by_scope_options.size).to eq(3)
12
+ expect(Comment.send(:_filterable_by_scope_options).count).to eq(3)
13
+ expect(Rating.send(:_filterable_by_scope_options).count).to eq(2)
14
+ expect(Post.send(:_filterable_by_scope_options).count).to eq(1)
13
15
  end
14
16
 
15
17
  it 'should ignore bad inputs' do
@@ -23,12 +25,18 @@ describe ActiveRecord::FilterableByHelper do
23
25
  it 'should generate simple scopes' do
24
26
  expect(Comment.filter_by('author_id' => alice.id).pluck(:title)).to match_array(['AA', 'AB'])
25
27
  expect(Comment.filter_by('author_id' => bob.id).pluck(:title)).to match_array(['BA', 'BB'])
28
+ expect(Comment.filter_by('author_id' => [alice.id, '']).pluck(:title)).to match_array(['AA', 'AB'])
26
29
 
27
30
  expect(Comment.filter_by('post_id' => apost.id).pluck(:title)).to match_array(['AA', 'BA'])
28
31
  expect(Comment.filter_by('post_id' => bpost.id).pluck(:title)).to match_array(['AB', 'BB'])
29
32
 
30
33
  expect(Comment.filter_by('post_author_id' => alice.id).pluck(:title)).to match_array(['AA', 'BA'])
31
34
  expect(Comment.filter_by('post_author_id' => bob.id).pluck(:title)).to match_array(['AB', 'BB'])
35
+
36
+ expect(Rating.filter_by('author_id' => alice.id).count).to eq(0)
37
+ expect(Rating.filter_by('author_id' => bob.id).count).to eq(1)
38
+
39
+ expect(Post.filter_by('author_id' => bob.id).count).to eq(1)
32
40
  end
33
41
 
34
42
  it 'should generate combined scopes' do
@@ -36,6 +44,9 @@ describe ActiveRecord::FilterableByHelper do
36
44
  expect(Comment.filter_by('author_id' => alice.id, 'post_id' => bpost.id).pluck(:title)).to match_array(['AB'])
37
45
  expect(Comment.filter_by('author_id' => bob.id, 'post_id' => apost.id).pluck(:title)).to match_array(['BA'])
38
46
  expect(Comment.filter_by('author_id' => bob.id, 'post_id' => bpost.id).pluck(:title)).to match_array(['BB'])
47
+
48
+ scope = Comment.filter_by('author_id' => [alice.id, bob.id], 'post_id' => bpost.id)
49
+ expect(scope.pluck(:title)).to match_array(['AB', 'BB'])
39
50
  end
40
51
 
41
52
  it 'should combine with other scopes' do
@@ -43,4 +54,10 @@ describe ActiveRecord::FilterableByHelper do
43
54
  expect(scope.pluck(:title)).to match_array(['AA'])
44
55
  end
45
56
 
57
+ it 'should ignore invalid scopes' do
58
+ expect(Comment.filter_by('invalid' => 1).count).to eq(4)
59
+ expect(Post.filter_by('post_id' => bpost.id).count).to eq(2)
60
+ expect(Rating.filter_by('post_author_id' => bob.id).count).to eq(1)
61
+ end
62
+
46
63
  end
data/spec/spec_helper.rb CHANGED
@@ -11,8 +11,10 @@ ActiveRecord::Base.connection.instance_eval do
11
11
  create_table :posts do |t|
12
12
  t.integer :author_id, null: false
13
13
  end
14
- create_table :comments do |t|
15
- t.string :title, null: false
14
+ create_table :feedbacks do |t|
15
+ t.string :type, null: false
16
+ t.string :title
17
+ t.integer :stars, null: false, default: 0
16
18
  t.integer :post_id, null: false
17
19
  t.integer :author_id, null: false
18
20
  end
@@ -23,18 +25,26 @@ end
23
25
 
24
26
  class Post < ActiveRecord::Base
25
27
  belongs_to :author
28
+
29
+ filterable_by :author_id
26
30
  end
27
31
 
28
- class Comment < ActiveRecord::Base
32
+ class Feedback < ActiveRecord::Base
29
33
  belongs_to :author
30
34
  belongs_to :post
31
35
 
32
36
  filterable_by :post_id, :author_id
37
+ end
38
+
39
+ class Comment < Feedback
33
40
  filterable_by :post_author_id do |scope, value|
34
41
  scope.joins(:post).where(Post.arel_table[:author_id].eq(value))
35
42
  end
36
43
  end
37
44
 
45
+ class Rating < Feedback
46
+ end
47
+
38
48
  AUTHORS = {
39
49
  alice: Author.create!,
40
50
  bob: Author.create!,
@@ -50,4 +60,5 @@ COMMENTS = {
50
60
  bob_on_alice: Comment.create!(title: 'BA', post_id: POSTS[:alices].id, author_id: AUTHORS[:bob].id),
51
61
  alice_on_bob: Comment.create!(title: 'AB', post_id: POSTS[:bobs].id, author_id: AUTHORS[:alice].id),
52
62
  bob_on_bob: Comment.create!(title: 'BB', post_id: POSTS[:bobs].id, author_id: AUTHORS[:bob].id),
63
+ boa_rating: Rating.create!(stars: 5, post_id: POSTS[:alices].id, author_id: AUTHORS[:bob].id),
53
64
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filterable-by
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dimitrij Denissenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-29 00:00:00.000000000 Z
11
+ date: 2016-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -80,20 +80,6 @@ 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'
97
83
  - !ruby/object:Gem::Dependency
98
84
  name: sqlite3
99
85
  requirement: !ruby/object:Gem::Requirement
@@ -116,8 +102,6 @@ extensions: []
116
102
  extra_rdoc_files: []
117
103
  files:
118
104
  - ".gitignore"
119
- - ".rubocop.yml"
120
- - ".rubocop_todo.yml"
121
105
  - ".travis.yml"
122
106
  - Gemfile
123
107
  - Gemfile.lock
@@ -148,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
132
  version: '0'
149
133
  requirements: []
150
134
  rubyforge_project:
151
- rubygems_version: 2.4.7
135
+ rubygems_version: 2.5.1
152
136
  signing_key:
153
137
  specification_version: 4
154
138
  summary: Generate white-listed filter scopes from URL parameter values
data/.rubocop.yml DELETED
@@ -1,7 +0,0 @@
1
- inherit_from: .rubocop_todo.yml
2
-
3
- Style/Documentation:
4
- Enabled: false
5
-
6
- Metrics/LineLength:
7
- Max: 120
data/.rubocop_todo.yml DELETED
@@ -1,40 +0,0 @@
1
- # This configuration was generated by `rubocop --auto-gen-config`
2
- # on 2015-10-29 12:58:41 +0000 using RuboCop version 0.32.0.
3
- # The point is for the user to remove these configuration records
4
- # one by one as the offenses are removed from the code base.
5
- # Note that changes in the inspected code, or installation of new
6
- # versions of RuboCop, may require this file to be generated again.
7
-
8
- # Offense count: 2
9
- # Cop supports --auto-correct.
10
- # Configuration parameters: EnforcedStyle, SupportedStyles.
11
- Style/EmptyLinesAroundBlockBody:
12
- Enabled: false
13
-
14
- # Offense count: 2
15
- # Cop supports --auto-correct.
16
- # Configuration parameters: EnforcedStyle, SupportedStyles.
17
- Style/EmptyLinesAroundModuleBody:
18
- Enabled: false
19
-
20
- # Offense count: 1
21
- # Configuration parameters: Exclude.
22
- Style/FileName:
23
- Enabled: false
24
-
25
- # Offense count: 1
26
- # Cop supports --auto-correct.
27
- Style/SingleSpaceBeforeFirstArg:
28
- Enabled: false
29
-
30
- # Offense count: 3
31
- # Cop supports --auto-correct.
32
- # Configuration parameters: EnforcedStyleForMultiline, SupportedStyles.
33
- Style/TrailingComma:
34
- Enabled: false
35
-
36
- # Offense count: 6
37
- # Cop supports --auto-correct.
38
- # Configuration parameters: WordRegex.
39
- Style/WordArray:
40
- MinSize: 2