searchlight 3.0.0 → 3.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6535dc6c570b01a67e7a25a51819bd57c155abb5
4
- data.tar.gz: 6741f5d2dd6a1bc93641263848b926662df22307
3
+ metadata.gz: 432e9a94536f38173152799da14d74ba3adb8e23
4
+ data.tar.gz: 649e95855e50d9663f831b8e639e97d7c9bbb64c
5
5
  SHA512:
6
- metadata.gz: 06768634ad235bb9b45fe93ceaf2f5d1a07fcfc016f396d1fa70b727721ac802ac8956869bcc2715539b413f6b22126e614a9042277b2bfe04c80af213d2fe9f
7
- data.tar.gz: 4350283762d804a7c1747b49815598f5b17eddd28ca8ee591ad0857ace1f578099d57d8e1f517d03cacbaf18cda126ab04e1a6d37eeb2eb5d7636cfa028f74a6
6
+ metadata.gz: 545f658dcc895609270b655f20d6dc14e3ffcdbb26567b4a2051d86372872c540e58b03264fd9e6a68f68a8b4860d5f5eb0ba68c9d60590dab71025523203798
7
+ data.tar.gz: 926a6a17de716ce11200f0c3182238338d57aaaa4aab19662a50df4021344f16affe998504db9f8e5219e9911bdc06616f63a2f100635cffacb042f937d69d58
data/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  Searchlight does its best to use [semantic versioning](http://semver.org).
4
4
 
5
+ ## v3.1.0
6
+
7
+ Allow callable search targets, thanks to [Adam Nowak](https://github.com/lubieniebieski).
8
+
5
9
  ## v3.0.0
6
10
 
7
11
  Two major releases in two days!? Well, I thought of another good, but breaking, change. To the major version bump, Robin!
data/README.md CHANGED
@@ -22,7 +22,7 @@ For example, if you have a Searchlight search class called `YetiSearch`, and you
22
22
 
23
23
  ```ruby
24
24
  search = YetiSearch.new(
25
- active: true, name: 'Jimmy', location_in: %w[NY LA] # or params[:search]
25
+ active: true, name: 'Jimmy', location_in: %w[NY LA] # or params[:yeti_search]
26
26
  )
27
27
  ```
28
28
 
@@ -233,6 +233,24 @@ SmallTownSearch.new(country_name_like: 'Norfolk').results.to_sql
233
233
  => "SELECT `cities`.* FROM `cities` WHERE (`cities`.`population` < 1000) AND (`countries`.`name` LIKE '%Norfolk%')"
234
234
  ```
235
235
 
236
+ ### Delayed scope evaluation
237
+
238
+ If your search target has a time-sensitive condition, you can wrap it in a callable object to prevent it from being evaluated when the class is defined. For example:
239
+
240
+ ```ruby
241
+ class RecentOrdersSearch < Searchlight::Search
242
+ search_on proc { Orders.since(Time.now - 3.hours) }
243
+ end
244
+ ```
245
+
246
+ This does make subclassing a bit more complex:
247
+
248
+ ```ruby
249
+ class ExpensiveRecentOrdersSearch < RecentOrderSearch
250
+ search_on proc { superclass.search_target.call.expensive }
251
+ end
252
+ ```
253
+
236
254
  ### Dependent Options
237
255
 
238
256
  To allow search options that don't trigger searches directly, just use `attr_accessor`.
@@ -286,7 +304,7 @@ class OrdersController < ApplicationController
286
304
 
287
305
  def search_params
288
306
  # Ensure the user can only browse or search their own orders
289
- (params[:search]) || {}).merge(user_id: current_user.id)
307
+ (params[:order_search] || {}).merge(user_id: current_user.id)
290
308
  end
291
309
  end
292
310
  ```
@@ -13,7 +13,14 @@ module Searchlight
13
13
  end
14
14
 
15
15
  def search
16
- @search ||= self.class.search_target
16
+ @search ||= begin
17
+ target = self.class.search_target
18
+ if target.respond_to?(:call)
19
+ target.call
20
+ else
21
+ target
22
+ end
23
+ end
17
24
  end
18
25
 
19
26
  def results
@@ -1,3 +1,3 @@
1
1
  module Searchlight
2
- VERSION = "3.0.0"
2
+ VERSION = "3.1.0"
3
3
  end
@@ -186,16 +186,16 @@ describe Searchlight::Search do
186
186
 
187
187
  describe "search_on" do
188
188
 
189
- context "when an explicit search target was provided" do
189
+ context "when an explicit, non-callable search target was provided" do
190
190
 
191
- let(:search_target) { "Bobby Fischer" }
191
+ let(:example_search_target) { "Bobby Fischer" }
192
192
 
193
193
  before :each do
194
- search_class.search_on search_target
194
+ search_class.search_on example_search_target
195
195
  end
196
196
 
197
197
  it "makes the object accessible via `search_target`" do
198
- expect(search_class.search_target).to eq(search_target)
198
+ expect(search_class.search_target).to eq(example_search_target)
199
199
  end
200
200
 
201
201
  it "makes the search target available to its children" do
@@ -210,6 +210,24 @@ describe Searchlight::Search do
210
210
 
211
211
  end
212
212
 
213
+ context "when an explicit, callable search target was provided" do
214
+
215
+ it "calls it in the process of producing search results" do
216
+ search = ProcSearch.new(first_name: "Jimmy")
217
+ results = search.results
218
+ expect(results).to be_a(MockRelation)
219
+ expect(results.called_methods).to eq([:some_scope, :where])
220
+ end
221
+
222
+ it "allows it to refer to a parent class's callable search target" do
223
+ search = ChildProcSearch.new(first_name: "Carlos")
224
+ results = search.results
225
+ expect(results).to be_a(MockRelation)
226
+ expect(results.called_methods).to eq([:some_scope, :other_scope, :where])
227
+ end
228
+
229
+ end
230
+
213
231
  context "when no explicit search target was provided" do
214
232
 
215
233
  let(:search_class) { Named::Class.new('Namespaced::ExampleSearch', described_class) }
data/spec/spec_helper.rb CHANGED
@@ -11,6 +11,7 @@ $LOAD_PATH << '.'
11
11
  require 'support/mock_model'
12
12
  require 'support/account_search'
13
13
  require 'support/spiffy_account_search'
14
+ require 'support/proc_search'
14
15
 
15
16
  RSpec.configure do |config|
16
17
  config.treat_symbols_as_metadata_keys_with_true_values = true
@@ -0,0 +1,17 @@
1
+ class ProcSearch < Searchlight::Search
2
+
3
+ searches :first_name
4
+
5
+ search_on proc { MockModel.some_scope }
6
+
7
+ def search_first_name
8
+ search.where(first_name: first_name)
9
+ end
10
+
11
+ end
12
+
13
+ class ChildProcSearch < ProcSearch
14
+
15
+ search_on proc { superclass.search_target.call.other_scope }
16
+
17
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: searchlight
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Long
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-04-15 00:00:00.000000000 Z
12
+ date: 2014-09-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: named
@@ -170,6 +170,7 @@ files:
170
170
  - spec/spec_helper.rb
171
171
  - spec/support/account_search.rb
172
172
  - spec/support/mock_model.rb
173
+ - spec/support/proc_search.rb
173
174
  - spec/support/spiffy_account_search.rb
174
175
  homepage: https://github.com/nathanl/searchlight
175
176
  licenses:
@@ -191,7 +192,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
191
192
  version: '0'
192
193
  requirements: []
193
194
  rubyforge_project:
194
- rubygems_version: 2.2.0
195
+ rubygems_version: 2.2.2
195
196
  signing_key:
196
197
  specification_version: 4
197
198
  summary: Searchlight is a low-magic way to build database searches using an ORM.
@@ -202,4 +203,5 @@ test_files:
202
203
  - spec/spec_helper.rb
203
204
  - spec/support/account_search.rb
204
205
  - spec/support/mock_model.rb
206
+ - spec/support/proc_search.rb
205
207
  - spec/support/spiffy_account_search.rb