search_object 1.1.2 → 1.1.3

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: dfd3ff677d11a1e11bed9b477960df1e286a162d
4
- data.tar.gz: 60cabe41c79f8596ce290c52cfe88317fb28fd00
3
+ metadata.gz: 3dcf24c846042501f789fdc3da39a47b15b33874
4
+ data.tar.gz: 80448932ff7fc522f8a20890ba8ef7ecda8f4eb3
5
5
  SHA512:
6
- metadata.gz: 331c98690d13135b9a3fb0427c9a6df1139785af9df0c53755fddd1c30db4b93c48a38f094156829f4d6a419eea9ba1b381f9fcd105dabcb06f04a6a936acaf4
7
- data.tar.gz: c9d14a709fd31ad3cdafdda6ce8028edd80da3549ae6e0c0e96e4f9afa64384cf3805a258f46776a684ed19c605f7d69e472e96864d4ac30cb874ebad03f7c92
6
+ metadata.gz: 3d4139961ee6295d3abe3a987d0d224a2197b6040a30b1da252989654a1d3892c215022132ef0407a75c9d4a7c37a4cfd62447f9f345acc8702a72e5f5d7a5b9
7
+ data.tar.gz: decde34bdf851ff385715e646107091fd6c3ac4debf860268f513e1b1105c8aaa6a904ca6eee64794572f1a8956f295525276f545879f560588ebe4c0d81e8bc
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## Version 1.1.3 (unreleased)
4
+
5
+ * __[feature]__ Passing nil as `scope` in constructor, falls back to default scope (@rstankov)
6
+
3
7
  ## Version 1.1.2
4
8
 
5
9
  * __[fix]__ Fix a warning due to Rails 5 `ActionController::Parameters` not being a Hash (@rstankov)
@@ -1,63 +1,63 @@
1
1
  module SearchObject
2
2
  # :api: private
3
3
  module Helper
4
- class << self
5
- def stringify_keys(hash)
6
- # Note(rstankov): From Rails 5+ ActionController::Parameters aren't Hash
7
- # In a lot of cases `stringify_keys` is used on action params
8
- hash = hash.to_unsafe_h if hash.respond_to? :to_unsafe_h
9
- Hash[(hash || {}).map { |k, v| [k.to_s, v] }]
10
- end
4
+ module_function
11
5
 
12
- def slice_keys(hash, keys)
13
- keys.inject({}) do |memo, key|
14
- memo[key] = hash[key] if hash.key? key
15
- memo
16
- end
17
- end
6
+ def stringify_keys(hash)
7
+ # Note(rstankov): From Rails 5+ ActionController::Parameters aren't Hash
8
+ # In a lot of cases `stringify_keys` is used on action params
9
+ hash = hash.to_unsafe_h if hash.respond_to? :to_unsafe_h
10
+ Hash[(hash || {}).map { |k, v| [k.to_s, v] }]
11
+ end
18
12
 
19
- def camelize(text)
20
- text.to_s.gsub(/(?:^|_)(.)/) { Regexp.last_match[1].upcase }
13
+ def slice_keys(hash, keys)
14
+ keys.inject({}) do |memo, key|
15
+ memo[key] = hash[key] if hash.key? key
16
+ memo
21
17
  end
18
+ end
22
19
 
23
- def ensure_included(item, collection)
24
- if collection.include? item
25
- item
26
- else
27
- collection.first
28
- end
20
+ def camelize(text)
21
+ text.to_s.gsub(/(?:^|_)(.)/) { Regexp.last_match[1].upcase }
22
+ end
23
+
24
+ def ensure_included(item, collection)
25
+ if collection.include? item
26
+ item
27
+ else
28
+ collection.first
29
29
  end
30
+ end
30
31
 
31
- def define_module(&block)
32
- Module.new do
33
- define_singleton_method :included do |base|
34
- base.class_eval(&block)
35
- end
32
+ def define_module(&block)
33
+ Module.new do
34
+ define_singleton_method :included do |base|
35
+ base.class_eval(&block)
36
36
  end
37
37
  end
38
+ end
38
39
 
39
- def normalize_search_handler(handler, name)
40
- case handler
41
- when Symbol then ->(scope, value) { method(handler).call scope, value }
42
- when Proc then handler
43
- else ->(scope, value) { scope.where name => value unless value.blank? }
44
- end
40
+ def normalize_search_handler(handler, name)
41
+ case handler
42
+ when Symbol then ->(scope, value) { method(handler).call scope, value }
43
+ when Proc then handler
44
+ else ->(scope, value) { scope.where name => value unless value.blank? }
45
45
  end
46
+ end
46
47
 
47
- def deep_copy(object) # rubocop:disable Metrics/MethodLength
48
- case object
49
- when Array
50
- object.map { |element| deep_copy(element) }
51
- when Hash
52
- object.inject({}) do |result, (key, value)|
53
- result[key] = deep_copy(value)
54
- result
55
- end
56
- when NilClass, FalseClass, TrueClass, Symbol, Method, Numeric
57
- object
58
- else
59
- object.dup
48
+ def deep_copy(object) # rubocop:disable Metrics/MethodLength
49
+ case object
50
+ when Array
51
+ object.map { |element| deep_copy(element) }
52
+ when Hash
53
+ object.inject({}) do |result, (key, value)|
54
+ result[key] = deep_copy(value)
55
+ result
60
56
  end
57
+ when NilClass, FalseClass, TrueClass, Symbol, Method, Numeric
58
+ object
59
+ else
60
+ object.dup
61
61
  end
62
62
  end
63
63
  end
@@ -4,7 +4,7 @@ module SearchObject
4
4
 
5
5
  class << self
6
6
  def build_for(config, options)
7
- scope = options.fetch(:scope) { config[:scope] && config[:scope].call }
7
+ scope = options[:scope] || (config[:scope] && config[:scope].call)
8
8
  filters = Helper.stringify_keys(options.fetch(:filters, {}))
9
9
  params = config[:defaults].merge Helper.slice_keys(filters, config[:actions].keys)
10
10
 
@@ -1,3 +1,3 @@
1
1
  module SearchObject
2
- VERSION = '1.1.2'.freeze
2
+ VERSION = '1.1.3'.freeze
3
3
  end
@@ -100,18 +100,27 @@ module SearchObject
100
100
 
101
101
  expect(search_class.new(scope: 'other scope').results).to eq 'other scope'
102
102
  end
103
+
104
+ it 'passing nil as scope in constructor, falls back to default scope' do
105
+ search_class = define_search_class do
106
+ scope { 'scope' }
107
+ end
108
+
109
+ expect(search_class.new(scope: nil).results).to eq 'scope'
110
+ end
103
111
  end
104
112
 
105
113
  describe 'option' do
106
114
  it 'has default filter' do
107
115
  scope = [1, 2, 3]
108
- expect(scope).to receive(:where).with('value' => 1) { 'results' }
116
+ allow(scope).to receive(:where).and_return 'results'
109
117
 
110
118
  search = new_search scope, value: 1 do
111
119
  option :value
112
120
  end
113
121
 
114
122
  expect(search.results).to eq 'results'
123
+ expect(scope).to have_received(:where).with('value' => 1)
115
124
  end
116
125
 
117
126
  it 'returns the scope if nil returned' do
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+ require 'ostruct'
2
3
 
3
4
  module SearchObject
4
5
  describe Search do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: search_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Radoslav Stankov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-29 00:00:00.000000000 Z
11
+ date: 2017-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler