Searchlight helps you build searches from options via Ruby methods that you write.
Searchlight helps you build searches from options via Ruby methods that you write.
I consider searchlight "done". It has no production dependencies, so there's no reason it shouldn't work indefinitely. I've also moved on to other things.
If you find a bug, feel free to open an issue so others can find it and discuss, but I'm unlikely to respond personally. If Searchlight doesn't meet your needs anymore, fork away! :)
Searchlight is a low-magic way to build database searches using an ORM.
Searchlight can work with any ORM or object that can build a query using chained method calls (eg, ActiveRecord's .where(...).where(...).limit(...), or similar chains with Sequel, Mongoid, etc).
A demo app and the code for that app are available to help you get started.
Searchlight's main use is to support search forms in web applications.
Searchlight doesn't write queries for you. What it does do is:
form_for in Rails)WHERE first_name = part)For example, if you have a Searchlight search class called YetiSearch, and you instantiate it like this:
search = YetiSearch.new(
# or params[:yeti_search]
"active" => true, "name" => "Jimmy", "location_in" => %w[NY LA]
)... calling search.results will build a search by calling the methods search_active, search_name, and search_location_in on your YetiSearch, assuming that you've defined them. (If you do it again but omit "name", it won't call search_name.)
The results method will then return the return value of the last search method. If you're using ActiveRecord, this would be an ActiveRecord::Relation, and you can then call each to loop through the results, to_sql to get the generated query, etc.
A search class has two main parts: a base_query and some search_ methods. For example:
class PersonSearch "Gregor", "last_name" => "Mendel").results` would run `Person.all.where(first_name: "Gregor").where(last_name: "Mendel")` and return the resulting `ActiveRecord::Relation`. If you omitted the `last_name` option, or provided `"last_name" => ""`, the second `where` would not be added.
Here's a fuller example search class. Note that **because Searchlight doesn't write queries for you, you're free to do anything your ORM supports**. (See `spec/support/book_search.rb` for even more fanciness.)
```ruby
# app/searches/city_search.rb
class CitySearch =' : ' "SELECT `cities`.* FROM `cities` "
CitySearch.new("name" => "Nairobi").results.to_sql
# => "SELECT `cities`.* FROM `cities` WHERE `cities`.`name` = 'Nairobi'"
CitySearch.new("country_name_like" => "aust", "continent" => "Europe").results.count # => 6
non_megas = CitySearch.new("is_megacity" => "false")
non_megas.results.to_sql
# => "SELECT `cities`.* FROM `cities` WHERE (`cities`.`population` true)` to find restricted results, just ensure that you check `options["super_user"]` when building your query.
## Usage in Rails
### ActionView adapter
Searchlight plays nicely with Rails forms - just include the `ActionView` adapter as follows:
```ruby
require "searchlight/adapters/action_view"
class MySearch < Searchlight::Search
include Searchlight::Adapters::ActionView
# ...etc
endThis will enable using a Searchlight::Search with form_for:
…As long as your form submits options your search understands, you can easily hook it up in your controller:
# app/controllers/orders_controller.rb
class OrdersController < ApplicationController
def index
@search = OrderSearch.new(search_params) # For use in a form
@results = @search.results # For display along with form
end
protected
def search_params
# Ensure the user can only browse or search their own orders
(params[:order_search] || {}).merge(user_id: current_user.id)
end
endFor any given version, check .travis.yml to see what Ruby versions we're testing for compatibility.
Add this line to your application's Gemfile:
gem 'searchlight'And then execute:
$ bundleOr install it yourself as:
$ gem install searchlightrake runs the tests; rake mutant runs mutation tests using mutant.
git checkout -b my-new-feature)git commit -am 'Add some feature')git push origin my-new-feature)No open issues yet, or sync has not completed.