trample_search 0.17.0 → 0.18.0

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: d6aae72f53b824559e0bcf84a7c544737bc8b180
4
- data.tar.gz: 50bca159319464c8cc994d0f87b7aec131b279c4
3
+ metadata.gz: 6935fd536814586a0f865e35502b3aced06e513b
4
+ data.tar.gz: 5da146f8bac62c4b417f1ac90f8822b7d4071221
5
5
  SHA512:
6
- metadata.gz: da90c4512a189ee0909d685eed2b4ea4e4f90408925efaa03dd78584055f07ae5c01f17b98052f11aa3207203e1320b885d419a99d59a7753f1ea23a8bf100b0
7
- data.tar.gz: 5bc12aaff9e5665ec1b50c86272495d85622d016bee5c81cbb7fd3a03a0c8ce9ef3289fb4628d6ed44cdb519463855f92d2d35bf3e4123a887757bf21fea7eba
6
+ metadata.gz: 01a3f811375bdd35d87d1de619002caeb0c6f3df8b6fde9de54d7e7dbf7f91dc72d0c68d46d24c46f090f6e35cd9109554b2a1ce69026b7dc210935f4b36c564
7
+ data.tar.gz: c15d50aacbf8234de5f2996a79818ad6f5e816955b7093b8c729d3915eb18c776b4c5fd884373fa83adb35df54d0aabf5dd401d11033ddfa792ffe2efdca34ea
data/.gitignore CHANGED
@@ -10,3 +10,4 @@
10
10
  /spec/reports/
11
11
  /tmp/
12
12
  vendor
13
+ .byebug_history
@@ -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
@@ -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"
@@ -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
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Trample
2
- VERSION = "0.17.0"
2
+ VERSION = "0.18.0"
3
3
  end
@@ -29,4 +29,6 @@ Gem::Specification.new do |spec|
29
29
  spec.add_development_dependency "rspec"
30
30
  spec.add_development_dependency "pry"
31
31
  spec.add_development_dependency "pry-byebug"
32
+ spec.add_development_dependency "guard"
33
+ spec.add_development_dependency "guard-rspec"
32
34
  end
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.17.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-08 00:00:00.000000000 Z
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.4.5.1
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