public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Added view path support for engines [DHH]
dhh (author)
Thu Nov 27 09:59:24 -0800 2008
commit  f2ee056873b84f8917e72d87181e1a9f5f653342
tree    47c47835a9f34d065b85f3d1d68f9e21e78649f4
parent  229f959d15e451890db60dbb73f8565079977814
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *2.3.0 [Edge]*
0
 
0
+* Added view path support for engines [DHH]
0
+
0
 * Added that config/routes.rb files in engine plugins are automatically loaded (and reloaded when they change in dev mode) [DHH]
0
 
0
 * Added app/[models|controllers|helpers] to the load path for plugins that has an app directory (go engines ;)) [DHH]
...
487
488
489
490
491
492
493
494
495
 
 
496
497
498
...
487
488
489
 
 
 
 
 
 
490
491
492
493
494
0
@@ -487,12 +487,8 @@ Run `rake gems:install` to install the missing gems.
0
     def initialize_routing
0
       return unless configuration.frameworks.include?(:action_controller)
0
 
0
-      ActionController::Routing.controller_paths = configuration.controller_paths + plugin_loader.controller_paths
0
-
0
-      ([ configuration.routes_configuration_file ] + plugin_loader.routing_files).each do |routing_file|
0
-        ActionController::Routing::Routes.add_configuration_file(routing_file)
0
-      end
0
-
0
+      ActionController::Routing.controller_paths += configuration.controller_paths
0
+      ActionController::Routing::Routes.add_configuration_file(configuration.routes_configuration_file)
0
       ActionController::Routing::Routes.reload
0
     end
0
 
...
71
72
73
 
 
 
 
 
74
75
76
...
95
96
97
98
99
100
101
102
 
103
104
105
...
71
72
73
74
75
76
77
78
79
80
81
...
100
101
102
 
 
 
 
 
103
104
105
106
0
@@ -71,6 +71,11 @@ module Rails
0
       File.exist?(routing_file)
0
     end
0
 
0
+
0
+    def view_path
0
+      File.join(directory, 'app', 'views')
0
+    end
0
+
0
     def controller_path
0
       File.join(directory, 'app', 'controllers')
0
     end
0
@@ -95,11 +100,7 @@ module Rails
0
 
0
       
0
       def app_paths
0
-        [ 
0
-          File.join(directory, 'app', 'models'), 
0
-          File.join(directory, 'app', 'controllers'),
0
-          File.join(directory, 'app', 'helpers')
0
-        ]
0
+        [ File.join(directory, 'app', 'models'), File.join(directory, 'app', 'helpers'), controller_path ]
0
       end
0
       
0
       def lib_path
...
39
40
41
 
 
42
43
44
...
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
 
 
 
 
 
 
 
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
80
81
...
39
40
41
42
43
44
45
46
...
65
66
67
 
 
 
 
 
 
 
 
 
 
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
0
@@ -39,6 +39,8 @@ module Rails
0
           register_plugin_as_loaded(plugin)
0
         end
0
 
0
+        configure_engines
0
+
0
         ensure_all_registered_plugins_are_loaded!
0
       end
0
       
0
@@ -63,19 +65,31 @@ module Rails
0
         $LOAD_PATH.uniq!
0
       end
0
       
0
-      # Returns an array of all the controller paths found inside engine-type plugins.
0
-      def controller_paths
0
-        engines.collect(&:controller_path)
0
-      end
0
-      
0
-      # Returns an array of routing.rb files from all the plugins that include config/routes.rb
0
-      def routing_files
0
-        plugins.select(&:routed?).collect(&:routing_file)
0
-      end
0
-      
0
       
0
       protected
0
+        def configure_engines
0
+          if engines.any?
0
+            add_engine_routing_configurations
0
+            add_engine_controller_paths
0
+            add_engine_view_paths
0
+          end
0
+        end
0
       
0
+        def add_engine_routing_configurations
0
+          engines.select(&:routed?).collect(&:routing_file).each do |routing_file|
0
+            ActionController::Routing::Routes.add_configuration_file(routing_file)
0
+          end
0
+        end
0
+        
0
+        def add_engine_controller_paths
0
+          ActionController::Routing.controller_paths += engines.collect(&:controller_path)
0
+        end
0
+        
0
+        def add_engine_view_paths
0
+          # reverse it such that the last engine can overwrite view paths from the first, like with routes
0
+          ActionController::Base.view_paths += ActionView::PathSet.new(engines.collect(&:view_path).reverse)
0
+        end
0
+
0
         # The locate_plugins method uses each class in config.plugin_locators to
0
         # find the set of all plugins available to this Rails application.
0
         def locate_plugins
...
1
 
2
3
...
 
1
2
3
0
@@ -1,2 +1,2 @@
0
-class EngineController < ActionController::Base
0
+class EngineController
0
 end
0
\ No newline at end of file
...
1
2
 
 
 
3
4
5
...
7
8
9
10
11
12
13
14
15
16
 
 
17
18
19
20
21
22
 
 
23
24
 
 
 
 
 
25
26
27
 
28
29
30
31
32
33
34
35
36
 
 
37
38
39
40
41
42
43
 
 
 
 
 
 
 
 
44
45
46
47
48
 
 
 
 
 
 
49
50
51
52
 
 
 
 
53
54
55
56
 
 
 
57
58
59
60
61
 
 
 
62
63
64
65
66
67
 
 
 
 
68
69
70
71
72
73
 
 
 
 
 
74
75
76
77
78
 
 
 
 
 
79
80
81
82
83
 
 
 
 
84
85
86
87
88
 
 
 
 
89
90
91
92
93
 
 
 
 
94
95
96
97
98
 
 
 
 
99
100
 
 
 
 
101
102
103
104
 
105
106
107
 
 
 
108
109
 
 
110
111
112
113
 
114
 
 
 
115
116
117
 
 
118
119
 
120
121
122
123
124
125
 
 
 
 
126
 
 
 
 
127
128
129
130
131
 
 
 
 
132
133
134
135
 
 
136
137
138
139
140
 
141
142
 
 
 
143
144
145
 
 
 
 
146
147
 
148
149
150
151
152
 
153
154
155
 
 
 
 
 
 
 
156
...
1
2
3
4
5
6
7
8
...
10
11
12
 
 
 
 
 
 
 
13
14
15
 
 
 
 
 
16
17
18
 
19
20
21
22
23
24
 
 
25
26
 
 
 
 
 
 
 
 
27
28
29
 
 
 
 
 
 
30
31
32
33
34
35
36
37
38
 
 
 
 
39
40
41
42
43
44
45
 
 
 
46
47
48
49
50
 
 
 
51
52
53
54
 
 
 
 
55
56
57
58
 
 
 
 
 
59
60
61
62
63
 
 
 
 
 
64
65
66
67
68
69
 
 
 
 
70
71
72
73
74
75
 
 
 
 
76
77
78
79
80
 
 
 
 
81
82
83
84
85
 
 
 
 
86
87
88
89
90
 
 
 
 
91
92
93
94
95
 
96
97
98
99
100
 
 
 
101
102
 
 
103
104
105
106
 
107
108
109
 
 
 
110
111
112
113
114
115
 
 
116
117
118
 
119
120
 
 
 
 
 
121
122
123
124
125
126
127
128
129
130
 
 
 
 
131
132
133
134
135
 
 
 
136
137
138
 
 
 
 
139
140
 
141
142
143
144
 
 
145
146
147
148
149
 
150
151
 
 
 
 
152
153
154
 
155
156
157
158
159
160
161
162
0
@@ -1,5 +1,8 @@
0
 require 'plugin_test_helper'
0
 
0
+$:.unshift File.dirname(__FILE__) + "/../../actionpack/lib"
0
+require 'action_controller'
0
+
0
 # Mocks out the configuration
0
 module Rails
0
   def self.configuration
0
@@ -7,149 +10,152 @@ module Rails
0
   end
0
 end
0
 
0
-uses_mocha "Plugin Loader Tests" do
0
-
0
-  class TestPluginLoader < Test::Unit::TestCase
0
-    ORIGINAL_LOAD_PATH = $LOAD_PATH.dup
0
-
0
-    def setup
0
-      reset_load_path!
0
+class TestPluginLoader < Test::Unit::TestCase
0
+  ORIGINAL_LOAD_PATH = $LOAD_PATH.dup
0
 
0
-      @configuration     = Rails::Configuration.new
0
-      @configuration.plugin_paths << plugin_fixture_root_path
0
-      @initializer       = Rails::Initializer.new(@configuration)
0
-      @valid_plugin_path = plugin_fixture_path('default/stubby')
0
-      @empty_plugin_path = plugin_fixture_path('default/empty')
0
+  def setup
0
+    reset_load_path!
0
 
0
-      @failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
0
+    @configuration     = Rails::Configuration.new
0
+    @configuration.plugin_paths << plugin_fixture_root_path
0
+    @initializer       = Rails::Initializer.new(@configuration)
0
+    @valid_plugin_path = plugin_fixture_path('default/stubby')
0
+    @empty_plugin_path = plugin_fixture_path('default/empty')
0
 
0
-      @loader = Rails::Plugin::Loader.new(@initializer)
0
-    end
0
+    @failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
0
 
0
-    def test_should_locate_plugins_by_asking_each_locator_specifed_in_configuration_for_its_plugins_result
0
-      locator_1 = stub(:plugins => [:a, :b, :c])
0
-      locator_2 = stub(:plugins => [:d, :e, :f])
0
-      locator_class_1 = stub(:new => locator_1)
0
-      locator_class_2 = stub(:new => locator_2)
0
-      @configuration.plugin_locators = [locator_class_1, locator_class_2]
0
-      assert_equal [:a, :b, :c, :d, :e, :f], @loader.send(:locate_plugins)
0
-    end
0
+    @loader = Rails::Plugin::Loader.new(@initializer)
0
+  end
0
 
0
-    def test_should_memoize_the_result_of_locate_plugins_as_all_plugins
0
-      plugin_list = [:a, :b, :c]
0
-      @loader.expects(:locate_plugins).once.returns(plugin_list)
0
-      assert_equal plugin_list, @loader.all_plugins
0
-      assert_equal plugin_list, @loader.all_plugins # ensuring that locate_plugins isn't called again
0
-    end
0
+  def test_should_locate_plugins_by_asking_each_locator_specifed_in_configuration_for_its_plugins_result
0
+    locator_1 = stub(:plugins => [:a, :b, :c])
0
+    locator_2 = stub(:plugins => [:d, :e, :f])
0
+    locator_class_1 = stub(:new => locator_1)
0
+    locator_class_2 = stub(:new => locator_2)
0
+    @configuration.plugin_locators = [locator_class_1, locator_class_2]
0
+    assert_equal [:a, :b, :c, :d, :e, :f], @loader.send(:locate_plugins)
0
+  end
0
 
0
-    def test_should_return_empty_array_if_configuration_plugins_is_empty
0
-      @configuration.plugins = []
0
-      assert_equal [], @loader.plugins
0
-    end
0
+  def test_should_memoize_the_result_of_locate_plugins_as_all_plugins
0
+    plugin_list = [:a, :b, :c]
0
+    @loader.expects(:locate_plugins).once.returns(plugin_list)
0
+    assert_equal plugin_list, @loader.all_plugins
0
+    assert_equal plugin_list, @loader.all_plugins # ensuring that locate_plugins isn't called again
0
+  end
0
 
0
-    def test_should_find_all_availble_plugins_and_return_as_all_plugins
0
-      assert_plugins [ :engine, :stubby, :plugin_with_no_lib_dir, :gemlike, :acts_as_chunky_bacon, :a], @loader.all_plugins.reverse, @failure_tip
0
-    end
0
+  def test_should_return_empty_array_if_configuration_plugins_is_empty
0
+    @configuration.plugins = []
0
+    assert_equal [], @loader.plugins
0
+  end
0
 
