Ticket #9815: inflector-route-reloading.diff
| File inflector-route-reloading.diff, 4.5 kB (added by mislav, 10 months ago) |
|---|
-
test/controller/routing_test.rb
old new 2031 2031 end 2032 2032 2033 2033 end 2034 2035 uses_mocha 'route loading' do 2036 class RouteLoadingTest < Test::Unit::TestCase 2037 2038 def setup 2039 routes.instance_variable_set '@routes_last_modified', nil 2040 silence_warnings { Object.const_set :RAILS_ROOT, '.' } 2041 2042 @stat = stub_everything 2043 end 2044 2045 def teardown 2046 Object.send :remove_const, :RAILS_ROOT 2047 end 2048 2049 def test_load 2050 File.expects(:stat).returns(@stat) 2051 routes.expects(:load).with(regexp_matches(/routes\.rb$/)) 2052 2053 routes.reload 2054 end 2055 2056 def test_no_reload_when_not_modified 2057 @stat.expects(:mtime).times(2).returns(1) 2058 File.expects(:stat).times(2).returns(@stat) 2059 routes.expects(:load).with(regexp_matches(/routes\.rb$/)).at_most_once 2060 2061 2.times { routes.reload } 2062 end 2063 2064 def test_reload_when_modified 2065 @stat.expects(:mtime).at_least(2).returns(1, 2) 2066 File.expects(:stat).at_least(2).returns(@stat) 2067 routes.expects(:load).with(regexp_matches(/routes\.rb$/)).times(2) 2068 2069 2.times { routes.reload } 2070 end 2071 2072 def test_bang_forces_reload 2073 @stat.expects(:mtime).at_least(2).returns(1) 2074 File.expects(:stat).at_least(2).returns(@stat) 2075 routes.expects(:load).with(regexp_matches(/routes\.rb$/)).times(2) 2076 2077 2.times { routes.reload! } 2078 end 2079 2080 def test_adding_inflections_forces_reload 2081 Inflector::Inflections.instance.expects(:uncountable).with('equipment') 2082 routes.expects(:reload!) 2083 2084 Inflector.inflections { |inflect| inflect.uncountable('equipment') } 2085 end 2086 2087 private 2088 def routes 2089 ActionController::Routing::Routes 2090 end 2091 2092 end 2093 end -
lib/action_controller/routing.rb
old new 196 196 # 197 197 # == Route globbing 198 198 # 199 # Specifying <tt>*[string]</tt> as part of a rule like :199 # Specifying <tt>*[string]</tt> as part of a rule like: 200 200 # 201 201 # map.connect '*path' , :controller => 'blog' , :action => 'unrecognized?' 202 202 # 203 # will glob all remaining parts of the route that were not recognized earlier. This idiom must appear at the end of the path. The globbed values are in <tt>params[:path]</tt> in this case. 203 # will glob all remaining parts of the route that were not recognized earlier. This idiom 204 # must appear at the end of the path. The globbed values are in <tt>params[:path]</tt> in 205 # this case. 204 206 # 205 207 # == Reloading routes 206 208 # … … 208 210 # 209 211 # ActionController::Routing::Routes.reload 210 212 # 211 # This will clear all named routes and reload routes.rb 213 # This will clear all named routes and reload routes.rb if the file has been modified from 214 # last load. To absolutely force reloading, use +reload!+. 212 215 # 213 216 # == Testing Routes 214 217 # … … 1248 1251 alias reload! load! 1249 1252 1250 1253 def reload 1251 if @routes_last_modified 1252 mtime =File.stat("#{RAILS_ROOT}/config/routes.rb").mtime1254 if @routes_last_modified && defined?(RAILS_ROOT) 1255 mtime = File.stat("#{RAILS_ROOT}/config/routes.rb").mtime 1253 1256 # if it hasn't been changed, then just return 1254 1257 return if mtime == @routes_last_modified 1255 1258 # if it has changed then record the new time and fall to the load! below 1256 @routes_last_modified =mtime1259 @routes_last_modified = mtime 1257 1260 end 1258 1261 load! 1259 1262 end … … 1261 1264 def load_routes! 1262 1265 if defined?(RAILS_ROOT) && defined?(::ActionController::Routing::Routes) && self == ::ActionController::Routing::Routes 1263 1266 load File.join("#{RAILS_ROOT}/config/routes.rb") 1264 @routes_last_modified =File.stat("#{RAILS_ROOT}/config/routes.rb").mtime1267 @routes_last_modified = File.stat("#{RAILS_ROOT}/config/routes.rb").mtime 1265 1268 else 1266 1269 add_route ":controller/:action/:id" 1267 1270 end … … 1452 1455 end 1453 1456 1454 1457 Routes = RouteSet.new 1458 1459 ::Inflector.module_eval do 1460 def inflections_with_route_reloading(&block) 1461 returning(inflections_without_route_reloading(&block)) { 1462 ActionController::Routing::Routes.reload! if block_given? 1463 } 1464 end 1465 1466 alias_method_chain :inflections, :route_reloading 1467 end 1455 1468 end 1456 1469 end