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
        rewritten_url << (options[:protocol] || @request.protocol) unless options[:only_path]
        rewritten_url << (options[:host] || @request.host_with_port) unless options[:only_path]
D
Initial  
David Heinemeier Hansson 已提交
25

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

31
        rewritten_url
D
Initial  
David Heinemeier Hansson 已提交
32 33
      end

34 35
      def rewrite_path(options)
        options = options.symbolize_keys
36
        options.update((options[:params] || {}).symbolize_keys)
37 38
        RESERVED_OPTIONS.each {|k| options.delete k}
        path, extras = Routing::Routes.generate(options, @request)
39 40

        if extras[:overwrite_params]
41
          params_copy = @request.parameters.reject { |k,v| %w(controller action).include? k }
42 43 44 45 46
          params_copy.update extras[:overwrite_params]
          extras.delete(:overwrite_params)
          extras.update(params_copy)
        end

47
        path <<  build_query_string(extras) unless extras.empty?
48
        
49
        path
D
Initial  
David Heinemeier Hansson 已提交
50 51 52 53 54 55 56
      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.
      def build_query_string(hash)
        elements = []
        query_string = ""
57
        
58
        hash.each do |key, value|
59 60
          key = CGI.escape key.to_s
          key <<  '[]' if value.class == Array
D
David Heinemeier Hansson 已提交
61
          value = [ value ] unless value.class == Array
62
          value.each { |val| elements << "#{key}=#{Routing.extract_parameter_value(val)}" }
63
        end
D
Initial  
David Heinemeier Hansson 已提交
64
        
65
        query_string << ("?" + elements.join("&")) unless elements.empty?
66
        query_string
D
Initial  
David Heinemeier Hansson 已提交
67 68
      end
  end
69
end