Sha256: fc813204204cecfed209ecefe061fb892f180e797a5d750f5f0e8397919364f2

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

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

# This code is modified for mongoid from the follow,
# http://github.com/outoftime/sunspot/blob/master/sunspot_rails/lib/sunspot/rails/searchable.rb
#
# Sunspot::Rails is distributed under the MIT License, copyright © 2009 Mat Brown
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

        def self.sunspot_setup(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
    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

2 entries across 2 versions & 1 rubygems

Version Path
sunspot_mongoid-0.1.2 lib/sunspot/mongoid.rb
sunspot_mongoid-0.1.1 lib/sunspot/mongoid.rb