tmp commit for search. start nccu0621 first

This commit is contained in:
Matthew K. Fu JuYuan committed 2012-06-22 11:34:10 +08:00
1 parent cb17fe0825
commit 3f95fe4f13
20 files changed
+949 -1792

No files matched your search

-46
View File
@@ -1,46 +0,0 @@
# coding: utf-8
# 基本 Model,加入一些通用功能
module Mongoid
module BaseModel
extend ActiveSupport::Concern
included do
scope :recent, desc(:_id)
scope :exclude_ids, Proc.new { |ids| where(:_id.nin => ids.map(&:to_i)) }
scope :by_week, where(:created_at.gte => 7.days.ago.utc)
end
module ClassMethods
# like ActiveRecord find_by_id
def find_by_id(id)
if id.is_a?(Integer) or id.is_a?(String)
where(:_id => id.to_i).first
else
nil
end
end
def find_in_batches(opts = {})
batch_size = opts[:batch_size] || 1000
start = opts.delete(:start).to_i || 0
objects = self.limit(batch_size).skip(start)
t = Time.new
while objects.any?
yield objects
start += batch_size
# Rails.logger.debug("processed #{start} records in #{Time.new - t} seconds") if Rails.logger.debug?
break if objects.size < batch_size
objects = self.limit(batch_size).skip(start)
end
end
def delay
Sidekiq::Extensions::Proxy.new(DelayedDocument, self)
end
end
def delay
Sidekiq::Extensions::Proxy.new(DelayedDocument, self)
end
end
end
-46
View File
@@ -1,46 +0,0 @@
# 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 Sunspot
module Mongoid
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