url_rewriter.rb 2.2 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]
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)
22
      
D
Initial  
David Heinemeier Hansson 已提交
23
        rewritten_url = ""
24 25
        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 已提交
26

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

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

34 35
      def rewrite_path(options)
        options = options.symbolize_keys
36
        options.update((options[:params] || {}).symbolize_keys)
37 38 39
        RESERVED_OPTIONS.each {|k| options.delete k}
        
        path, extras = Routing::Routes.generate(options, @request)
40
        path = "/#{path.join('/')}".chomp '/'
41
        path = '/' if path.empty?
42 43
        path += build_query_string(extras)
        
D
Initial  
David Heinemeier Hansson 已提交
44 45 46 47 48 49 50 51
        return path
      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 = ""
52
        
53
        hash.each do |key, value|
54
          key = key.to_s
D
David Heinemeier Hansson 已提交
55 56 57
          key = CGI.escape key
          key += '[]' if value.class == Array
          value = [ value ] unless value.class == Array
58
          value.each { |val| elements << "#{key}=#{Routing.extract_parameter_value(val)}" }
59
        end
D
Initial  
David Heinemeier Hansson 已提交
60
        
61
        query_string << ("?" + elements.join("&")) unless elements.empty?
D
Initial  
David Heinemeier Hansson 已提交
62 63 64
        return query_string
      end
  end
65
end