Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #9815: inflector-route-reloading.2.diff

File inflector-route-reloading.2.diff, 4.6 kB (added by kampers, 9 months ago)

updated against [7816]

  • actionpack/test/controller/routing_test.rb

    old new  
    20312031  end 
    20322032   
    20332033end 
     2034 
     2035uses_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 
     2093end 
  • actionpack/lib/action_controller/routing.rb

    old new  
    196196  # 
    197197  # == Route globbing 
    198198  # 
    199   # Specifying <tt>*[string]</tt> as part of a rule like 
     199  # Specifying <tt>*[string]</tt> as part of a rule like
    200200  # 
    201201  #  map.connect '*path' , :controller => 'blog' , :action => 'unrecognized?' 
    202202  # 
    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.   
    204206  # 
    205207  # == Reloading routes 
    206208  # 
     
    208210  # 
    209211  #  ActionController::Routing::Routes.reload 
    210212  # 
    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!+. 
    212215  # 
    213216  # == Testing Routes 
    214217  # 
     
    12481251      alias reload! load! 
    12491252       
    12501253      def reload 
    1251         if @routes_last_modified 
    1252           mtime=File.stat("#{RAILS_ROOT}/config/routes.rb").mtime 
     1254        if @routes_last_modified && defined?(RAILS_ROOT) 
     1255          mtime = File.stat("#{RAILS_ROOT}/config/routes.rb").mtime 
    12531256          # if it hasn't been changed, then just return 
    12541257          return if mtime == @routes_last_modified 
    12551258          # if it has changed then record the new time and fall to the load! below 
    1256           @routes_last_modified=mtime 
     1259          @routes_last_modified = mtime 
    12571260        end 
    12581261        load! 
    12591262      end 
     
    12611264      def load_routes! 
    12621265        if defined?(RAILS_ROOT) && defined?(::ActionController::Routing::Routes) && self == ::ActionController::Routing::Routes 
    12631266          load File.join("#{RAILS_ROOT}/config/routes.rb") 
    1264           @routes_last_modified=File.stat("#{RAILS_ROOT}/config/routes.rb").mtime 
     1267          @routes_last_modified = File.stat("#{RAILS_ROOT}/config/routes.rb").mtime 
    12651268        else 
    12661269          add_route ":controller/:action/:id" 
    12671270        end 
     
    14551458    end 
    14561459 
    14571460    Routes = RouteSet.new 
     1461     
     1462    ::Inflector.module_eval do 
     1463      def inflections_with_route_reloading(&block) 
     1464        returning(inflections_without_route_reloading(&block)) { 
     1465          ActionController::Routing::Routes.reload! if block_given? 
     1466        } 
     1467      end 
     1468 
     1469      alias_method_chain :inflections, :route_reloading 
     1470    end 
    14581471  end 
    14591472end