Skip to content

Instantly share code, notes, and snippets.

@elucid
Created February 21, 2011 03:35
Show Gist options
  • Save elucid/836628 to your computer and use it in GitHub Desktop.
Save elucid/836628 to your computer and use it in GitHub Desktop.
port Merb's :identify routing option to Rails 3 router
# see actionpack/lib/action_dispatch/routing/route_set.rb
module ActionDispatch
module Routing
class RouteSet
class Generator
# modified to allow :identify option ala Merb routing
def opts # :nodoc:
parameterize = lambda do |name, value|
to_param = lambda do |val|
if options[:identify] and name == :id and not val.is_a?(String)
val.send options[:identify]
else
val.to_param
end
end
if name == :controller
value
elsif value.is_a?(Array)
value.map { |v| Rack::Mount::Utils.escape_uri(to_param[v]) }.join('/')
else
return nil unless param = to_param[value]
param.split('/').map { |v| Rack::Mount::Utils.escape_uri(v) }.join("/")
end
end
{:parameterize => parameterize}
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment