Skip to content

Commit

Permalink
Merge pull request #45251 from jhawthorn/deprecated_constant_for_module
Browse files Browse the repository at this point in the history
Fix includes of deprecation proxy modules
  • Loading branch information
jhawthorn committed Jun 6, 2022
2 parents 889c877 + c81a9bc commit 2c1c65b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
15 changes: 15 additions & 0 deletions activesupport/lib/active_support/deprecation/proxy_wrappers.rb
Expand Up @@ -158,6 +158,21 @@ def class
target.class
end

def append_features(base)
@deprecator.warn(@message, caller_locations)
base.include(target)
end

def prepend_features(base)
@deprecator.warn(@message, caller_locations)
base.prepend(target)
end

def extended(base)
@deprecator.warn(@message, caller_locations)
base.extend(target)
end

private
def target
ActiveSupport::Inflector.constantize(@new_const.to_s)
Expand Down
37 changes: 37 additions & 0 deletions activesupport/test/deprecation/proxy_wrappers_test.rb
Expand Up @@ -7,6 +7,12 @@ class ProxyWrappersTest < ActiveSupport::TestCase
Waffles = false
NewWaffles = :hamburgers

module WaffleModule
def waffle?
true
end
end

def test_deprecated_object_proxy_doesnt_wrap_falsy_objects
proxy = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(nil, "message")
assert_not proxy
Expand All @@ -21,4 +27,35 @@ def test_deprecated_constant_proxy_doesnt_wrap_falsy_objects
proxy = ActiveSupport::Deprecation::DeprecatedConstantProxy.new(Waffles, NewWaffles)
assert_not proxy
end

def test_including_proxy_module
proxy = ActiveSupport::Deprecation::DeprecatedConstantProxy.new("OldWaffleModule", WaffleModule.name)
klass = Class.new
assert_deprecated do
klass.include proxy
end
assert klass.new.waffle?
end

def test_prepending_proxy_module
proxy = ActiveSupport::Deprecation::DeprecatedConstantProxy.new("OldWaffleModule", WaffleModule.name)
klass = Class.new do
def waffle?
false
end
end
assert_deprecated do
klass.prepend proxy
end
assert klass.new.waffle?
end

def test_extending_proxy_module
proxy = ActiveSupport::Deprecation::DeprecatedConstantProxy.new("OldWaffleModule", WaffleModule.name)
obj = Object.new
assert_deprecated do
obj.extend proxy
end
assert obj.waffle?
end
end
2 changes: 1 addition & 1 deletion railties/lib/rails/generators/testing/behavior.rb
Expand Up @@ -108,7 +108,7 @@ def migration_file_name(relative)
end
end

Behaviour = ActiveSupport::Deprecation::DeprecatedConstantProxy.new("behavior", "Behavior")
Behaviour = ActiveSupport::Deprecation::DeprecatedConstantProxy.new("Behaviour", "Behavior")
end
end
end

0 comments on commit 2c1c65b

Please sign in to comment.