Skip to content

Commit

Permalink
Make sure that custom inflections are picked up by map.resources by t…
Browse files Browse the repository at this point in the history
…riggering a routing reload when new inflections are defined. Closes #9815 [mislav, kampers]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7849 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
NZKoz committed Oct 13, 2007
1 parent faff774 commit bd03bf9
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 7 deletions.
2 changes: 2 additions & 0 deletions actionpack/CHANGELOG
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,7 @@
*SVN* *SVN*


* Make sure that custom inflections are picked up by map.resources. #9815 [mislav]

* Changed SanitizeHelper#sanitize to only allow the custom attributes and tags when specified in the call [DHH] * Changed SanitizeHelper#sanitize to only allow the custom attributes and tags when specified in the call [DHH]


* Extracted sanitization methods from TextHelper to SanitizeHelper [DHH] * Extracted sanitization methods from TextHelper to SanitizeHelper [DHH]
Expand Down
27 changes: 20 additions & 7 deletions actionpack/lib/action_controller/routing.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -196,19 +196,22 @@ module ActionController
# #
# == Route globbing # == Route globbing
# #
# Specifying <tt>*[string]</tt> as part of a rule like : # Specifying <tt>*[string]</tt> as part of a rule like:
# #
# map.connect '*path' , :controller => 'blog' , :action => 'unrecognized?' # map.connect '*path' , :controller => 'blog' , :action => 'unrecognized?'
# #
# 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. # 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.
# #
# == Reloading routes # == Reloading routes
# #
# You can reload routes if you feel you must: # You can reload routes if you feel you must:
# #
# ActionController::Routing::Routes.reload # ActionController::Routing::Routes.reload
# #
# This will clear all named routes and reload routes.rb # This will clear all named routes and reload routes.rb if the file has been modified from
# last load. To absolutely force reloading, use +reload!+.
# #
# == Testing Routes # == Testing Routes
# #
Expand Down Expand Up @@ -1248,20 +1251,20 @@ def load!
alias reload! load! alias reload! load!


def reload def reload
if @routes_last_modified if @routes_last_modified && defined?(RAILS_ROOT)
mtime=File.stat("#{RAILS_ROOT}/config/routes.rb").mtime mtime = File.stat("#{RAILS_ROOT}/config/routes.rb").mtime
# if it hasn't been changed, then just return # if it hasn't been changed, then just return
return if mtime == @routes_last_modified return if mtime == @routes_last_modified
# if it has changed then record the new time and fall to the load! below # if it has changed then record the new time and fall to the load! below
@routes_last_modified=mtime @routes_last_modified = mtime
end end
load! load!
end end


def load_routes! def load_routes!
if defined?(RAILS_ROOT) && defined?(::ActionController::Routing::Routes) && self == ::ActionController::Routing::Routes if defined?(RAILS_ROOT) && defined?(::ActionController::Routing::Routes) && self == ::ActionController::Routing::Routes
load File.join("#{RAILS_ROOT}/config/routes.rb") load File.join("#{RAILS_ROOT}/config/routes.rb")
@routes_last_modified=File.stat("#{RAILS_ROOT}/config/routes.rb").mtime @routes_last_modified = File.stat("#{RAILS_ROOT}/config/routes.rb").mtime
else else
add_route ":controller/:action/:id" add_route ":controller/:action/:id"
end end
Expand Down Expand Up @@ -1455,5 +1458,15 @@ def extract_request_environment(request)
end end


Routes = RouteSet.new Routes = RouteSet.new

::Inflector.module_eval do
def inflections_with_route_reloading(&block)
returning(inflections_without_route_reloading(&block)) {
ActionController::Routing::Routes.reload! if block_given?
}
end

alias_method_chain :inflections, :route_reloading
end
end end
end end
60 changes: 60 additions & 0 deletions actionpack/test/controller/routing_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2031,3 +2031,63 @@ def test_routing_helper_module
end end


end end

uses_mocha 'route loading' do
class RouteLoadingTest < Test::Unit::TestCase

def setup
routes.instance_variable_set '@routes_last_modified', nil
silence_warnings { Object.const_set :RAILS_ROOT, '.' }

@stat = stub_everything
end

def teardown
Object.send :remove_const, :RAILS_ROOT
end

def test_load
File.expects(:stat).returns(@stat)
routes.expects(:load).with(regexp_matches(/routes\.rb$/))

routes.reload
end

def test_no_reload_when_not_modified
@stat.expects(:mtime).times(2).returns(1)
File.expects(:stat).times(2).returns(@stat)
routes.expects(:load).with(regexp_matches(/routes\.rb$/)).at_most_once

2.times { routes.reload }
end

def test_reload_when_modified
@stat.expects(:mtime).at_least(2).returns(1, 2)
File.expects(:stat).at_least(2).returns(@stat)
routes.expects(:load).with(regexp_matches(/routes\.rb$/)).times(2)

2.times { routes.reload }
end

def test_bang_forces_reload
@stat.expects(:mtime).at_least(2).returns(1)
File.expects(:stat).at_least(2).returns(@stat)
routes.expects(:load).with(regexp_matches(/routes\.rb$/)).times(2)

2.times { routes.reload! }
end

def test_adding_inflections_forces_reload
Inflector::Inflections.instance.expects(:uncountable).with('equipment')
routes.expects(:reload!)

Inflector.inflections { |inflect| inflect.uncountable('equipment') }
end

private
def routes
ActionController::Routing::Routes
end

end
end

0 comments on commit bd03bf9

Please sign in to comment.