From f5ee855f439feb7958504dc1d8e62aad4da5fac6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 26 Mar 2010 14:41:40 +0100 Subject: Improve heuristic for railties default name, otherwise railties may be named :railtie, :engine and so on. --- railties/lib/rails/engine.rb | 2 +- railties/lib/rails/railtie.rb | 17 ++++++++++------- railties/test/railties/railtie_test.rb | 8 ++++++++ 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 85b4ff8470..e4888cdef7 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -14,7 +14,7 @@ module Rails alias :engine_names :railtie_names def inherited(base) - unless abstract_railtie?(base) + unless base.abstract_railtie? base.called_from = begin # Remove the line number from backtraces making sure we don't leave anything behind call_stack = caller.map { |p| p.split(':')[0..-2].join(':') } diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index be5d4ff260..b7f6c02503 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -9,7 +9,8 @@ module Rails include Initializable - ABSTRACT_RAILTIES = %w(Rails::Plugin Rails::Engine Rails::Application) + ABSTRACT_RAILTIES = %w(Rails::Railtie Rails::Plugin Rails::Engine Rails::Application) + RAILTIES_TYPES = ABSTRACT_RAILTIES.map { |r| r.split('::').last } class << self def subclasses @@ -17,7 +18,7 @@ module Rails end def inherited(base) - unless abstract_railtie?(base) + unless base.abstract_railtie? base.send(:include, self::Configurable) subclasses << base end @@ -52,14 +53,16 @@ module Rails @generators end - protected - - def abstract_railtie?(base) - ABSTRACT_RAILTIES.include?(base.name) + def abstract_railtie? + ABSTRACT_RAILTIES.include?(name) end + protected + def default_name - ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(name)) + namespaces = name.split("::") + namespaces.pop if RAILTIES_TYPES.include?(namespaces.last) + ActiveSupport::Inflector.underscore(namespaces.last).to_sym end end diff --git a/railties/test/railties/railtie_test.rb b/railties/test/railties/railtie_test.rb index 22fb0a2ec3..9283482893 100644 --- a/railties/test/railties/railtie_test.rb +++ b/railties/test/railties/railtie_test.rb @@ -15,6 +15,14 @@ module RailtiesTest @app ||= Rails.application end + test "name can be retrieved from namespaced railties" do + module ActiveRailtie + class Railtie < Rails::Railtie; end + end + + assert_equal :active_railtie, ActiveRailtie::Railtie.railtie_name + end + test "can find railtie by name" do class TieA < Rails::Railtie; railtie_name :railtie_a; end class TieB < Rails::Railtie; railtie_name :railtie_b; end -- cgit v1.2.3