Navigation Menu

Skip to content

Commit

Permalink
[WIP] Support Mongo 7.x
Browse files Browse the repository at this point in the history
  • Loading branch information
PikachuEXE committed May 28, 2018
1 parent 2640fc9 commit a749606
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Gemfile.mongoid-7.0
@@ -0,0 +1,15 @@
source "https://rubygems.org"

gemspec

gem 'rails', '~> 5.0'
gem 'sqlite3', platforms: [:ruby]
gem 'activerecord-jdbcsqlite3-adapter', platforms: [:jruby]
gem 'mongoid', '~> 7.0.0'

gem "rspec"

platforms :rbx do
gem 'rubysl', '~> 2.0'
gem 'rubinius-developer_tools'
end
6 changes: 6 additions & 0 deletions lib/bullet/dependency.rb
Expand Up @@ -42,6 +42,8 @@ def mongoid_version
'mongoid5x'
elsif mongoid6x?
'mongoid6x'
elsif mongoid7x?
'mongoid7x'
else
raise "Bullet does not support mongoid #{::Mongoid::VERSION} yet"
end
Expand Down Expand Up @@ -91,5 +93,9 @@ def mongoid5x?
def mongoid6x?
mongoid? && ::Mongoid::VERSION =~ /\A6/
end

def mongoid7x?
mongoid? && ::Mongoid::VERSION =~ /\A7/
end
end
end
60 changes: 60 additions & 0 deletions lib/bullet/mongoid7x.rb
@@ -0,0 +1,60 @@
# frozen_string_literal: true

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(opts = {})
result = origin_first(opts)
Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
result
end

def last(opts = {})
result = origin_last(opts)
Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
result
end

def each(&block)
return to_enum unless block_given?
records = []
origin_each { |record| records << record }
if records.length > 1
Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
elsif records.size == 1
Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
end
records.each(&block)
end

def eager_load(docs)
associations = criteria.inclusions.map(&:name)
docs.each do |doc|
Bullet::Detector::NPlusOneQuery.add_object_associations(doc, associations)
end
Bullet::Detector::UnusedEagerLoading.add_eager_loadings(docs, associations)
origin_eager_load(docs)
end
end

::Mongoid::Association::Accessors.class_eval do
alias_method :origin_get_relation, :get_relation

def get_relation(name, association, object, reload = false)
result = origin_get_relation(name, association, object, reload)
unless association.embedded?
Bullet::Detector::NPlusOneQuery.call_association(self, name)
end
result
end
end
end
end
end
1 change: 1 addition & 0 deletions test.sh
Expand Up @@ -6,6 +6,7 @@ BUNDLE_GEMFILE=Gemfile.rails-5.0 bundle && BUNDLE_GEMFILE=Gemfile.rails-5.0 bund
BUNDLE_GEMFILE=Gemfile.rails-4.2 bundle && BUNDLE_GEMFILE=Gemfile.rails-4.2 bundle exec rspec spec
BUNDLE_GEMFILE=Gemfile.rails-4.1 bundle && BUNDLE_GEMFILE=Gemfile.rails-4.1 bundle exec rspec spec
BUNDLE_GEMFILE=Gemfile.rails-4.0 bundle && BUNDLE_GEMFILE=Gemfile.rails-4.0 bundle exec rspec spec
BUNDLE_GEMFILE=Gemfile.mongoid-7.0 bundle && BUNDLE_GEMFILE=Gemfile.mongoid-7.0 bundle exec rspec spec
BUNDLE_GEMFILE=Gemfile.mongoid-6.0 bundle && BUNDLE_GEMFILE=Gemfile.mongoid-6.0 bundle exec rspec spec
BUNDLE_GEMFILE=Gemfile.mongoid-5.0 bundle && BUNDLE_GEMFILE=Gemfile.mongoid-5.0 bundle exec rspec spec
BUNDLE_GEMFILE=Gemfile.mongoid-4.0 bundle && BUNDLE_GEMFILE=Gemfile.mongoid-4.0 bundle exec rspec spec
1 change: 1 addition & 0 deletions update.sh
Expand Up @@ -4,6 +4,7 @@ BUNDLE_GEMFILE=Gemfile.rails-5.0 bundle update
BUNDLE_GEMFILE=Gemfile.rails-4.2 bundle update
BUNDLE_GEMFILE=Gemfile.rails-4.1 bundle update
BUNDLE_GEMFILE=Gemfile.rails-4.0 bundle update
BUNDLE_GEMFILE=Gemfile.mongoid-7.0 bundle update
BUNDLE_GEMFILE=Gemfile.mongoid-6.0 bundle update
BUNDLE_GEMFILE=Gemfile.mongoid-5.0 bundle update
BUNDLE_GEMFILE=Gemfile.mongoid-4.0 bundle update

0 comments on commit a749606

Please sign in to comment.