DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Rails 2.1.0 Monkey Patch To Add The Recognized Or Matched Rails Route To The Request
This was based on code I found here from Chris Cruft - http://cho.hapgoods.com/wordpress/?p=151
Enhanced to support rails 2.1.0 and also allows alias method chains for recognize from other plugins still to work.
module ActionController
class AbstractRequest
attr_reader :recognized_route
def init_recognized_route_from_path_parameters
recognized_route = path_parameters.delete(:recognized_route)
end
end
module Routing
class RouteSet
def recognize_with_route(request)
result = recognize_without_route(request)
request.init_recognized_route_from_path_parameters
result
end
alias_method_chain :recognize, :route
def write_recognize_optimized_with_route
tree = segment_tree(routes)
body = generate_code(tree)
instance_eval %{
def recognize_optimized(path, env)
segments = to_plain_segments(path)
index = #{body}
return nil unless index
while index < routes.size
result = routes[index].recognize(path, env) and result[:recognized_route] = routes[index] and return result
index += 1
end
nil
end
}, __FILE__, __LINE__
end
alias_method_chain :write_recognize_optimized, :route
end
end
end





