Simple search engine for Refinery CMS.
By default it supports search in
Refinery::Blog::Post (if installed), but it can be easily configured for other Rails models too.
This version of
refinerycms-search supports Refinery 3.x and Rails 4.1.x.
For Refinery 2.1.x and Rails 3.x, use the 2-1-stable branch.
In summary you can
- Search in
- Add others models in your configuration
- Scope search query with a special method
- Use a custom search page URL
- Paginate the search result
What it can't do :
- It can't search all languages in the same query: it only display the results of the current locale.
Refinery CMS version 4.0.0 or above.
Open up your
Gemfile and at the bottom, add this line:
gem 'refinerycms-search', github: 'refinery/refinerycms-search', branch: 'master'
Next, to install the search plugin run:
rails generate refinery:search
Run database migrations:
Finally seed your database and you're done.
You will have to RE-SAVE all records that have not been indexed before.
You can do it easily with a command like this in rails console :
A sample search form can be found in views/refinery/shared/_search.html.erb. You can either use this partial directly, or copy the appropriate parts.
The default installation will search in
Refinery::Blog::Post (if installed).
If you wish to find results in other plugins you have created or installed, you can specify these in
config/initializers/refinery/search.rb like so:
config.enable_for = ['Refinery::Page']
Simply add any additional models you wish to search to this array. For example, if you have the portfolio plugin installed:
config.enable_for = ['Refinery::Page', 'Refinery::PortfolioEntry']
The above line will add indexing to PortfolioEntry in the portfolio plugin, which does not come indexed.
Any model you wish to search will need to be indexed using acts as indexed. To add indexing, simple add:
acts_as_indexed :fields => [:title, :body]
If your model doesn't use a
:title attribute, remember to add an
alias_attribute :title, :name #for example
You can use any public method, as well. So if you have
:last_name but a method like
name to join them, it can be indexed.
acts_as_indexed :fields => [:name, :biography] #... def name [first_name, last_name].reject(&:blank?).join(' ') end
You will need to replace the indexed fields with those appropriate for your model.
If you wish to override the url used in the search results just add a
url method to your model and the result of this method will be used instead.
Displaying a friendlier name for your model
Just define the method
friendly_search_name to override what is displayed
for each search result for your model as per the implementing pull request.
def friendly_search_name "Document" end
refinery_search_scope on searchable models
If you want to add a custom scope on a searchable model, you can use the method
refinery_search_scope in your model.
# app/decorators/models/refinery/page_decorator.rb Refinery::Page.class_eval do def self.refinery_search_scope live end end