trample_search 0.17.0 → 0.18.0
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/.gitignore +1 -0
- data/Guardfile +17 -0
- data/lib/trample.rb +2 -0
- data/lib/trample/condition.rb +25 -1
- data/lib/trample/lookup_not_found.rb +37 -0
- data/lib/trample/search.rb +9 -2
- data/lib/trample/text_lookup.rb +27 -0
- data/lib/trample/version.rb +1 -1
- data/trample.gemspec +2 -0
- metadata +34 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6935fd536814586a0f865e35502b3aced06e513b
|
4
|
+
data.tar.gz: 5da146f8bac62c4b417f1ac90f8822b7d4071221
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01a3f811375bdd35d87d1de619002caeb0c6f3df8b6fde9de54d7e7dbf7f91dc72d0c68d46d24c46f090f6e35cd9109554b2a1ce69026b7dc210935f4b36c564
|
7
|
+
data.tar.gz: c15d50aacbf8234de5f2996a79818ad6f5e816955b7093b8c729d3915eb18c776b4c5fd884373fa83adb35df54d0aabf5dd401d11033ddfa792ffe2efdca34ea
|
data/.gitignore
CHANGED
data/Guardfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
guard :rspec, cmd: "bundle exec rspec --color --format documentation" do
|
2
|
+
require "guard/rspec/dsl"
|
3
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
4
|
+
watch(%r{^spec/(.*)\/?(.*)_spec\.rb$})
|
5
|
+
|
6
|
+
# Feel free to open issues for suggestions and improvements
|
7
|
+
|
8
|
+
# RSpec files
|
9
|
+
rspec = dsl.rspec
|
10
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
11
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
12
|
+
watch(rspec.spec_files)
|
13
|
+
|
14
|
+
# Ruby files
|
15
|
+
ruby = dsl.ruby
|
16
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
17
|
+
end
|
data/lib/trample.rb
CHANGED
@@ -3,9 +3,11 @@ require 'virtus'
|
|
3
3
|
require "trample/version"
|
4
4
|
require "trample/aggregation"
|
5
5
|
require "trample/serializable"
|
6
|
+
require "trample/lookup_not_found"
|
6
7
|
require "trample/condition"
|
7
8
|
require "trample/condition_proxy"
|
8
9
|
require "trample/metadata"
|
10
|
+
require "trample/text_lookup"
|
9
11
|
require "trample/search"
|
10
12
|
require "trample/backend/searchkick"
|
11
13
|
require "trample/results"
|
data/lib/trample/condition.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
module Trample
|
2
2
|
class Condition
|
3
3
|
include Virtus.model
|
4
|
-
|
5
4
|
attribute :name, Symbol
|
6
5
|
attribute :query_name, Symbol, default: :name
|
7
6
|
attribute :values, Array
|
@@ -20,12 +19,32 @@ module Trample
|
|
20
19
|
attribute :fields, Array
|
21
20
|
attribute :user_query, Hash
|
22
21
|
attribute :transform, Proc, default: ->(_,_) { ->(val) { val } }
|
22
|
+
attribute :search_klass
|
23
|
+
attribute :lookup, Hash
|
23
24
|
|
24
25
|
def initialize(attrs)
|
25
26
|
attrs.merge!(single: true) if attrs[:name] == :keywords
|
26
27
|
super(attrs)
|
27
28
|
end
|
28
29
|
|
30
|
+
def lookup_autocomplete
|
31
|
+
if require_autocomplete_lookup?
|
32
|
+
options = (lookup || {}).dup
|
33
|
+
klass = options.delete(:klass) || '::Trample::TextLookup'
|
34
|
+
|
35
|
+
options.assert_valid_keys(:key, :label)
|
36
|
+
|
37
|
+
options = options.merge({
|
38
|
+
search_klass: search_klass,
|
39
|
+
condition_name: name
|
40
|
+
})
|
41
|
+
|
42
|
+
lookup_instance = klass.to_s.constantize.new(options)
|
43
|
+
|
44
|
+
self.values = lookup_instance.load(values)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
29
48
|
def blank?
|
30
49
|
values.reject { |v| v == "" || v.nil? }.empty? && !range?
|
31
50
|
end
|
@@ -204,5 +223,10 @@ module Trample
|
|
204
223
|
|
205
224
|
{ runtime_query_name => hash }
|
206
225
|
end
|
226
|
+
|
227
|
+
def require_autocomplete_lookup?
|
228
|
+
(values.present? && values.first.is_a?(Hash)) &&
|
229
|
+
values.any? { |v| v[:text].blank? }
|
230
|
+
end
|
207
231
|
end
|
208
232
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Trample
|
2
|
+
class LookupNotFoundError < StandardError
|
3
|
+
|
4
|
+
attr_reader :condition, :lookup_results, :corresponding_lookup
|
5
|
+
|
6
|
+
def initialize(condition, lookup_results, corresponding_lookup)
|
7
|
+
@condition = condition
|
8
|
+
@lookup_results = lookup_results
|
9
|
+
@corresponding_lookup = corresponding_lookup
|
10
|
+
super(message)
|
11
|
+
end
|
12
|
+
|
13
|
+
def message
|
14
|
+
msg = <<-MSG.strip_heredoc
|
15
|
+
No corresponding lookup found for condition #{condition.name}
|
16
|
+
|
17
|
+
Corresponding Lookup Result: #{ corresponding_lookup }
|
18
|
+
|
19
|
+
Criteria Keys: #{ criteria_keys.join(", ") }
|
20
|
+
Lookup Result Keys: #{ lookup_keys.join(", ") }
|
21
|
+
Missing: #{ diff.join(", ") }
|
22
|
+
MSG
|
23
|
+
end
|
24
|
+
|
25
|
+
def criteria_keys
|
26
|
+
@criteria_keys ||= Array(condition.values).map { |l| l[:key] }.sort
|
27
|
+
end
|
28
|
+
|
29
|
+
def lookup_keys
|
30
|
+
@lookup_keys ||= Array(lookup_results).map {|l| l[:key] }.sort
|
31
|
+
end
|
32
|
+
|
33
|
+
def diff
|
34
|
+
criteria_keys - lookup_keys
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/trample/search.rb
CHANGED
@@ -23,7 +23,8 @@ module Trample
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def self.condition(name, attrs = {})
|
26
|
-
attrs.merge!(name: name)
|
26
|
+
attrs.merge!(name: name, search_klass: self)
|
27
|
+
|
27
28
|
@_conditions[name] = Condition.new(attrs)
|
28
29
|
end
|
29
30
|
|
@@ -133,9 +134,12 @@ module Trample
|
|
133
134
|
@backend ||= Backend::Searchkick.new(metadata, self.class._models)
|
134
135
|
end
|
135
136
|
|
136
|
-
def query!
|
137
|
+
def query!(options = { lookup: true })
|
137
138
|
@records = nil
|
138
139
|
hash = backend.query!(conditions, aggregations)
|
140
|
+
|
141
|
+
load_autocompletes if options[:lookup]
|
142
|
+
|
139
143
|
self.metadata.took = hash[:took]
|
140
144
|
self.metadata.scroll_id = hash[:scroll_id]
|
141
145
|
self.metadata.pagination.total = hash[:total]
|
@@ -191,5 +195,8 @@ module Trample
|
|
191
195
|
Marshal.load(Marshal.dump(o))
|
192
196
|
end
|
193
197
|
|
198
|
+
def load_autocompletes
|
199
|
+
self.conditions.values.each(&:lookup_autocomplete)
|
200
|
+
end
|
194
201
|
end
|
195
202
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Trample
|
2
|
+
class TextLookup
|
3
|
+
include Virtus.model
|
4
|
+
attribute :search_klass
|
5
|
+
attribute :condition_name, Symbol
|
6
|
+
attribute :key, Symbol, default: :id
|
7
|
+
attribute :label, Symbol, default: :text
|
8
|
+
|
9
|
+
def load(values)
|
10
|
+
search = search_klass.new
|
11
|
+
search.condition(condition_name).in(values)
|
12
|
+
search.query!(lookup: false)
|
13
|
+
|
14
|
+
values.map do | value |
|
15
|
+
result = find_corresponding_value(value, search.results)
|
16
|
+
value[:text] = result[label]
|
17
|
+
value
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def find_corresponding_value(value, results)
|
24
|
+
results.find { |result| value[:key].to_s == result[key].to_s }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/trample/version.rb
CHANGED
data/trample.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trample_search
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.18.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- richmolj
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-08-
|
11
|
+
date: 2016-08-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: virtus
|
@@ -150,6 +150,34 @@ dependencies:
|
|
150
150
|
- - ">="
|
151
151
|
- !ruby/object:Gem::Version
|
152
152
|
version: '0'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: guard
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: guard-rspec
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
type: :development
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - ">="
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '0'
|
153
181
|
description: Abstraction on top of searchkick to handle form inputs easily
|
154
182
|
email:
|
155
183
|
- richmolj@gmail.com
|
@@ -161,6 +189,7 @@ files:
|
|
161
189
|
- ".rspec"
|
162
190
|
- ".travis.yml"
|
163
191
|
- Gemfile
|
192
|
+
- Guardfile
|
164
193
|
- LICENSE.txt
|
165
194
|
- README.md
|
166
195
|
- Rakefile
|
@@ -174,12 +203,14 @@ files:
|
|
174
203
|
- lib/trample/condition.rb
|
175
204
|
- lib/trample/condition_proxy.rb
|
176
205
|
- lib/trample/errors.rb
|
206
|
+
- lib/trample/lookup_not_found.rb
|
177
207
|
- lib/trample/metadata.rb
|
178
208
|
- lib/trample/railtie.rb
|
179
209
|
- lib/trample/results.rb
|
180
210
|
- lib/trample/search.rb
|
181
211
|
- lib/trample/serializable.rb
|
182
212
|
- lib/trample/swagger.rb
|
213
|
+
- lib/trample/text_lookup.rb
|
183
214
|
- lib/trample/version.rb
|
184
215
|
- trample.gemspec
|
185
216
|
homepage:
|
@@ -202,7 +233,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
202
233
|
version: '0'
|
203
234
|
requirements: []
|
204
235
|
rubyforge_project:
|
205
|
-
rubygems_version: 2.
|
236
|
+
rubygems_version: 2.5.1
|
206
237
|
signing_key:
|
207
238
|
specification_version: 4
|
208
239
|
summary: Abstraction on top of searchkick to handle form inputs easily
|