Skip to content

Commit

Permalink
Merge pull request #736 from marekls/mongoid-9
Browse files Browse the repository at this point in the history
Mongoid 9 support
  • Loading branch information
flyerhzm authored Feb 19, 2025
2 parents b34f34e + a73883d commit 86654e7
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 1 deletion.
11 changes: 11 additions & 0 deletions Gemfile.mongoid-9.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
source "https://rubygems.org"

gemspec

gem 'rails', '~> 8.0'
gem 'sqlite3'
gem 'activerecord-jdbcsqlite3-adapter', platforms: [:jruby]
gem 'mongoid', '~> 9.0'
gem 'rspec'


6 changes: 6 additions & 0 deletions lib/bullet/dependency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def mongoid_version
'mongoid7x'
elsif mongoid8x?
'mongoid8x'
elsif mongoid9x?
'mongoid9x'
else
raise "Bullet does not support mongoid #{::Mongoid::VERSION} yet"
end
Expand Down Expand Up @@ -149,5 +151,9 @@ def mongoid7x?
def mongoid8x?
mongoid? && ::Mongoid::VERSION =~ /\A8/
end

def mongoid9x?
mongoid? && ::Mongoid::VERSION =~ /\A9/
end
end
end
72 changes: 72 additions & 0 deletions lib/bullet/mongoid9x.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# frozen_string_literal: true

module Bullet
module Mongoid
def self.enable
require 'mongoid'
require 'rubygems'
::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

%i[first last].each do |context|
default = Gem::Version.new(::Mongoid::VERSION) >= Gem::Version.new('7.5') ? nil : {}
define_method(context) do |opts = default|
result = send(:"origin_#{context}", opts)
Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
result
end
end

def each(&_block)
return to_enum unless block_given?

first_document = nil
document_count = 0

origin_each do |document|
document_count += 1

if document_count == 1
first_document = document
elsif document_count == 2
Bullet::Detector::NPlusOneQuery.add_possible_objects([first_document, document])
yield(first_document)
first_document = nil
yield(document)
else
Bullet::Detector::NPlusOneQuery.add_possible_objects(document)
yield(document)
end
end

if document_count == 1
Bullet::Detector::NPlusOneQuery.add_impossible_object(first_document)
yield(first_document)
end

self
end

def eager_load(docs)
associations = criteria.inclusions.map(&:name)
docs.each { |doc| Bullet::Detector::NPlusOneQuery.add_object_associations(doc, associations) }
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)
Bullet::Detector::NPlusOneQuery.call_association(self, name) unless association.embedded?
result
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/support/mongo_seed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ 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)}/ }
if %w[7.1 7.2 7.3 7.4 7.5 8 8.1 9.0].any? { |version| Mongoid::VERSION =~ /\A#{Regexp.quote(version)}/ }
Mongoid.logger =
Logger.new(STDERR).tap do |logger|
logger.level = Logger::WARN
Expand Down
1 change: 1 addition & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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-9.0 bundle && BUNDLE_GEMFILE=Gemfile.mongoid-9.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
Expand Down
1 change: 1 addition & 0 deletions update.sh
Original file line number Diff line number Diff line change
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-9.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
Expand Down

0 comments on commit 86654e7

Please sign in to comment.