Permalink
Browse files

different mongid 2.4 and mongid 3

  • Loading branch information...
1 parent c5983c2 commit 6eba55f19edc3f65eb39ccfa3e5103228a730a0b @flyerhzm committed May 26, 2012
Showing with 69 additions and 14 deletions.
  1. +5 −1 lib/bullet.rb
  2. +11 −13 lib/bullet/{mongoid.rb → mongoid24.rb}
  3. +53 −0 lib/bullet/mongoid3.rb
View
@@ -12,7 +12,11 @@ module Bullet
autoload :ActiveRecord, 'bullet/active_record2'
autoload :ActionController, 'bullet/action_controller2'
end
- autoload :Mongoid, 'bullet/mongoid'
+ if Mongoid::VERSION =~ /\A2\.4/
+ autoload :Mongoid, 'bullet/mongoid24'
+ elsif Mongoid::VERSION =~ /\A3/
+ autoload :Mongoid, 'bullet/mongoid3'
+ end
autoload :Rack, 'bullet/rack'
autoload :BulletLogger, 'bullet/logger'
autoload :Notification, 'bullet/notification'
@@ -2,10 +2,12 @@ module Bullet
module Mongoid
def self.enable
require 'mongoid'
+
::Mongoid::Contexts::Mongo.class_eval do
alias_method :origin_first, :first
alias_method :origin_last, :last
alias_method :origin_iterate, :iterate
+ alias_method :origin_eager_load, :eager_load
def first
result = origin_first
@@ -28,19 +30,6 @@ def iterate(&block)
end
origin_iterate(&block)
end
- end
-
- ::Mongoid::Relations::Accessors.class_eval do
- alias_method :origin_set_relation, :set_relation
-
- def set_relation(name, relation)
- Bullet::Detector::NPlusOneQuery.call_association(self, name)
- origin_set_relation(name, relation)
- end
- end
-
- ::Mongoid::Contexts::Mongo.class_eval do
- alias_method :origin_eager_load, :eager_load
def eager_load(docs)
associations = criteria.inclusions.map(&:name)
@@ -51,6 +40,15 @@ def eager_load(docs)
origin_eager_load(docs)
end
end
+
+ ::Mongoid::Relations::Accessors.class_eval do
+ alias_method :origin_set_relation, :set_relation
+
+ def set_relation(name, relation)
+ Bullet::Detector::NPlusOneQuery.call_association(self, name)
+ origin_set_relation(name, relation)
+ end
+ end
end
end
end
View
@@ -0,0 +1,53 @@
+module Bullet
+ module Mongoid
+ def self.enable
+ require 'mongoid'
+ ::Mongoid::Contextual::Mongo.class_eval do
+ alias_method :origin_first, :first
+ alias_method :origin_last, :last
+ alias_method :origin_each, :each
+ alias_method :origin_eager_load, :eager_load
+
+ def first
+ result = origin_first
+ Bullet::Detector::Association.add_impossible_object(result)
+ result
+ end
+
+ def last
+ result = origin_last
+ Bullet::Detector::Association.add_impossible_object(result)
+ result
+ end
+
+ def each(&block)
+ records = query.map{ |doc| ::Mongoid::Factory.from_db(klass, doc) }
+ if records.length > 1
+ Bullet::Detector::Association.add_possible_objects(records)
+ elsif records.size == 1
+ Bullet::Detector::Association.add_impossible_object(records.first)
+ end
+ origin_each(&block)
+ end
+
+ def eager_load(docs)
+ associations = criteria.inclusions.map(&:name)
+ docs.each do |doc|
+ Bullet::Detector::Association.add_object_associations(doc, associations)
+ end
+ Bullet::Detector::Association.add_eager_loadings(docs, associations)
+ origin_eager_load(docs)
+ end
+ end
+
+ ::Mongoid::Relations::Accessors.class_eval do
+ alias_method :origin_set_relation, :set_relation
+
+ def set_relation(name, relation)
+ Bullet::Detector::NPlusOneQuery.call_association(self, name)
+ origin_set_relation(name, relation)
+ end
+ end
+ end
+ end
+end

0 comments on commit 6eba55f

Please sign in to comment.