where_conditioner 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/where_conditioner.rb +20 -1
- data/spec/lib/where_conditioner_spec.rb +5 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cadede51475344b93a3d88c4220b047430a5668a
|
4
|
+
data.tar.gz: 386286509e4c3fee25ec8b5c5b5ea39b14aca939
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0c81ade509e725b5e2c1a22fb0eac3572f522c3e33a9efed92c96ccca2d41e50141c23b30c2b1aadf4ff35d8006fe368a2c91ef7f229467201a7530dc472ee53
|
7
|
+
data.tar.gz: ac5d175acabfb3722c2287d6aa1eade21ee40733c63d593d0dae1d9b08318b85ed0789cb9592b1ae0d4dd4bb9c3cfe17c820aeecab21ba566aaeb8ce95f85085
|
data/lib/where_conditioner.rb
CHANGED
@@ -32,7 +32,7 @@ module WhereConditioner
|
|
32
32
|
|
33
33
|
def where_if_present *args
|
34
34
|
if Hash === args.first && args.length == 1
|
35
|
-
hash = args.first
|
35
|
+
hash = WhereConditioner.recursive_compact(args.first)
|
36
36
|
if hash.present?
|
37
37
|
self.where hash
|
38
38
|
else
|
@@ -65,6 +65,25 @@ private
|
|
65
65
|
end
|
66
66
|
end
|
67
67
|
end
|
68
|
+
|
69
|
+
def self.recursive_compact(hash)
|
70
|
+
hash.dup.tap do |hash|
|
71
|
+
hash.each do |key,value|
|
72
|
+
case value
|
73
|
+
when nil
|
74
|
+
hash.delete key
|
75
|
+
when Hash
|
76
|
+
compacted = recursive_compact(value)
|
77
|
+
if compacted.empty?
|
78
|
+
hash.delete key
|
79
|
+
else
|
80
|
+
hash[key] = compacted
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
68
87
|
end
|
69
88
|
|
70
89
|
module ActiveRecord
|
@@ -22,6 +22,11 @@ describe WhereConditioner do
|
|
22
22
|
relation.where_if_present(key1: '', key2: nil, key3: 'value', key4: 0)
|
23
23
|
end
|
24
24
|
|
25
|
+
it 'filters out nils in nested hashes' do
|
26
|
+
expect(relation).to receive(:where).with(table1: { key1: '', key3: 'value', key4: 0 }, table3: { key6: 'value' })
|
27
|
+
relation.where_if_present(table1: {key1: '', key2: nil, key3: 'value', key4: 0}, table2: {key5: nil}, table3: {key6: 'value'})
|
28
|
+
end
|
29
|
+
|
25
30
|
it 'does not call where(), and returns self, if all values are nil' do
|
26
31
|
expect(relation).not_to receive(:where)
|
27
32
|
relation.where_if_present(key1: nil, key2: nil)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: where_conditioner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tony Novak
|
@@ -72,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
72
|
version: '0'
|
73
73
|
requirements: []
|
74
74
|
rubyforge_project:
|
75
|
-
rubygems_version: 2.2.
|
75
|
+
rubygems_version: 2.2.1
|
76
76
|
signing_key:
|
77
77
|
specification_version: 4
|
78
78
|
summary: Where Conditioner
|