Module: Mack::Routes::Urls

Included in:
Application, Spec::Example::ExampleMethods
Defined in:
lib/mack/routing/urls.rb

Overview

This module is the repository for named_routes. See Mack::Routes::RouteMap for more information.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_method(n_route, route) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/mack/routing/urls.rb', line 81

def self.create_method(n_route, route)
  define_method("#{n_route}_url") do |*options|
    options = *options
    options = options.parse_splat_args if options.is_a?(Array)
    options = {} if options.nil? || !options.is_a?(Hash)
    url_for_pattern(route.path, (route.options.reject{|k,v| k.to_sym == :action || k.to_sym == :controller || k.to_sym == :method}).merge(options))
  end
  
  define_method("#{n_route}_full_url") do |*options|
    options = *options
    options = {} if options.nil? || !options.is_a?(Hash)
    if @request
      options = {:host => @request.host, :scheme => @request.scheme, :port => @request.port}.merge(options)
    end
    self.send("#{n_route}_url", options)
  end
end

Instance Method Details

#redirect_html(original_path, new_path, status) ⇒ Object

Builds a simple HTML page to be rendered when a redirect occurs. Hopefully no one sees the HTML, but in case the browser won’t do the redirect it’s nice to let people know what’s happening.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mack/routing/urls.rb', line 65

def redirect_html(original_path, new_path, status) # :nodoc:
  %{
    <!DOCTYPE HTML PUBLIC 
        "-//IETF//DTD HTML 2.0//EN"> 
    <html>
      <head> 
        <title>#{status} Found</title> 
      </head>
      <body> 
        <h1>Found</h1> 
         <p>The document has moved <a href="#{new_path}">here</a>.</p> 
      </body>
    </html>
  }
end

#url_for_pattern(url, options = {}) ⇒ Object

Takes a url pattern and merges it with the options to hopefully produce a well formed url. Query string parameters will get escaped.

Example:

url_for_pattern("/:controller/:action/:id", {:controller => :blog, :action => :show, :id => 1})
  # => "/blog/show/1
url_for_pattern("/:controller/:action/:id", {:controller => :blog, :action => :show})
  # => "/blog/show/:id"
url_for_pattern("/blog/:id", {:id => 1})
  # => "/blog/1
url_for_pattern("/blog/:id", {:id => 1, :n_id => 2})
  # => "/blog/1?n_id=2


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mack/routing/urls.rb', line 18

def url_for_pattern(url, options = {})
  u = url.dup
  u = "/" if url.blank?
  unused_params = []
  format = nil
  host_options = {:host => options[:host], :port => options[:port], :scheme => options[:scheme]}
  options - [:host, :port, :scheme, :runner_block]
  if host_options[:host]
    hu = host_options[:host].dup
    options.each_pair do |k, v|
      vp = Rack::Utils.escape(v.to_param)
      unless hu.gsub!(":#{k}", vp).nil?
        options - [k.to_sym]
      end
    end
    host_options[:host] = hu
  end
  options.each_pair do |k, v|
    unless k.to_sym == :format
      if u.match(/\*#{k}/)
        vp = [v].flatten.collect {|c| Rack::Utils.escape(c.to_param)}
        if u.gsub!("*#{k}", File.join(vp)).nil?
          unused_params << "#{Rack::Utils.escape(k)}=#{vp}"
        end
      else
        vp = Rack::Utils.escape(v.to_param)
        if u.gsub!(":#{k}", vp).nil?
          unused_params << "#{Rack::Utils.escape(k)}=#{vp}"
        end
      end
    else
      format = v
    end
  end
  if format
    u << ".#{format}"
  end
  unless unused_params.empty?
    u << "?" << unused_params.sort.join("&")
  end
  File.join(build_full_host_from_options(host_options), u)
  # u
end