aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/module/introspection.rb14
-rw-r--r--activesupport/lib/active_support/dependencies.rb6
-rw-r--r--activesupport/test/core_ext/module_test.rb8
-rw-r--r--activesupport/test/dependencies_test.rb11
5 files changed, 38 insertions, 3 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index a78c774634..ec9c94fe47 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Update Dependencies to ignore constants inherited from ancestors. Closes #6951. [Nicholas Seckar]
+
* Array#to_query preserves its ordering. #7756 [Greg Spurrier]
* Out-of-range Time calculations transparently overflow to DateTime. Introduce Time#to_datetime. #7706, #7715 [Geoff Buesing]
diff --git a/activesupport/lib/active_support/core_ext/module/introspection.rb b/activesupport/lib/active_support/core_ext/module/introspection.rb
index 0cd0d1ff2c..36481927aa 100644
--- a/activesupport/lib/active_support/core_ext/module/introspection.rb
+++ b/activesupport/lib/active_support/core_ext/module/introspection.rb
@@ -18,4 +18,18 @@ class Module
parents << Object unless parents.include? Object
parents
end
+
+ # Return the constants that have been defined locally by this object and not
+ # in an ancestor. This method may miss some constants if their definition in
+ # the ancestor is identical to their definition in the receiver.
+ def local_constants
+ inherited = {}
+ ancestors.each do |anc|
+ next if anc == self
+ anc.constants.each { |const| inherited[const] = anc.const_get(const) }
+ end
+ constants.select do |const|
+ ! inherited.key?(const) || inherited[const].object_id != const_get(const).object_id
+ end
+ end
end
diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb
index 4880b70804..2f6ae284bf 100644
--- a/activesupport/lib/active_support/dependencies.rb
+++ b/activesupport/lib/active_support/dependencies.rb
@@ -318,13 +318,13 @@ module Dependencies #:nodoc:
watch_frames = descs.collect do |desc|
if desc.is_a? Module
mod_name = desc.name
- initial_constants = desc.constants
+ initial_constants = desc.local_constants
elsif desc.is_a?(String) || desc.is_a?(Symbol)
mod_name = desc.to_s
# Handle the case where the module has yet to be defined.
initial_constants = if qualified_const_defined?(mod_name)
- mod_name.constantize.constants
+ mod_name.constantize.local_constants
else
[]
end
@@ -349,7 +349,7 @@ module Dependencies #:nodoc:
mod = mod_name.constantize
next [] unless mod.is_a? Module
- new_constants = mod.constants - prior_constants
+ new_constants = mod.local_constants - prior_constants
# Make sure no other frames takes credit for these constants.
constant_watch_stack.each do |frame_name, constants|
diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb
index bbf6dd1640..884b44030c 100644
--- a/activesupport/test/core_ext/module_test.rb
+++ b/activesupport/test/core_ext/module_test.rb
@@ -1,10 +1,14 @@
require File.dirname(__FILE__) + '/../abstract_unit'
module One
+ Constant1 = "Hello World"
+ Constant2 = "What's up?"
end
class Ab
include One
+ Constant1 = "Hello World" # Will have different object id than One::Constant1
+ Constant3 = "Goodbye World"
end
module Xy
@@ -91,6 +95,10 @@ class ModuleTest < Test::Unit::TestCase
assert_equal [Yz::Zy, Yz, Object], Yz::Zy::Cd.parents
assert_equal [Yz, Object], Yz::Zy.parents
end
+
+ def test_local_constants
+ assert_equal %w(Constant1 Constant3), Ab.local_constants.sort
+ end
def test_as_load_path
assert_equal 'yz/zy', Yz::Zy.as_load_path
diff --git a/activesupport/test/dependencies_test.rb b/activesupport/test/dependencies_test.rb
index 755cff2e3f..ad3472bddb 100644
--- a/activesupport/test/dependencies_test.rb
+++ b/activesupport/test/dependencies_test.rb
@@ -9,6 +9,10 @@ module ModuleWithMissing
end
end
+module ModuleWithConstant
+ InheritedConstant = "Hello"
+end
+
class DependenciesTest < Test::Unit::TestCase
def teardown
Dependencies.clear
@@ -574,6 +578,13 @@ class DependenciesTest < Test::Unit::TestCase
Object.send :remove_const, :M rescue nil
end
+ def test_new_constants_in_with_inherited_constants
+ m = Dependencies.new_constants_in(:Object) do
+ Object.send :include, ModuleWithConstant
+ end
+ assert_equal [], m
+ end
+
def test_file_with_multiple_constants_and_require_dependency
with_loading 'autoloading_fixtures' do
assert ! defined?(MultipleConstantFile)