From 81b7416afa10934022ad8b1b486419d5a4ed1349 Mon Sep 17 00:00:00 2001 From: wangjohn Date: Mon, 18 Mar 2013 22:12:35 -0400 Subject: Removing Railtie::Configurable from the base Railtie object and making Railtie itself abstract. This stops the weird behavior of forcing subclasses of Railtie to include the Configurable module. --- railties/CHANGELOG.md | 6 +++++- railties/lib/rails/engine.rb | 8 ++++---- railties/lib/rails/railtie.rb | 36 ++++++++++++++++++++++++++++++++-- railties/test/railties/engine_test.rb | 5 ----- railties/test/railties/railtie_test.rb | 11 ++--------- 5 files changed, 45 insertions(+), 21 deletions(-) diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index ba914fb12e..e1b98eda55 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,3 +1,7 @@ -* No changes. +* Rails::Railtie no longer forces the Rails::Configurable module on everything + that subclassess it. Instead, the methods from Rails::Configurable have been + moved to class methods in Railtie and the Railtie has been made abstract. + + *John Wang* Please check [4-0-stable](https://github.com/rails/rails/blob/4-0-stable/railties/CHANGELOG.md) for previous changes. diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 86f62dfb40..94edcd352a 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -634,6 +634,10 @@ module Rails end end + def routes? #:nodoc: + @routes + end + protected def run_tasks_blocks(*) #:nodoc: @@ -641,10 +645,6 @@ module Rails paths["lib/tasks"].existent.sort.each { |ext| load(ext) } end - def routes? #:nodoc: - @routes - end - def has_migrations? #:nodoc: paths["db/migrate"].existent.any? end diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index 9437e9c406..2b6ad1ff2d 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -112,7 +112,6 @@ module Rails # Be sure to look at the documentation of those specific classes for more information. # class Railtie - autoload :Configurable, "rails/railtie/configurable" autoload :Configuration, "rails/railtie/configuration" include Initializable @@ -121,6 +120,7 @@ module Rails class << self private :new + delegate :config, to: :instance def subclasses @subclasses ||= [] @@ -128,7 +128,6 @@ module Rails def inherited(base) unless base.abstract_railtie? - base.send(:include, Railtie::Configurable) subclasses << base end end @@ -166,14 +165,47 @@ module Rails @railtie_name ||= generate_railtie_name(self.name) end + # Since Rails::Railtie cannot be instantiated, any methods that call + # +instance+ are intended to be called only on subclasses of a Railtie. + def instance + @instance ||= new + end + + def respond_to_missing?(*args) + instance.respond_to?(*args) || super + end + + # Allows you to configure the railtie. This is the same method seen in + # Railtie::Configurable, but this module is no longer required for all + # subclasses of Railtie so we provide the class method here. + def configure(&block) + class_eval(&block) + end + protected def generate_railtie_name(class_or_module) ActiveSupport::Inflector.underscore(class_or_module).tr("/", "_") end + + # If the class method does not have a method, then send the method call + # to the Railtie instance. + def method_missing(name, *args, &block) + if instance.respond_to?(name) + instance.public_send(name, *args, &block) + else + super + end + end end delegate :railtie_name, to: :class + def initialize + if self.class.abstract_railtie? + raise "#{self.class.name} is abstract, you cannot instantiate it directly." + end + end + def config @config ||= Railtie::Configuration.new end diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb index 01fa2c6864..0948ae59c0 100644 --- a/railties/test/railties/engine_test.rb +++ b/railties/test/railties/engine_test.rb @@ -416,11 +416,6 @@ YAML boot_rails end - test "Rails::Engine itself does not respond to config" do - boot_rails - assert !Rails::Engine.respond_to?(:config) - end - test "initializers are executed after application configuration initializers" do @plugin.write "lib/bukkits.rb", <<-RUBY module Bukkits diff --git a/railties/test/railties/railtie_test.rb b/railties/test/railties/railtie_test.rb index 0786b8f8c7..520a855c90 100644 --- a/railties/test/railties/railtie_test.rb +++ b/railties/test/railties/railtie_test.rb @@ -19,8 +19,8 @@ module RailtiesTest @app ||= Rails.application end - test "Rails::Railtie itself does not respond to config" do - assert !Rails::Railtie.respond_to?(:config) + test "cannot instantiate a Railtie object" do + assert_raise(RuntimeError) { Rails::Railtie.new } end test "Railtie provides railtie_name" do @@ -39,13 +39,6 @@ module RailtiesTest assert_equal "bar", Foo.railtie_name end - test "cannot inherit from a railtie" do - class Foo < Rails::Railtie ; end - assert_raise RuntimeError do - class Bar < Foo; end - end - end - test "config is available to railtie" do class Foo < Rails::Railtie ; end assert_nil Foo.config.action_controller.foo -- cgit v1.2.3