Module: Umlaut::UrlGeneration

Included in:
Helper
Defined in:
app/helpers/umlaut/url_generation.rb

Overview

A Rails view helper module, which over-rides #url_for and some other rails url-generating methods, so that they can be forced to generate absolute URLs if a controller iVar is set to say so.

This is used by our partial HTML api responses, so make sure html snippets have absolute URLs in them.

Instance Method Summary collapse

Instance Method Details

#path_to_image(source) ⇒ Object Also known as: image_path

over-ride path_to_image to generate complete urls with hostname and everything if @generate_url_with_host is set. This makes image_tag generate src with full url with host. See #url_for



46
47
48
49
50
51
52
53
# File 'app/helpers/umlaut/url_generation.rb', line 46

def path_to_image(source)
  path = super(source)
  if @generate_urls_with_host
    protocol =  request.protocol()
    path = protocol + request.host_with_port() + path
  end
  return path
end

#path_to_javascript(source) ⇒ Object



69
70
71
72
73
74
75
# File 'app/helpers/umlaut/url_generation.rb', line 69

def path_to_javascript(source)
  path = super
  if @generate_urls_with_host    
    path = request.protocol() + request.host_with_port() + path
  end
  return path
end

#path_to_stylesheet(source) ⇒ Object

We want stylesheets and javascripts to do the exact same thing, magic of polymorphous super() makes it work:



61
62
63
64
65
66
67
# File 'app/helpers/umlaut/url_generation.rb', line 61

def path_to_stylesheet(source)
  path = super
  if @generate_urls_with_host    
    path = request.protocol() + request.host_with_port() + path
  end
  return path
end

#url_for(argument = {}) ⇒ Object

Over-ride to allow default forcing of urls with hostnames. This is neccesary for our partial_html_sections service to work properly. Just set @generate_url_with_host = true in your controller, and urls will be generated with hostnames for the remainder of that action.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/helpers/umlaut/url_generation.rb', line 15

def url_for(argument = {})    
  if @generate_urls_with_host
    case argument
    when Hash
      # Force only_path = false if not already set
      argument[:only_path] = false if argument[:only_path].nil?
      return super(argument)
    when String
      # We already have a straight string, if it looks relative, 
      # absolutize it. 
      if argument.starts_with?("/")
        return root_url.chomp("/") + argument
      else
        return super(argument)
      end
    when :back
      return super(argument)
    else 
      # polymorphic, we want to force polymorphic_url instead
      # of default polymorphic_path         
      return polymorphic_url(argument)
    end    
  else
    # @generate_urls_with_host not set, just super
    super(argument)
  end    
end