Sha256: e409cefd9e064d29ad272ada884402eeeca2fc863c8b5e7d62796d2690a812a3

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

require 'sunspot'
require 'mongoid'
require 'sunspot/rails'

# == Examples:
#
# class Post
#   include Mongoid::Document
#   field :title
# 
#   include Sunspot::Mongoid
#   searchable do
#     text :title
#   end
# end
#
module Sunspot
  module Mongoid
    def self.included(base)
      base.class_eval do
        Sunspot::Adapters::DataAccessor.register(DataAccessor, base)
        Sunspot::Adapters::InstanceAdapter.register(InstanceAdapter, base)

        extend Sunspot::Rails::Searchable::ClassMethods
        include Sunspot::Rails::Searchable::InstanceMethods

        extend Sunspot::Mongoid::ClassMethods
      end
    end

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

        class_inheritable_hash :sunspot_options

        unless options[:auto_index] == false
          before_save :maybe_mark_for_auto_indexing
          after_save :maybe_auto_index
        end

        unless options[:auto_remove] == false
          after_destroy do |searchable|
            searchable.remove_from_index
          end
        end
        options[:include] = Sunspot::Util::Array(options[:include])

        self.sunspot_options = options
      end
    end

    class InstanceAdapter < Sunspot::Adapters::InstanceAdapter
      def id
        @instance.id
      end
    end

    class DataAccessor < Sunspot::Adapters::DataAccessor
      def load(id)
        @clazz.find(id) rescue nil
      end

      def load_all(ids)
        @clazz.criteria.in(:_id => ids)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sunspot_mongoid-0.3.0 lib/sunspot/mongoid.rb