Sha256: f0ab83feedbc9c4c696d3631f204225bfc006becc766dfa4e437eaca6a9ead58

Contents?: true

Size: 1.53 KB

Versions: 1

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.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
    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.2.0 lib/sunspot/mongoid.rb