Skip to content

Instantly share code, notes, and snippets.

@wagenet
Created March 7, 2009 21:02
Show Gist options
  • Save wagenet/75440 to your computer and use it in GitHub Desktop.
Save wagenet/75440 to your computer and use it in GitHub Desktop.
Path Expander
# OBSOLETE: See http://in.finitu.de/2009/04/25/use_route-param-for-rails-url-helper
module PathExpander
def self.included(base)
base.send :include, InstanceMethods
base.helper_method :expand_url, :expand_path
end
module InstanceMethods
def expand_url(path, options = {})
path, raw_params_str = path.split("?")
raw_params = (raw_params_str || "").split('&').map{|var| var.split('=') }
params = raw_params.each_with_object({}) {|(key, value), hsh| hsh[key] = value }
# TODO: This recognize path may be a bit inefficient
params.merge(ActionController::Routing::Routes.recognize_path(path))
url_for(params.merge(options))
end
def expand_path(path, options = {})
expand_url(path, options.merge(:only_path => true))
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment