Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge [6426] to stable

git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/1-2-stable@6428 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit b41920de18681b49582ab4326a88d9b6d7b2e3d5 1 parent 93d7ab4
Nicholas Seckar seckar authored
5 activesupport/CHANGELOG
View
@@ -1,3 +1,8 @@
+*SVN*
+
+* Update Dependencies to ignore constants inherited from ancestors. Closes #6951. [Nicholas Seckar]
+
+
*1.4.2* (March 12th, 2007)
* Ruby 1.8.6 and 1.9 define private Time#to_date and #to_datetime; make them
14 activesupport/lib/active_support/core_ext/module/introspection.rb
View
@@ -18,4 +18,18 @@ def parents
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
6 activesupport/lib/active_support/dependencies.rb
View
@@ -318,13 +318,13 @@ def new_constants_in(*descs)
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 @@ def new_constants_in(*descs)
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|
8 activesupport/test/core_ext/module_test.rb
View
@@ -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 @@ def test_parents
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
11 activesupport/test/dependencies_test.rb
View
@@ -9,6 +9,10 @@ def self.const_missing(name)
end
end
+module ModuleWithConstant
+ InheritedConstant = "Hello"
+end
+
class DependenciesTest < Test::Unit::TestCase
def teardown
Dependencies.clear
@@ -574,6 +578,13 @@ def test_new_constants_in_module_using_name
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)
Please sign in to comment.
Something went wrong with that request. Please try again.