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 +4 -4
- data/Gemfile.lock +1 -1
- data/filterable-by.gemspec +1 -1
- data/lib/filterable_by.rb +41 -26
- data/spec/spec_helper.rb +5 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 12ba86a11c254ce3ce2a03cafd2b39f75f2b6df80d6899d05ef160dd98c07c96
|
4
|
+
data.tar.gz: 916d29d238b9ae4c14d559cd880b51f8d0f6063fd66cd27470756bd2e38bea43
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 70df7b4aa8a04fd6b820a63658357186185d5ecbfa3a63c9f9a45e9bbdffba4aa8a0d4ec9fca6aadc43d122fb6a298086dd1cf3fc7a692961cfd6642f6a426aa
|
7
|
+
data.tar.gz: 1ce3a1f26079fbafc6cba6d516930adacbf69bcb0523d0431692750f9158058b2df96af5aa8e16d8bcd4138e76d06bec94ae032464c00aeb9178c8909ccefce9
|
data/Gemfile.lock
CHANGED
data/filterable-by.gemspec
CHANGED
data/lib/filterable_by.rb
CHANGED
@@ -4,40 +4,56 @@ require 'set'
|
|
4
4
|
|
5
5
|
module ActiveRecord
|
6
6
|
module FilterableBy
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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
|
-
|
33
|
+
scope = scope.merge(invert_where(sub))
|
34
|
+
end
|
35
|
+
|
36
|
+
scope
|
24
37
|
end
|
25
38
|
|
26
|
-
|
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
|
-
|
33
|
-
|
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
|
-
|
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.
|
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-
|
11
|
+
date: 2022-04-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|