Skip to content

Commit

Permalink
MONGOID-5831 Remove usage of Rails private API (#5908) (#5926)
Browse files Browse the repository at this point in the history
  • Loading branch information
comandeo-mongo authored Dec 19, 2024
1 parent c3054b3 commit 454b171
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions lib/mongoid/traversable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,29 @@ module Mongoid
# Provides behavior 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

def _parent
@__parent ||= nil
Expand All @@ -30,11 +53,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

0 comments on commit 454b171

Please sign in to comment.