Skip to content

Commit

Permalink
Merge pull request #668 from randikabanura/Mongoid-8.x-support
Browse files Browse the repository at this point in the history
Mongoid 8.x support add
  • Loading branch information
flyerhzm committed Sep 12, 2023
2 parents 1a71e5a + 8d083ca commit 5164635
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Gemfile.mongoid-8.0
@@ -0,0 +1,15 @@
source "https://rubygems.org"

gemspec

gem 'rails', '~> 6.1'
gem 'sqlite3', platforms: [:ruby]
gem 'activerecord-jdbcsqlite3-adapter', platforms: [:jruby]
gem 'mongoid', '~> 8.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 @@ -48,6 +48,8 @@ def mongoid_version
'mongoid6x'
elsif mongoid7x?
'mongoid7x'
elsif mongoid8x?
'mongoid8x'
else
raise "Bullet does not support mongoid #{::Mongoid::VERSION} yet"
end
Expand Down Expand Up @@ -121,5 +123,9 @@ def mongoid6x?
def mongoid7x?
mongoid? && ::Mongoid::VERSION =~ /\A7/
end

def mongoid8x?
mongoid? && ::Mongoid::VERSION =~ /\A8/
end
end
end
59 changes: 59 additions & 0 deletions lib/bullet/mongoid8x.rb
@@ -0,0 +1,59 @@

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
6 changes: 6 additions & 0 deletions spec/support/mongo_seed.rb
Expand Up @@ -42,6 +42,12 @@ def setup_db
config.load_configuration(sessions: { default: { database: 'bullet', hosts: %w[localhost:27017] } })
end
else
if %w[7.1 7.2 7.3 7.4 7.5 8 8.1].any? {|version| Mongoid::VERSION =~ /\A#{Regexp.quote(version)}/ }
Mongoid.logger = Logger.new(STDERR).tap do |logger|
logger.level = Logger::WARN
end
end

Mongoid.configure do |config|
config.load_configuration(clients: { default: { database: 'bullet', hosts: %w[localhost:27017] } })
end
Expand Down
1 change: 1 addition & 0 deletions test.sh
Expand Up @@ -9,6 +9,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-8.0 bundle && BUNDLE_GEMFILE=Gemfile.mongoid-8.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
Expand Down
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-8.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
Expand Down

0 comments on commit 5164635

Please sign in to comment.