Skip to content
This repository
Browse code

Revert "Make constantize look into ancestors"

[#410 state:open]

This reverts commit 87790e0.
  • Loading branch information...
commit d7b7ff0556ea5a66aeda455141ba06b558671e3d 1 parent 75f5596
Jeremy Kemper authored
65  activesupport/lib/active_support/inflector.rb
@@ -323,30 +323,47 @@ def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
323 323
       underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
324 324
     end
325 325
 
326  
-    # Tries to find a constant with the name specified in the argument string:
327  
-    #
328  
-    #   "Module".constantize     # => Module
329  
-    #   "Test::Unit".constantize # => Test::Unit
330  
-    #
331  
-    # The name is assumed to be the one of a top-level constant, no matter whether
332  
-    # it starts with "::" or not. No lexical context is taken into account:
333  
-    #
334  
-    #   C = 'outside'
335  
-    #   module M
336  
-    #     C = 'inside'
337  
-    #     C               # => 'inside'
338  
-    #     "C".constantize # => 'outside', same as ::C
339  
-    #   end
340  
-    #
341  
-    # NameError is raised when the name is not in CamelCase or the constant is
342  
-    # unknown.
343  
-    def constantize(camel_cased_word)
344  
-      names = camel_cased_word.split('::')
345  
-      names.shift if names.empty? || names.first.empty?
346  
-
347  
-      constant = Object
348  
-      names.each { |name| constant = constant.const_get(name) }
349  
-      constant
  326
+    # Ruby 1.9 introduces an inherit argument for Module#const_get and
  327
+    # #const_defined? and changes their default behavior.
  328
+    if Module.method(:const_get).arity == 1
  329
+      # Tries to find a constant with the name specified in the argument string:
  330
+      #
  331
+      #   "Module".constantize     # => Module
  332
+      #   "Test::Unit".constantize # => Test::Unit
  333
+      #
  334
+      # The name is assumed to be the one of a top-level constant, no matter whether
  335
+      # it starts with "::" or not. No lexical context is taken into account:
  336
+      #
  337
+      #   C = 'outside'
  338
+      #   module M
  339
+      #     C = 'inside'
  340
+      #     C               # => 'inside'
  341
+      #     "C".constantize # => 'outside', same as ::C
  342
+      #   end
  343
+      #
  344
+      # NameError is raised when the name is not in CamelCase or the constant is
  345
+      # unknown.
  346
+      def constantize(camel_cased_word)
  347
+        names = camel_cased_word.split('::')
  348
+        names.shift if names.empty? || names.first.empty?
  349
+
  350
+        constant = Object
  351
+        names.each do |name|
  352
+          constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
  353
+        end
  354
+        constant
  355
+      end
  356
+    else
  357
+      def constantize(camel_cased_word) #:nodoc:
  358
+        names = camel_cased_word.split('::')
  359
+        names.shift if names.empty? || names.first.empty?
  360
+
  361
+        constant = Object
  362
+        names.each do |name|
  363
+          constant = constant.const_get(name, false) || constant.const_missing(name)
  364
+        end
  365
+        constant
  366
+      end
350 367
     end
351 368
 
352 369
     # Turns a number into an ordinal string used to denote the position in an
17  activesupport/test/inflector_test.rb
@@ -2,21 +2,8 @@
2 2
 require 'inflector_test_cases'
3 3
 
4 4
 module Ace
5  
-  module Extension
6  
-    def self.included(base)
7  
-      base.extend(ClassMethods)
8  
-    end
9  
-
10  
-    module ClassMethods
11  
-      def mission_accomplished?
12  
-        false
13  
-      end
14  
-    end
15  
-  end
16  
-
17 5
   module Base
18 6
     class Case
19  
-      include Extension
20 7
     end
21 8
   end
22 9
 end
@@ -174,9 +161,7 @@ def test_constantize
174 161
   end
175 162
 
176 163
   def test_constantize_does_lexical_lookup
177  
-    assert_equal InflectorTest, ActiveSupport::Inflector.constantize("Ace::Base::InflectorTest")
178  
-    assert_nothing_raised { Ace::Base::Case::ClassMethods }
179  
-    assert_nothing_raised { assert_equal Ace::Base::Case::ClassMethods, ActiveSupport::Inflector.constantize("Ace::Base::Case::ClassMethods") }
  164
+    assert_raises(NameError) { ActiveSupport::Inflector.constantize("Ace::Base::InflectorTest") }
180 165
   end
181 166
 
182 167
   def test_ordinal

0 notes on commit d7b7ff0

Please sign in to comment.
Something went wrong with that request. Please try again.