Module: Mongoid::Haystack::Search

Defined in:
lib/mongoid-haystack/search.rb

Constant Summary collapse

ClassMethods =
proc do
  def mongoid_haystack_searchable?
    true
  end

  def search(*args, &block)
    options = Map.options_for!(args)
    options[:types] = Array(options[:types]).flatten.compact
    options[:types].push(self)
    args.push(options)
    results = Haystack.search(*args, &block)
  end

  def search_index_all!(*args, &block)
    options = Map.options_for!(args)
    models = args.shift
    
    unless models
      models = where(:haystack_index => nil)
    end

    threads = options[:threads] || 16

    models.all.each do |doc|
      Mongoid::Haystack::Index.remove(doc)
    end

    models.all.each do |doc|
      Mongoid::Haystack::Index.add(doc)
    end
  end

  after_save do |doc|
    begin
      doc.search_index! if doc.persisted?
    rescue Object
      nil
    end
  end

  after_destroy do |doc|
    begin
      doc.search_unindex! if doc.destroyed?
    rescue Object
      nil
    end
  end

  has_one(:haystack_index, :as => :model, :class_name => '::Mongoid::Haystack::Index')
end
InstanceMethods =
proc do
  def search_index!
    doc = self
    Mongoid::Haystack::Index.remove(doc)
    Mongoid::Haystack::Index.add(doc)
  end

  def search_unindex!
    doc = self
    Mongoid::Haystack::Index.remove(doc)
  end
end

Class Method Summary collapse

Class Method Details

.included(other) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/mongoid-haystack/search.rb', line 68

def Search.included(other)
  super
ensure
  unless other.respond_to?(:mongoid_haystack_searchable?)
    other.instance_eval(&ClassMethods)
    other.class_eval(&InstanceMethods)
  end
end