filterable-by 0.6.2 → 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a92cb2cde41910a93ed81348238f61031c3e5c8071ab2b118bfceb25307a8c14
4
- data.tar.gz: 231b7712b1fe257b34ab63d8a3dc362304c45318984c3eb1a2c8cf004ef2ee44
3
+ metadata.gz: 12ba86a11c254ce3ce2a03cafd2b39f75f2b6df80d6899d05ef160dd98c07c96
4
+ data.tar.gz: 916d29d238b9ae4c14d559cd880b51f8d0f6063fd66cd27470756bd2e38bea43
5
5
  SHA512:
6
- metadata.gz: 8ca4f69341eaa58f296280b703398c5ce6ad1dcd6551758322c36a03db7f2b9af7e37defeb96c205a5a1520f40e154d4ebd2f5c6110591ccb1505f55f99ac870
7
- data.tar.gz: 10691ed7dc8e33b7c6920e8a8e6e670b854bc51d6155baa0923d0cfe363ec2fb8824ed79f3cc635cc9b68f824e3c1951ea32cda8e320e0f40b5ad12fbfa37c2f
6
+ metadata.gz: 70df7b4aa8a04fd6b820a63658357186185d5ecbfa3a63c9f9a45e9bbdffba4aa8a0d4ec9fca6aadc43d122fb6a298086dd1cf3fc7a692961cfd6642f6a426aa
7
+ data.tar.gz: 1ce3a1f26079fbafc6cba6d516930adacbf69bcb0523d0431692750f9158058b2df96af5aa8e16d8bcd4138e76d06bec94ae032464c00aeb9178c8909ccefce9
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- filterable-by (0.6.2)
4
+ filterable-by (0.6.3)
5
5
  activerecord
6
6
  activesupport
7
7
 
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'filterable-by'
3
- s.version = '0.6.2'
3
+ s.version = '0.6.3'
4
4
  s.authors = ['Dimitrij Denissenko']
5
5
  s.email = ['dimitrij@blacksquaremedia.com']
6
6
  s.summary = 'Generate white-listed filter scopes from URL parameter values'
data/lib/filterable_by.rb CHANGED
@@ -4,40 +4,56 @@ require 'set'
4
4
 
5
5
  module ActiveRecord
6
6
  module FilterableBy
7
- def self.normalize(value)
8
- case value
9
- when String, Numeric
10
- value
11
- when Array
12
- value.select {|v| normalize(v) }
7
+ class << self
8
+ def normalize(value)
9
+ case value
10
+ when String, Numeric
11
+ value
12
+ when Array
13
+ value.select {|v| normalize(v) }
14
+ end
13
15
  end
14
- end
15
16
 
16
- def self.merge(scope, unscoped, hash, name, **opts, &block)
17
- key = name
18
- positive = normalize(hash[key]) if hash.key?(key)
19
- if positive.present?
20
- sub = block.arity == 2 ? yield(unscoped, positive, **opts) : yield(positive, **opts)
21
- return nil unless sub
17
+ def merge(scope, unscoped, hash, name, **opts, &block)
18
+ key = name
19
+ positive = normalize(hash[key]) if hash.key?(key)
20
+ if positive.present?
21
+ sub = eval_scope(scope, unscoped, positive, **opts, &block)
22
+ return nil unless sub
23
+
24
+ scope = scope.merge(sub)
25
+ end
26
+
27
+ key = "#{name}_not"
28
+ negative = normalize(hash[key]) if hash.key?(key)
29
+ if negative.present?
30
+ sub = eval_scope(scope, unscoped, negative, **opts, &block)
31
+ return nil unless sub
22
32
 
23
- scope = scope.merge(sub)
33
+ scope = scope.merge(invert_where(sub))
34
+ end
35
+
36
+ scope
24
37
  end
25
38
 
26
- key = "#{name}_not"
27
- negative = normalize(hash[key]) if hash.key?(key)
28
- if negative.present?
29
- sub = block.arity == 2 ? yield(unscoped, negative, **opts) : yield(negative, **opts)
30
- return nil unless sub
39
+ private
31
40
 
32
- if sub.respond_to?(:invert_where)
33
- sub = sub.invert_where
41
+ def eval_scope(scope, unscoped, value, **opts, &block)
42
+ if block.arity == 2
43
+ scope.instance_exec(unscoped, value, **opts, &block)
34
44
  else
35
- sub.where_clause = sub.where_clause.invert
45
+ scope.instance_exec(value, **opts, &block)
36
46
  end
37
- scope = scope.merge(sub)
38
47
  end
39
48
 
40
- scope
49
+ def invert_where(scope)
50
+ if scope.respond_to?(:invert_where!)
51
+ scope.invert_where!
52
+ else
53
+ scope.where_clause = scope.where_clause.invert
54
+ end
55
+ scope
56
+ end
41
57
  end
42
58
 
43
59
  module ClassMethods
@@ -58,7 +74,7 @@ module ActiveRecord
58
74
  end
59
75
 
60
76
  names.each do |name|
61
- _filterable_by_config[name.to_s] = block
77
+ _filterable_by_config[name.to_s] = block || ->(value, **) { where(name.to_sym => value) }
62
78
  end
63
79
  end
64
80
 
@@ -74,7 +90,6 @@ module ActiveRecord
74
90
  return scope unless hash.respond_to?(:key?) && hash.respond_to?(:[])
75
91
 
76
92
  _filterable_by_config.each do |name, block|
77
- block = ->(value, **) { where(name.to_sym => value) } if block.nil?
78
93
  scope = FilterableBy.merge(scope, unscoped, hash, name, **opts, &block)
79
94
  break unless scope
80
95
  end
data/spec/spec_helper.rb CHANGED
@@ -27,12 +27,8 @@ end
27
27
 
28
28
  class AbstractPost < ActiveRecord::Base
29
29
  self.abstract_class = true
30
- filterable_by :author_id
31
- end
32
-
33
- class Post < AbstractPost
34
- belongs_to :author
35
30
 
31
+ filterable_by :author_id
36
32
  filterable_by :only do |value, **opts|
37
33
  case value
38
34
  when 'me'
@@ -43,6 +39,10 @@ class Post < AbstractPost
43
39
  end
44
40
  end
45
41
 
42
+ class Post < AbstractPost
43
+ belongs_to :author
44
+ end
45
+
46
46
  class Feedback < ActiveRecord::Base
47
47
  belongs_to :author
48
48
  belongs_to :post
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.6.2
4
+ version: 0.6.3
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-04-05 00:00:00.000000000 Z
11
+ date: 2022-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord