diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 99a3019f3a..32f41934f1 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -1,6 +1,7 @@ require 'rack/mount' require 'forwardable' require 'active_support/core_ext/object/to_query' +require 'active_support/core_ext/hash/slice' module ActionDispatch module Routing diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 0e85e6d1d5..2db131261c 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -85,13 +85,24 @@ module Rails end def reload_routes! - _routes = self.routes - _routes.disable_clear_and_finalize = true - _routes.clear! + routes_to_reload.each do |_routes, draw_block| + _routes = self.routes + _routes.disable_clear_and_finalize = true + _routes.clear! + _routes.draw(&draw_block) if draw_block + end routes_reloader.paths.each { |path| load(path) } - ActiveSupport.on_load(:action_controller) { _routes.finalize! } + routes_to_reload.each do |_routes, draw_block| + ActiveSupport.on_load(:action_controller) { _routes.finalize! } + end ensure - _routes.disable_clear_and_finalize = false + routes_to_reload.each do |_routes, draw_block| + _routes.disable_clear_and_finalize = false + end + end + + def routes_to_reload + @routes_to_reload ||= {} end def initialize! diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index ababf6a242..aa82a82b19 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -399,8 +399,10 @@ module Rails } end - def routes + def routes(&block) @routes ||= ActionDispatch::Routing::RouteSet.new + self.routes_draw_block = block if block_given? + @routes end def initializers @@ -447,6 +449,7 @@ module Rails end initializer :add_routing_paths do |app| + app.routes_to_reload[self.routes] = routes_draw_block paths.config.routes.to_a.each do |route| app.routes_reloader.paths.unshift(route) if File.exists?(route) end @@ -499,6 +502,8 @@ module Rails end protected + attr_accessor :routes_draw_block + def find_root_with_flag(flag, default=nil) root_path = self.class.called_from diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb index 17bffe05e1..2b9d728b0c 100644 --- a/railties/test/railties/engine_test.rb +++ b/railties/test/railties/engine_test.rb @@ -663,5 +663,45 @@ module RailtiesTest assert_equal AppTemplate._railtie, AppTemplate::Engine end + + test "properly reload routes" do + # when routes are inside application class definition + # they should not be reloaded when engine's routes + # file has changed + add_to_config <<-RUBY + routes do + mount lambda{|env| [200, {}, ["foo"]]} => "/foo" + mount Bukkits::Engine => "/bukkits" + end + RUBY + + FileUtils.rm(File.join(app_path, "config/routes.rb")) + + @plugin.write "config/routes.rb", <<-RUBY + Bukkits::Engine.routes.draw do + mount lambda{|env| [200, {}, ["bar"]]} => "/bar" + end + RUBY + + @plugin.write "lib/bukkits.rb", <<-RUBY + module Bukkits + class Engine < ::Rails::Engine + namespace(Bukkits) + end + end + RUBY + + require 'rack/test' + extend Rack::Test::Methods + + boot_rails + + require "#{rails_root}/config/environment" + get "/foo" + assert_equal "foo", last_response.body + + get "/bukkits/bar" + assert_equal "bar", last_response.body + end end end