Module: Sunspot::Rails::Searchable::ActsAsMethods

Defined in:
lib/sunspot/rails/searchable.rb

Instance Method Summary collapse

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).

:if::

Only index models in Solr if the method, proc or string evaluates
to true (e.g. <code>:if => :should_index?</code> or <code>:if =>
proc { |model| model.foo > 2 }</code>).  Models that do not match
the constraint will be removed from the index upon save.  Multiple
constraints can be specified by passing an array (e.g. <code>:if =>
[:method1, :method2]</code>).

:ignore_attribute_changes_of::

Define attributes, that should not trigger a reindex of that
object. Usual suspects are updated_at or counters.

:only_reindex_attribute_changes_of::

Define attributes, that are the only attributes that should
trigger a reindex of that object. Useful if there are a small
number of searchable attributes and a large number of attributes
to ignore.

: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: [] 

:unless::

Only index models in Solr if the method, proc or string evaluates
to false (e.g. <code>:unless => :should_not_index?</code> or <code>
:unless => proc { |model| model.foo <= 2 }</code>).  Models that do
not match the constraint will be removed from the index upon save.
Multiple constraints can be specified by passing an array (e.g.
<code>:unless => [:method1, :method2]</code>).

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


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/sunspot/rails/searchable.rb', line 80

def searchable(options = {}, &block)
  Sunspot.setup(self, &block)

  if searchable?
    sunspot_options[:include].concat(Util::Array(options[:include]))
  else
    extend ClassMethods
    include InstanceMethods

    class_attribute :sunspot_options

    unless options[:auto_index] == false
      before_save :mark_for_auto_indexing_or_removal

      # after_commit :perform_index_tasks, :if => :persisted?
      __send__ Sunspot::Rails.configuration.auto_index_callback,
               :perform_index_tasks,
               :if => :persisted?
    end

    unless options[:auto_remove] == false
      # after_commit { |searchable| searchable.remove_from_index }, :on => :destroy
      # Only add the on filter if the callback supports it
      if Sunspot::Rails.configuration.auto_remove_callback =~ /save|destroy|create/
        __send__ Sunspot::Rails.configuration.auto_remove_callback,
                proc { |searchable| searchable.remove_from_index }
      else
        __send__ Sunspot::Rails.configuration.auto_remove_callback,
                proc { |searchable| searchable.remove_from_index },
                :on => :destroy
      end
    end
    options[:include] = Util::Array(options[:include])

    self.sunspot_options = options
  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

Returns:

  • (Boolean)


127
128
129
# File 'lib/sunspot/rails/searchable.rb', line 127

def searchable?
  false
end