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<Boolean>

Automatically index models in Solr when they are saved. Default: true

:auto_remove<Boolean>

Automatically remove models from the Solr index when they are destroyed. Setting this option to false is not recommended (see the README).

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


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sunspot/rails/searchable.rb', line 51

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

  unless searchable?
    extend ClassMethods
    include InstanceMethods

    unless options[:auto_index] == false
      after_save do |searchable|
        searchable.index
      end
    end

    unless options[:auto_remove] == false
      after_destroy do |searchable|
        searchable.remove_from_index
      end
    end
  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)


81
82
83
# File 'lib/sunspot/rails/searchable.rb', line 81

def searchable?
  false
end