# coding: utf-8
# this is from : https://github.com/jugyo/sunspot_mongoid
# this file is special for mongoid_auto_increment_id
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 OrbitSearchLib
  module ObjectSearchable
    def self.included(base)
      base.class_eval do
        extend Sunspot::Rails::Searchable::ActsAsMethods
        Sunspot::Adapters::DataAccessor.register(DataAccessor, base)
        Sunspot::Adapters::InstanceAdapter.register(InstanceAdapter, base)
      end
    end

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

    class DataAccessor < Sunspot::Adapters::DataAccessor
      def load(id)
        @clazz.where(:_id => id).first
      end

      def load_all(ids)
        @clazz.where(:_id.in => ids.collect { |id| id.to_i })
      end
    end
  end
end