0
-    def test_should_return_all_plugins_as_plugins_when_registered_plugin_list_is_untouched
0
-      assert_plugins [:a, :acts_as_chunky_bacon, :engine, :gemlike, :plugin_with_no_lib_dir, :stubby], @loader.plugins, @failure_tip
0
-    end
0
+  def test_should_find_all_availble_plugins_and_return_as_all_plugins
0
+    assert_plugins [ :engine, :stubby, :plugin_with_no_lib_dir, :gemlike, :acts_as_chunky_bacon, :a], @loader.all_plugins.reverse, @failure_tip
0
+  end
0
 
0
-    def test_should_return_all_plugins_as_plugins_when_registered_plugin_list_is_nil
0
-      @configuration.plugins = nil
0
-      assert_plugins [:a, :acts_as_chunky_bacon, :engine, :gemlike, :plugin_with_no_lib_dir, :stubby], @loader.plugins, @failure_tip
0
-    end
0
+  def test_should_return_all_plugins_as_plugins_when_registered_plugin_list_is_untouched
0
+    assert_plugins [:a, :acts_as_chunky_bacon, :engine, :gemlike, :plugin_with_no_lib_dir, :stubby], @loader.plugins, @failure_tip
0
+  end
0
 
0
-    def test_should_return_specific_plugins_named_in_config_plugins_array_if_set
0
-      plugin_names = [:acts_as_chunky_bacon, :stubby]
0
-      only_load_the_following_plugins! plugin_names
0
-      assert_plugins plugin_names, @loader.plugins
0
-    end
0
+  def test_should_return_all_plugins_as_plugins_when_registered_plugin_list_is_nil
0
+    @configuration.plugins = nil
0
+    assert_plugins [:a, :acts_as_chunky_bacon, :engine, :gemlike, :plugin_with_no_lib_dir, :stubby], @loader.plugins, @failure_tip
0
+  end
0
 
0
-    def test_should_respect_the_order_of_plugins_given_in_configuration
0
-      plugin_names = [:stubby, :acts_as_chunky_bacon]
0
-      only_load_the_following_plugins! plugin_names
0
-      assert_plugins plugin_names, @loader.plugins
0
-    end
0
+  def test_should_return_specific_plugins_named_in_config_plugins_array_if_set
0
+    plugin_names = [:acts_as_chunky_bacon, :stubby]
0
+    only_load_the_following_plugins! plugin_names
0
+    assert_plugins plugin_names, @loader.plugins
0
+  end
0
 
0
-    def test_should_load_all_plugins_in_natural_order_when_all_is_used
0
-      only_load_the_following_plugins! [:all]
0
-      assert_plugins [:a, :acts_as_chunky_bacon, :engine, :gemlike, :plugin_with_no_lib_dir, :stubby], @loader.plugins, @failure_tip
0
-    end
0
+  def test_should_respect_the_order_of_plugins_given_in_configuration
0
+    plugin_names = [:stubby, :acts_as_chunky_bacon]
0
+    only_load_the_following_plugins! plugin_names
0
+    assert_plugins plugin_names, @loader.plugins
0
+  end
0
 
0
-    def test_should_load_specified_plugins_in_order_and_then_all_remaining_plugins_when_all_is_used
0
-      only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon, :all]
0
-      assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :engine, :gemlike, :plugin_with_no_lib_dir], @loader.plugins, @failure_tip
0
-    end
0
+  def test_should_load_all_plugins_in_natural_order_when_all_is_used
0
+    only_load_the_following_plugins! [:all]
0
+    assert_plugins [:a, :acts_as_chunky_bacon, :engine, :gemlike, :plugin_with_no_lib_dir, :stubby], @loader.plugins, @failure_tip
0
+  end
0
 
