Skip to content
This repository
Browse code

Merge pull request #9791 from wangjohn/remove_configurable

Removing Railtie::Configurable and replacing it with class methods
  • Loading branch information...
commit 8c3affeb76edf6cf887aa69e549f10895172f23b 2 parents feb44b9 + 81b7416
Rafael Mendonça França authored April 29, 2013
6  railties/CHANGELOG.md
Source Rendered
... ...
@@ -1,3 +1,7 @@
1  
-* No changes.
  1
+*   Rails::Railtie no longer forces the Rails::Configurable module on everything
  2
+    that subclassess it. Instead, the methods from Rails::Configurable have been
  3
+    moved to class methods in Railtie and the Railtie has been made abstract.
  4
+
  5
+    *John Wang*
2 6
 
3 7
 Please check [4-0-stable](https://github.com/rails/rails/blob/4-0-stable/railties/CHANGELOG.md) for previous changes.
8  railties/lib/rails/engine.rb
@@ -634,6 +634,10 @@ def load_seed
634 634
       end
635 635
     end
636 636
 
  637
+    def routes? #:nodoc:
  638
+      @routes
  639
+    end
  640
+
637 641
     protected
638 642
 
639 643
     def run_tasks_blocks(*) #:nodoc:
@@ -641,10 +645,6 @@ def run_tasks_blocks(*) #:nodoc:
641 645
       paths["lib/tasks"].existent.sort.each { |ext| load(ext) }
642 646
     end
643 647
 
644  
-    def routes? #:nodoc:
645  
-      @routes
646  
-    end
647  
-
648 648
     def has_migrations? #:nodoc:
649 649
       paths["db/migrate"].existent.any?
650 650
     end
36  railties/lib/rails/railtie.rb
@@ -112,7 +112,6 @@ module Rails
112 112
   # Be sure to look at the documentation of those specific classes for more information.
113 113
   #
114 114
   class Railtie
115  
-    autoload :Configurable,  "rails/railtie/configurable"
116 115
     autoload :Configuration, "rails/railtie/configuration"
117 116
 
118 117
     include Initializable
@@ -121,6 +120,7 @@ class Railtie
121 120
 
122 121
     class << self
123 122
       private :new
  123
+      delegate :config, to: :instance
124 124
 
125 125
       def subclasses
126 126
         @subclasses ||= []
@@ -128,7 +128,6 @@ def subclasses
128 128
 
129 129
       def inherited(base)
130 130
         unless base.abstract_railtie?
131  
-          base.send(:include, Railtie::Configurable)
132 131
           subclasses << base
133 132
         end
134 133
       end
@@ -166,14 +165,47 @@ def railtie_name(name = nil)
166 165
         @railtie_name ||= generate_railtie_name(self.name)
167 166
       end
168 167
 
  168
+      # Since Rails::Railtie cannot be instantiated, any methods that call
  169
+      # +instance+ are intended to be called only on subclasses of a Railtie.
  170
+      def instance
  171
+        @instance ||= new
  172
+      end
  173
+
  174
+      def respond_to_missing?(*args)
  175
+        instance.respond_to?(*args) || super
  176
+      end
  177
+
  178
+      # Allows you to configure the railtie. This is the same method seen in
  179
+      # Railtie::Configurable, but this module is no longer required for all
  180
+      # subclasses of Railtie so we provide the class method here.
  181
+      def configure(&block)
  182
+        class_eval(&block)
  183
+      end
  184
+
169 185
       protected
170 186
         def generate_railtie_name(class_or_module)
171 187
           ActiveSupport::Inflector.underscore(class_or_module).tr("/", "_")
172 188
         end
  189
+
  190
+        # If the class method does not have a method, then send the method call
  191
+        # to the Railtie instance.
  192
+        def method_missing(name, *args, &block)
  193
+          if instance.respond_to?(name)
  194
+            instance.public_send(name, *args, &block)
  195
+          else
  196
+            super
  197
+          end
  198
+        end
173 199
     end
174 200
 
175 201
     delegate :railtie_name, to: :class
176 202
 
  203
+    def initialize
  204
+      if self.class.abstract_railtie?
  205
+        raise "#{self.class.name} is abstract, you cannot instantiate it directly."
  206
+      end
  207
+    end
  208
+
177 209
     def config
178 210
       @config ||= Railtie::Configuration.new
179 211
     end
5  railties/test/railties/engine_test.rb
@@ -416,11 +416,6 @@ def call(env)
416 416
       boot_rails
417 417
     end
418 418
 
419  
-    test "Rails::Engine itself does not respond to config" do
420  
-      boot_rails
421  
-      assert !Rails::Engine.respond_to?(:config)
422  
-    end
423  
-
424 419
     test "initializers are executed after application configuration initializers" do
425 420
       @plugin.write "lib/bukkits.rb", <<-RUBY
426 421
         module Bukkits
11  railties/test/railties/railtie_test.rb
@@ -19,8 +19,8 @@ def app
19 19
       @app ||= Rails.application
20 20
     end
21 21
 
22  
-    test "Rails::Railtie itself does not respond to config" do
23  
-      assert !Rails::Railtie.respond_to?(:config)
  22
+    test "cannot instantiate a Railtie object" do
  23
+      assert_raise(RuntimeError) { Rails::Railtie.new }
24 24
     end
25 25
 
26 26
     test "Railtie provides railtie_name" do
@@ -39,13 +39,6 @@ class Foo < Rails::Railtie
39 39
       assert_equal "bar", Foo.railtie_name
40 40
     end
41 41
 
42  
-    test "cannot inherit from a railtie" do
43  
-      class Foo < Rails::Railtie ; end
44  
-      assert_raise RuntimeError do
45  
-        class Bar < Foo; end
46  
-      end
47  
-    end
48  
-
49 42
     test "config is available to railtie" do
50 43
       class Foo < Rails::Railtie ; end
51 44
       assert_nil Foo.config.action_controller.foo

0 notes on commit 8c3affe

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