Created
March 7, 2009 21:02
-
-
Save wagenet/75440 to your computer and use it in GitHub Desktop.
Path Expander
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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