searchlight 3.0.0 → 3.1.0
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/README.md +20 -2
- data/lib/searchlight/search.rb +8 -1
- data/lib/searchlight/version.rb +1 -1
- data/spec/searchlight/search_spec.rb +22 -4
- data/spec/spec_helper.rb +1 -0
- data/spec/support/proc_search.rb +17 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 432e9a94536f38173152799da14d74ba3adb8e23
|
4
|
+
data.tar.gz: 649e95855e50d9663f831b8e639e97d7c9bbb64c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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[:
|
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[:
|
307
|
+
(params[:order_search] || {}).merge(user_id: current_user.id)
|
290
308
|
end
|
291
309
|
end
|
292
310
|
```
|
data/lib/searchlight/search.rb
CHANGED
data/lib/searchlight/version.rb
CHANGED
@@ -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(:
|
191
|
+
let(:example_search_target) { "Bobby Fischer" }
|
192
192
|
|
193
193
|
before :each do
|
194
|
-
search_class.search_on
|
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(
|
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
@@ -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.
|
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-
|
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.
|
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
|