1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Refactor url methods a bit

Use if..else conditions instead of return guards.
Use _ for not used arguments when iterating.
Set the path variable directly instead of using an empty string and <<.
This commit is contained in:
Carlos Antonio da Silva 2012-11-16 09:35:26 -02:00
parent 8e49fa607f
commit f73a05fb20

View file

@ -8,14 +8,16 @@ module ActionDispatch
class << self class << self
def extract_domain(host, tld_length = @@tld_length) def extract_domain(host, tld_length = @@tld_length)
return nil unless named_host?(host) host.split('.').last(1 + tld_length).join('.') if named_host?(host)
host.split('.').last(1 + tld_length).join('.')
end end
def extract_subdomains(host, tld_length = @@tld_length) def extract_subdomains(host, tld_length = @@tld_length)
return [] unless named_host?(host) if named_host?(host)
parts = host.split('.') parts = host.split('.')
parts[0..-(tld_length + 2)] parts[0..-(tld_length + 2)]
else
[]
end
end end
def extract_subdomain(host, tld_length = @@tld_length) def extract_subdomain(host, tld_length = @@tld_length)
@ -23,15 +25,13 @@ module ActionDispatch
end end
def url_for(options = {}) def url_for(options = {})
path = "" path = options.delete(:script_name).to_s.chomp("/")
path << options.delete(:script_name).to_s.chomp("/")
path << options.delete(:path).to_s path << options.delete(:path).to_s
params = options[:params] || {} params = options[:params] || {}
params.reject! {|k,v| v.to_param.nil? } params.reject! { |_,v| v.to_param.nil? }
result = build_host_url(options) result = build_host_url(options)
result << (options[:trailing_slash] ? path.sub(/\?|\z/) { "/" + $& } : path) result << (options[:trailing_slash] ? path.sub(/\?|\z/) { "/" + $& } : path)
result << "?#{params.to_query}" unless params.empty? result << "?#{params.to_query}" unless params.empty?
result << "##{Journey::Router::Utils.escape_fragment(options[:anchor].to_param.to_s)}" if options[:anchor] result << "##{Journey::Router::Utils.escape_fragment(options[:anchor].to_param.to_s)}" if options[:anchor]