Never been to DZone Snippets before?

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 (See related posts)

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.

   1  
   2  module ActionController
   3    class AbstractRequest
   4      attr_reader :recognized_route
   5      
   6      def init_recognized_route_from_path_parameters
   7        recognized_route = path_parameters.delete(:recognized_route) 
   8      end
   9    end
  10  
  11    module Routing
  12      
  13      class RouteSet
  14        def recognize_with_route(request)
  15          result = recognize_without_route(request)
  16          request.init_recognized_route_from_path_parameters
  17          result
  18        end
  19        alias_method_chain :recognize, :route
  20  
  21        def write_recognize_optimized_with_route
  22          tree = segment_tree(routes)
  23          body = generate_code(tree)
  24          instance_eval %{
  25            def recognize_optimized(path, env)
  26              segments = to_plain_segments(path)
  27              index = #{body}
  28              return nil unless index
  29              while index < routes.size
  30                result = routes[index].recognize(path, env) and result[:recognized_route] = routes[index] and return result
  31                index += 1
  32              end
  33              nil
  34            end
  35          }, __FILE__, __LINE__
  36        end
  37        alias_method_chain :write_recognize_optimized, :route
  38  
  39      end
  40    end
  41  end
  42  

You need to create an account or log in to post comments to this site.


Click here to browse all 5276 code snippets

Related Posts