Skip to content

Instantly share code, notes, and snippets.

@batasrki
Created January 28, 2010 16:22
Show Gist options
  • Save batasrki/288881 to your computer and use it in GitHub Desktop.
Save batasrki/288881 to your computer and use it in GitHub Desktop.
class Product
include MongoMapper::Document
include MongoMapper::Sunspot
belongs_to :crawl
many :inventories
key :product_no, Integer
key :price_in_cents, Integer
key :alcohol_content, Integer
key :name, String
key :stock_type, String
key :primary_category, String
key :secondary_category, String
key :origin, String
key :package, String
key :sugar_content, String
key :producer_name, String
key :inventory_count, Integer
ensure_index [:crawl_id, :product_no]
sunspot do
string :crawl_id
integer :product_no
string :name
string :stock_type
string :primary_category
string :secondary_category
string :origin
string :package
string :producer_name
end
end
module MongoMapper
module Sunspot
def self.included(doc)
doc.extend(ClassMethods)
end
module ClassMethods
def sunspot(&block)
Adapters.register(self)
::Sunspot.setup(self, &block)
end
end
module Adapters
def self.register(doc)
::Sunspot::Adapters::InstanceAdapter.register(InstanceAdapter, doc)
::Sunspot::Adapters::DataAccessor.register(DataAccessor, doc)
end
class InstanceAdapter < ::Sunspot::Adapters::InstanceAdapter
def id
@instance._id
end
end
class DataAccessor < ::Sunspot::Adapters::DataAccessor
def load(id)
@clazz.find(id)
end
def load_all(ids)
@clazz.find(*ids)
end
end
end
end
end
I have not tested any of this very much, but I will try to update
the gist if anything changes. I'm sure there is a nicer way to
integrate with MongoMapper somehow.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment