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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c44ce09a4fe3727fe41e7db71d8dc99635964804
4
- data.tar.gz: 8ee269cec9b22ef62b15669b4b61a1f4ed03aeee
3
+ metadata.gz: cadede51475344b93a3d88c4220b047430a5668a
4
+ data.tar.gz: 386286509e4c3fee25ec8b5c5b5ea39b14aca939
5
5
  SHA512:
6
- metadata.gz: c266e1cb927721587d933c59b39461acffa37c854052d45785cb2e5899a55b8d091157ed275bf3ea7965539b65df433f4f29433d9efefd80dfc97b69fb88403c
7
- data.tar.gz: 8797fabf41f090281b204ed1df708f76458334ae49ec23e44595c914f120765c410a7c0b620e5b2dccb44ff294e353960715722046c6ab2738de709f394dda3d
6
+ metadata.gz: 0c81ade509e725b5e2c1a22fb0eac3572f522c3e33a9efed92c96ccca2d41e50141c23b30c2b1aadf4ff35d8006fe368a2c91ef7f229467201a7530dc472ee53
7
+ data.tar.gz: ac5d175acabfb3722c2287d6aa1eade21ee40733c63d593d0dae1d9b08318b85ed0789cb9592b1ae0d4dd4bb9c3cfe17c820aeecab21ba566aaeb8ce95f85085
@@ -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.compact
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.0
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.2
75
+ rubygems_version: 2.2.1
76
76
  signing_key:
77
77
  specification_version: 4
78
78
  summary: Where Conditioner