lib/usher/util/generate.rb in usher-0.7.3 vs lib/usher/util/generate.rb in usher-0.7.4

- old
+ new

@@ -1,35 +1,25 @@ class Usher module Util class Generators class Generic - attr_accessor :usher def generate(name, params) generate_path_for_base_params(@usher.named_routes[name].find_matching_path(params), params) end def generate_path_for_base_params(path, params) raise UnrecognizedException.new unless path case params when nil, Hash - generate_path_for_base_params_with_hash(path, params) + path.generate_from_hash(params) else - generate_path_for_base_params_with_array(path, Array(params)) + path.generate(*Array(params).slice!(0, path.dynamic_parts.size)) end end - - def generate_path_for_base_params_with_hash(path, params) - path.generate_from_hash(params) - end - - def generate_path_for_base_params_with_array(path, params) - path.generate(*params.slice!(0, path.dynamic_parts.size)) - end - end class URL < Generic class UrlParts < Struct.new(:path, :request) @@ -112,18 +102,15 @@ result = generate_path_for_base_params(path, params) Rack::Utils.uri_escape!(result) params = extra_params if extra_params - unless !generate_extra || params.nil? || params.empty? + if generate_extra && params && !params.empty? if usher.consider_destination_keys? && path.route.destination_keys params.delete_if{|k, v| path.route.destination_keys.include?(k)} end - unless params.empty? - result << '?' unless result[??] - result << generate_extra_params(params) - end + result << '?' << generate_querystring(params) unless params.empty? end result end def generation_module @@ -182,29 +169,12 @@ url = url_parts.url (url[-1] == ?/) ? url[0..-2] : url end - def path_for_routing_lookup(routing_lookup, params = {}) - path = case routing_lookup - when Route::Path - routing_lookup - when Symbol - route = @usher.named_routes[routing_lookup] - raise UnrecognizedException unless route - route.find_matching_path(params || {}) - when Route - routing_lookup.find_matching_path(params) - when nil - params.is_a?(Hash) ? usher.path_for_options(params) : raise - end - end - - - def generate_extra_params(params) + def generate_querystring(params) extra_params_result = '' - params.each do |k,v| case v when Array v.each do |v_part| extra_params_result << '&' unless extra_params_result.empty? @@ -216,11 +186,25 @@ end end extra_params_result end - end + def path_for_routing_lookup(routing_lookup, params = {}) + path = case routing_lookup + when Route::Path + routing_lookup + when Symbol + route = @usher.named_routes[routing_lookup] + raise UnrecognizedException unless route + route.find_matching_path(params || {}) + when Route + routing_lookup.find_matching_path(params) + when nil + params.is_a?(Hash) ? usher.path_for_options(params) : raise + end + end + end end end end