module Sunspot::Rails::Searchable::InstanceMethods

Public Instance Methods

indexable?() click to toggle source
# File lib/sunspot/rails/searchable.rb, line 499
def indexable?
  # options[:if] is not specified or they successfully pass
  if_passes = self.class.sunspot_options[:if].nil? ||
              constraint_passes?(self.class.sunspot_options[:if])

  # options[:unless] is not specified or they successfully pass
  unless_passes = self.class.sunspot_options[:unless].nil? ||
                  !constraint_passes?(self.class.sunspot_options[:unless])

  if_passes and unless_passes
end
solr_atomic_update(updates = {}) click to toggle source

Updates specified model properties in Solr. Unlike ClassMethods#solr_atomic_update you dont need to pass object id, you only need to pass hash with property changes

# File lib/sunspot/rails/searchable.rb, line 455
def solr_atomic_update(updates = {})
  Sunspot.atomic_update(self.class, self.id => updates)
end
solr_atomic_update!(updates = {}) click to toggle source

Updates specified model properties in Solr and immediately commit. See solr_atomic_update

# File lib/sunspot/rails/searchable.rb, line 463
def solr_atomic_update!(updates = {})
  Sunspot.atomic_update!(self.class, self.id => updates)
end
solr_index() click to toggle source

Index the model in Solr. If the model is already indexed, it will be updated. Using the defaults, you will usually not need to call this method, as models are indexed automatically when they are created or updated. If you have disabled automatic indexing (see ClassMethods#searchable), this method allows you to manage indexing manually.

# File lib/sunspot/rails/searchable.rb, line 439
def solr_index
  Sunspot.index(self)
end
solr_index!() click to toggle source

Index the model in Solr and immediately commit. See index

# File lib/sunspot/rails/searchable.rb, line 446
def solr_index!
  Sunspot.index!(self)
end
solr_more_like_this(*args, &block) click to toggle source
# File lib/sunspot/rails/searchable.rb, line 486
def solr_more_like_this(*args, &block)
  options = args.extract_options!
  self.class.solr_execute_search(options) do
    Sunspot.new_more_like_this(self, *args, &block)
  end
end
solr_more_like_this_ids(&block) click to toggle source
# File lib/sunspot/rails/searchable.rb, line 493
def solr_more_like_this_ids(&block)
  self.class.solr_execute_search_ids do
    solr_more_like_this(&block)
  end
end
solr_remove_from_index() click to toggle source

Remove the model from the Solr index. Using the defaults, this should not be necessary, as models will automatically be removed from the index when they are destroyed. If you disable automatic removal (which is not recommended!), you can use this method to manage removal manually.

# File lib/sunspot/rails/searchable.rb, line 474
def solr_remove_from_index
  Sunspot.remove(self)
end
solr_remove_from_index!() click to toggle source

Remove the model from the Solr index and commit immediately. See remove_from_index

# File lib/sunspot/rails/searchable.rb, line 482
def solr_remove_from_index!
  Sunspot.remove!(self)
end

Private Instance Methods

constraint_passes?(constraint) click to toggle source
# File lib/sunspot/rails/searchable.rb, line 513
def constraint_passes?(constraint)
  case constraint
  when Symbol
    self.__send__(constraint)
  when String
    self.__send__(constraint.to_sym)
  when Enumerable
    # All constraints must pass
    constraint.all? { |inner_constraint| constraint_passes?(inner_constraint) }
  else
    if constraint.respond_to?(:call) # could be a Proc or anything else that responds to call
      constraint.call(self)
    else
      raise ArgumentError, "Unknown constraint type: #{constraint} (#{constraint.class})"
    end
  end
end
mark_for_auto_indexing_or_removal() click to toggle source
# File lib/sunspot/rails/searchable.rb, line 531
def mark_for_auto_indexing_or_removal
  if indexable?
    # :if/:unless constraints pass or were not present

    @marked_for_auto_indexing =
      if !new_record? && ignore_attributes = self.class.sunspot_options[:ignore_attribute_changes_of]
        !(changed.map { |attr| attr.to_sym } - ignore_attributes).blank?
      elsif !new_record? && only_attributes = self.class.sunspot_options[:only_reindex_attribute_changes_of]
        !(changed.map { |attr| attr.to_sym } & only_attributes).blank?
      else
        true
      end

    @marked_for_auto_removal = false
  else
    # :if/:unless constraints did not pass; do not auto index and
    # actually go one step further by removing it from the index
    @marked_for_auto_indexing = false
    @marked_for_auto_removal = true
  end

  true
end
perform_index_tasks() click to toggle source
# File lib/sunspot/rails/searchable.rb, line 555
def perform_index_tasks
  if @marked_for_auto_indexing
    solr_index
    remove_instance_variable(:@marked_for_auto_indexing)
  elsif @marked_for_auto_removal
    solr_remove_from_index
    remove_instance_variable(:@marked_for_auto_removal)
  end
end