Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MONGOID-5831 Remove usage of Rails private API (#5908) #5909

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions lib/mongoid/traversable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,29 @@ module Mongoid
# around traversing the document graph.
module Traversable
extend ActiveSupport::Concern
# This code is extracted from ActiveSupport so that we do not depend on
# their private API that may change at any time.
# This code should be reviewed and maybe removed when implementing
# https://jira.mongodb.org/browse/MONGOID-5832
class << self
# @api private
def __redefine(owner, name, value)
if owner.singleton_class?
owner.redefine_method(name) { value }
owner.send(:public, name)
end
owner.redefine_singleton_method(name) { value }
owner.singleton_class.send(:public, name)
owner.redefine_singleton_method("#{name}=") do |new_value|
if owner.equal?(self)
value = new_value
else
::Mongoid::Traversable.redefine(self, name, new_value)
end
end
owner.singleton_class.send(:public, "#{name}=")
end
end

# Class-level methods for the Traversable behavior.
module ClassMethods
Expand Down Expand Up @@ -105,11 +128,7 @@ def discriminator_key=(value)
if value
Mongoid::Fields::Validators::Macro.validate_field_name(self, value)
value = value.to_s
if defined?(::ActiveSupport::ClassAttribute)
::ActiveSupport::ClassAttribute.redefine(self, 'discriminator_key', value)
else
super
end
::Mongoid::Traversable.__redefine(self, 'discriminator_key', value)
else
# When discriminator key is set to nil, replace the class's definition
# of the discriminator key reader (provided by class_attribute earlier)
Expand Down
Loading