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 +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/search_object/helper.rb +45 -45
- data/lib/search_object/search.rb +1 -1
- data/lib/search_object/version.rb +1 -1
- data/spec/search_object/base_spec.rb +10 -1
- data/spec/search_object/search_spec.rb +1 -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: 3dcf24c846042501f789fdc3da39a47b15b33874
|
4
|
+
data.tar.gz: 80448932ff7fc522f8a20890ba8ef7ecda8f4eb3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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)
|
data/lib/search_object/helper.rb
CHANGED
@@ -1,63 +1,63 @@
|
|
1
1
|
module SearchObject
|
2
2
|
# :api: private
|
3
3
|
module Helper
|
4
|
-
|
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
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
20
|
-
|
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
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
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
|
data/lib/search_object/search.rb
CHANGED
@@ -4,7 +4,7 @@ module SearchObject
|
|
4
4
|
|
5
5
|
class << self
|
6
6
|
def build_for(config, options)
|
7
|
-
scope = options
|
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
|
|
@@ -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
|
-
|
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
|
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.
|
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:
|
11
|
+
date: 2017-04-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|