0
-    def test_should_be_able_to_specify_loading_of_plugins_loaded_after_all
0
-      only_load_the_following_plugins!  [:stubby, :all, :acts_as_chunky_bacon]
0
-      assert_plugins [:stubby, :a, :engine, :gemlike, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], @loader.plugins, @failure_tip
0
-    end
0
+  def test_should_load_specified_plugins_in_order_and_then_all_remaining_plugins_when_all_is_used
0
+    only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon, :all]
0
+    assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :engine, :gemlike, :plugin_with_no_lib_dir], @loader.plugins, @failure_tip
0
+  end
0
 
0
-    def test_should_accept_plugin_names_given_as_strings
0
-      only_load_the_following_plugins! ['stubby', 'acts_as_chunky_bacon', :a, :plugin_with_no_lib_dir]
0
-      assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :plugin_with_no_lib_dir], @loader.plugins, @failure_tip
0
-    end
0
+  def test_should_be_able_to_specify_loading_of_plugins_loaded_after_all
0
+    only_load_the_following_plugins!  [:stubby, :all, :acts_as_chunky_bacon]
0
+    assert_plugins [:stubby, :a, :engine, :gemlike, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], @loader.plugins, @failure_tip
0
+  end
0
 
0
-    def test_should_add_plugin_load_paths_to_global_LOAD_PATH_array
0
-      only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon]
0
-      stubbed_application_lib_index_in_LOAD_PATHS = 4
0
-      @loader.stubs(:application_lib_index).returns(stubbed_application_lib_index_in_LOAD_PATHS)
0
+  def test_should_accept_plugin_names_given_as_strings
0
+    only_load_the_following_plugins! ['stubby', 'acts_as_chunky_bacon', :a, :plugin_with_no_lib_dir]
0
+    assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :plugin_with_no_lib_dir], @loader.plugins, @failure_tip
0
+  end
0
 
0
-      @loader.add_plugin_load_paths
0
+  def test_should_add_plugin_load_paths_to_global_LOAD_PATH_array
0
+    only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon]
0
+    stubbed_application_lib_index_in_LOAD_PATHS = 4
0
+    @loader.stubs(:application_lib_index).returns(stubbed_application_lib_index_in_LOAD_PATHS)
0
 
0
-      assert $LOAD_PATH.index(File.join(plugin_fixture_path('default/stubby'), 'lib')) >= stubbed_application_lib_index_in_LOAD_PATHS
0
-      assert $LOAD_PATH.index(File.join(plugin_fixture_path('default/acts/acts_as_chunky_bacon'), 'lib')) >= stubbed_application_lib_index_in_LOAD_PATHS
0
-    end
0
+    @loader.add_plugin_load_paths
0
 
0
-    def test_should_add_plugin_load_paths_to_Dependencies_load_paths
0
-      only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon]
0
+    assert $LOAD_PATH.index(File.join(plugin_fixture_path('default/stubby'), 'lib')) >= stubbed_application_lib_index_in_LOAD_PATHS
0
+    assert $LOAD_PATH.index(File.join(plugin_fixture_path('default/acts/acts_as_chunky_bacon'), 'lib')) >= stubbed_application_lib_index_in_LOAD_PATHS
0
+  end
0
 
0
-      @loader.add_plugin_load_paths
0
+  def test_should_add_plugin_load_paths_to_Dependencies_load_paths
0
+    only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon]
0
 
0
-      assert ActiveSupport::Dependencies.load_paths.include?(File.join(plugin_fixture_path('default/stubby'), 'lib'))
0
-      assert ActiveSupport::Dependencies.load_paths.include?(File.join(plugin_fixture_path('default/acts/acts_as_chunky_bacon'), 'lib'))
0
-    end
0
+    @loader.add_plugin_load_paths
0
 
0
+    assert ActiveSupport::Dependencies.load_paths.include?(File.join(plugin_fixture_path('default/stubby'), 'lib'))
0
+    assert ActiveSupport::Dependencies.load_paths.include?(File.join(plugin_fixture_path('default/acts/acts_as_chunky_bacon'), 'lib'))
0
+  end
0
 
