Module: Sunspot::Rails::Searchable::ActsAsMethods
- Defined in:
- lib/sunspot/rails/searchable.rb
Instance Method Summary collapse
-
#searchable(options = {}, &block) ⇒ Object
Makes a class searchable if it is not already, or adds search configuration if it is.
-
#searchable? ⇒ Boolean
This method is defined on all ActiveRecord::Base subclasses.
Instance Method Details
#searchable(options = {}, &block) ⇒ Object
Makes a class searchable if it is not already, or adds search configuration if it is. Note that the options passed in are only used the first time this method is called for a particular class; so, search should be defined before activating any mixins that extend search configuration.
The block passed into this method is evaluated by the
Sunspot.setup method. See the Sunspot documentation for
complete information on the functionality provided by that method.
Options (+options+)
:auto_index
Automatically index models in Solr when they are saved.
Default: true
:auto_remove
Automatically remove models from the Solr index when they are
destroyed. <b>Setting this option to +false+ is not recommended
</b>(see the README).
:ignore_attribute_changes_of
Define attributes, that should not trigger a reindex of that
object. Usual suspects are updated_at or counters.
:include
Define default ActiveRecord includes, set this to allow ActiveRecord
to load required associations when indexing. See ActiveRecord's
documentation on eager-loading for examples on how to set this
Default: []
:paginate
Retreive models using page numbers rather than database-style limit
and offset. Assumes the total count of models cannot be determined
without performing at least one request.
Default: false
:max_batch_size
Override the command line specified batch size that is used for
indexing on a per-model basis.
Example
class Post < ActiveRecord::Base
searchable do
text :title, :body
string :sort_title do
title.downcase.sub(/^(an?|the)/, '')
end
integer :blog_id
time :updated_at
end
end
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/sunspot/rails/searchable.rb', line 69 def searchable( = {}, &block) Sunspot.setup(self, &block) if searchable? [:include].concat(Util::Array([:include])) else extend ClassMethods include InstanceMethods class_inheritable_hash :sunspot_options unless [:auto_index] == false before_save :maybe_mark_for_auto_indexing after_save :maybe_auto_index end unless [:auto_remove] == false after_destroy do |searchable| searchable.remove_from_index end end [:include] = Util::Array([:include]) self. = end end |
#searchable? ⇒ Boolean
This method is defined on all ActiveRecord::Base subclasses. It is false for classes on which #searchable has not been called, and true for classes on which #searchable has been called.
Returns
false
105 106 107 |
# File 'lib/sunspot/rails/searchable.rb', line 105 def searchable? false end |