Skip to content

Commit

Permalink
ActionDispatch::HTTP::Url#url_for refactor method
Browse files Browse the repository at this point in the history
Separated right side url generation(before query string)
from left side url generation(after query string)
  • Loading branch information
bogdan committed Apr 24, 2012
1 parent afcae34 commit 0129499
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions actionpack/lib/action_dispatch/http/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,6 @@ def extract_subdomain(host, tld_length = @@tld_length)
end

def url_for(options = {})
if options[:host].blank? && options[:only_path].blank?
raise ArgumentError, 'Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true'
end

rewritten_url = ""

unless options[:only_path]
unless options[:protocol] == false
rewritten_url << (options[:protocol] || "http")
rewritten_url << ":" unless rewritten_url.match(%r{:|//})
end
rewritten_url << "//" unless rewritten_url.match("//")
rewritten_url << rewrite_authentication(options)
rewritten_url << host_or_subdomain_and_domain(options)
rewritten_url << ":#{options.delete(:port)}" if options[:port]
end

path = ""
path << options.delete(:script_name).to_s.chomp("/")
Expand All @@ -47,14 +31,36 @@ def url_for(options = {})
params = options[:params] || {}
params.reject! {|k,v| v.to_param.nil? }

rewritten_url << (options[:trailing_slash] ? path.sub(/\?|\z/) { "/" + $& } : path)
rewritten_url << "?#{params.to_query}" unless params.empty?
rewritten_url << "##{Journey::Router::Utils.escape_fragment(options[:anchor].to_param.to_s)}" if options[:anchor]
rewritten_url
result = build_host_url(options)

result << (options[:trailing_slash] ? path.sub(/\?|\z/) { "/" + $& } : path)
result << "?#{params.to_query}" unless params.empty?
result << "##{Journey::Router::Utils.escape_fragment(options[:anchor].to_param.to_s)}" if options[:anchor]
result
end

private

def build_host_url(options)
if options[:host].blank? && options[:only_path].blank?
raise ArgumentError, 'Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true'
end

result = ""

unless options[:only_path]
unless options[:protocol] == false
result << (options[:protocol] || "http")
result << ":" unless result.match(%r{:|//})
end
result << "//" unless result.match("//")
result << rewrite_authentication(options)
result << host_or_subdomain_and_domain(options)
result << ":#{options.delete(:port)}" if options[:port]
end
result
end

def named_host?(host)
host && IP_HOST_REGEXP !~ host
end
Expand Down

4 comments on commit 0129499

@pechorin
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you forgot about empty line at the begin of method -

or it is okay?

@bogdan
Copy link
Contributor Author

@bogdan bogdan commented on 0129499 Apr 25, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think it's something to care about.

@pechorin
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah :) it's okay to ignore ruby style guides in rails core

@josevalim
Copy link
Contributor

@josevalim josevalim commented on 0129499 Apr 25, 2012 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.