0
-    def test_should_add_engine_load_paths_to_Dependencies_load_paths
0
-      only_load_the_following_plugins! [:engine]
0
+  def test_should_add_engine_load_paths_to_Dependencies_load_paths
0
+    only_load_the_following_plugins! [:engine]
0
 
0
-      @loader.add_plugin_load_paths
0
+    @loader.add_plugin_load_paths
0
 
0
-      %w( models controllers helpers ).each do |app_part|
0
-        assert ActiveSupport::Dependencies.load_paths.include?(
0
-          File.join(plugin_fixture_path('engines/engine'), 'app', app_part)
0
-        ), "Couldn't find #{app_part} in load path"
0
-      end
0
+    %w( models controllers helpers ).each do |app_part|
0
+      assert ActiveSupport::Dependencies.load_paths.include?(
0
+        File.join(plugin_fixture_path('engines/engine'), 'app', app_part)
0
+      ), "Couldn't find #{app_part} in load path"
0
     end
0
+  end
0
+  
0
+  def test_engine_controllers_should_have_their_view_path_set_when_loaded
0
+    only_load_the_following_plugins!([ :engine ])
0
 
0
-    def test_should_add_plugin_load_paths_to_Dependencies_load_once_paths
0
-      only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon]
0
-
0
-      @loader.add_plugin_load_paths
0
+    @loader.send :add_engine_view_paths
0
+    
0
+    assert_equal [ File.join(plugin_fixture_path('engines/engine'), 'app', 'views') ], ActionController::Base.view_paths
0
+  end
0
 
0
-      assert ActiveSupport::Dependencies.load_once_paths.include?(File.join(plugin_fixture_path('default/stubby'), 'lib'))
0
-      assert ActiveSupport::Dependencies.load_once_paths.include?(File.join(plugin_fixture_path('default/acts/acts_as_chunky_bacon'), 'lib'))
0
-    end
0
+  def test_should_add_plugin_load_paths_to_Dependencies_load_once_paths
0
+    only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon]
0
 
0
-    def test_should_add_all_load_paths_from_a_plugin_to_LOAD_PATH_array
0
-      plugin_load_paths = ["a", "b"]
0
-      plugin = stub(:load_paths => plugin_load_paths)
0
-      @loader.stubs(:plugins).returns([plugin])
0
+    @loader.add_plugin_load_paths
0
 
0
-      @loader.add_plugin_load_paths
0
+    assert ActiveSupport::Dependencies.load_once_paths.include?(File.join(plugin_fixture_path('default/stubby'), 'lib'))
0
+    assert ActiveSupport::Dependencies.load_once_paths.include?(File.join(plugin_fixture_path('default/acts/acts_as_chunky_bacon'), 'lib'))
0
+  end
0
 
0
-      plugin_load_paths.each { |path| assert $LOAD_PATH.include?(path) }
0
-    end
0
+  def test_should_add_all_load_paths_from_a_plugin_to_LOAD_PATH_array
0
+    plugin_load_paths = ["a", "b"]
0
+    plugin = stub(:load_paths => plugin_load_paths)
0
+    @loader.stubs(:plugins).returns([plugin])
0
 
0
-    private
0
+    @loader.add_plugin_load_paths
0
 
0
-      def reset_load_path!
0
-        $LOAD_PATH.clear
0
-        ORIGINAL_LOAD_PATH.each { |path| $LOAD_PATH << path }
0
-      end
0
+    plugin_load_paths.each { |path| assert $LOAD_PATH.include?(path) }
0
   end
0
 
0
-end
0
+
0
+  private
0
+    def reset_load_path!
0
+      $LOAD_PATH.clear
0
+      ORIGINAL_LOAD_PATH.each { |path| $LOAD_PATH << path }
0
+    end
0
+end
0
\ No newline at end of file

Comments