url_rewriter.rb 2.5 KB
Newer Older
D
Initial  
David Heinemeier Hansson 已提交
1
module ActionController
2
  # Rewrites URLs for Base.redirect_to and Base.url_for in the controller.
3

D
Initial  
David Heinemeier Hansson 已提交
4
  class UrlRewriter #:nodoc:
5
    RESERVED_OPTIONS = [:anchor, :params, :only_path, :host, :protocol, :trailing_slash, :skip_relative_url_root]
6 7
    def initialize(request, parameters)
      @request, @parameters = request, parameters
D
Initial  
David Heinemeier Hansson 已提交
8 9
    end
    
10
    def rewrite(options = {})      
11
      rewrite_url(rewrite_path(options), options)
D
Initial  
David Heinemeier Hansson 已提交
12 13 14
    end

    def to_str
15
      "#{@request.protocol}, #{@request.host_with_port}, #{@request.path}, #{@parameters[:controller]}, #{@parameters[:action]}, #{@request.parameters.inspect}"
D
Initial  
David Heinemeier Hansson 已提交
16 17
    end

18 19
    alias_method :to_s, :to_str

D
Initial  
David Heinemeier Hansson 已提交
20
    private
21
      def rewrite_url(path, options)
D
Initial  
David Heinemeier Hansson 已提交
22
        rewritten_url = ""
23 24 25 26
        unless options[:only_path]
          rewritten_url << (options[:protocol] || @request.protocol)
          rewritten_url << (options[:host] || @request.host_with_port)
        end
D
Initial  
David Heinemeier Hansson 已提交
27

28
        rewritten_url << @request.relative_url_root.to_s unless options[:skip_relative_url_root]
D
Initial  
David Heinemeier Hansson 已提交
29
        rewritten_url << path
30
        rewritten_url << '/' if options[:trailing_slash]
D
Initial  
David Heinemeier Hansson 已提交
31
        rewritten_url << "##{options[:anchor]}" if options[:anchor]
32

33
        rewritten_url
D
Initial  
David Heinemeier Hansson 已提交
34 35
      end

36 37 38
      def rewrite_path(options)
        options = options.symbolize_keys
        options.update(options[:params].symbolize_keys) if options[:params]
39
        if (overwrite = options.delete(:overwrite_params))
40
          options.update(@parameters.symbolize_keys)
41 42
          options.update(overwrite)
        end
43
        RESERVED_OPTIONS.each {|k| options.delete k}
44
        path, extra_keys = Routing::Routes.generate(options.dup, @request) # Warning: Routes will mutate and violate the options hash
45

46
        path << build_query_string(options, extra_keys) unless extra_keys.empty?
47
        
48
        path
D
Initial  
David Heinemeier Hansson 已提交
49 50 51 52
      end

      # Returns a query string with escaped keys and values from the passed hash. If the passed hash contains an "id" it'll
      # be added as a path element instead of a regular parameter pair.
53
      def build_query_string(hash, only_keys = nil)
D
Initial  
David Heinemeier Hansson 已提交
54 55
        elements = []
        query_string = ""
56 57

        only_keys ||= hash.keys
58
        
59 60
        only_keys.each do |key|
          value = hash[key] 
61
          key = CGI.escape key.to_s
62 63 64 65 66
          if value.class == Array
            key <<  '[]'
          else
            value = [ value ]
          end
67
          value.each { |val| elements << "#{key}=#{Routing.extract_parameter_value(val)}" }
68
        end
D
Initial  
David Heinemeier Hansson 已提交
69
        
70
        query_string << ("?" + elements.join("&")) unless elements.empty?
71
        query_string
D
Initial  
David Heinemeier Hansson 已提交
72 73
      end
  end
74
end