Module: Doozer::ViewHelpers

Included in:
Controller, Mailer, MailerPartial, Partial
Defined in:
lib/doozer/view_helpers.rb

Overview

ViewHelpers which are included in Controllers and Partials

Instance Method Summary collapse

Instance Method Details

Creates an html anchor tag with an absolute url.

text - the text of the anchor tag

opt - a hash of options which are passed to url(opt)

prop - a hash of anchor tag attributes to add to the link



86
87
88
# File 'lib/doozer/view_helpers.rb', line 86

def alink(text='', opt={}, prop={})
  "<a href=\"#{aurl(opt)}\"#{hash_to_props(prop)}>#{text}</a>"
end

#app_nameObject

Returns the app name configured in app.yml



223
224
225
# File 'lib/doozer/view_helpers.rb', line 223

def app_name
  Doozer::Configs.app_name
end

#app_pathObject

Returns the app path the application was loaded in. This defaults to the path all scripts were executed from. In general, this is the root of your project directory unless specified otherwise.



229
230
231
# File 'lib/doozer/view_helpers.rb', line 229

def app_path
  Doozer::Configs.app_path
end

#aurl(opt) ⇒ Object

A wrapper method for #url which automatically sets the base_url to the one configured for the application



63
64
65
66
# File 'lib/doozer/view_helpers.rb', line 63

def aurl(opt)
  opt[:base_url] = base_url if opt.kind_of? Hash
  url(opt)
end

#authtoken(args = {}) ⇒ Object

Creates an authtoken form element

By default, all post requests expect this value to be present unless overrided with Doozer::Controller#after_initialize

You can customize the elemement id by passing arg to the method.

The value contains an checksum of the app_name and cookie sid



174
175
176
177
# File 'lib/doozer/view_helpers.rb', line 174

def authtoken(args={})
  id = args[:id] if args[:id]
  "<input type=\"hidden\" id=\"#{id}_authtoken\" name=\"_authtoken\" value=\"#{generate_authtoken(@request.cookies['sid'])}\" />"
end

#base_urlObject

Returns the base url configured in app.yml



211
212
213
# File 'lib/doozer/view_helpers.rb', line 211

def base_url
  Doozer::Configs.base_url
end

#feed(opt = {}, prop = {}) ⇒ Object

Creates a link tag for feeds.

opt - a hash of options which are passed to url(opt)

prop - a hash of link tag attributes.

> Example: :rel=>‘alternate’, :type=>‘application/rss+’, :media=>‘all’



121
122
123
# File 'lib/doozer/view_helpers.rb', line 121

def feed(opt={}, prop={})
  "<link #{hash_to_props(prop)} href=\"#{url(opt)}\" />"
end

#h(s) ⇒ Object

Safe encodes a string by entity encoding all less then and greater then signs



202
203
204
205
206
207
# File 'lib/doozer/view_helpers.rb', line 202

def h(s)
  s.gsub!(/&/,'&amp;')
  s.gsub!(/</,'&lt;')
  s.gsub!(/>/,'&gt;')
  return s
end

#hash_to_props(opt = {}) ⇒ Object

Turns a hash of key/value pairs in to a key1=“value1” key2=“value2” key3=“value3”



180
181
182
183
184
185
186
187
# File 'lib/doozer/view_helpers.rb', line 180

def hash_to_props(opt={})
  props=[]
  opt.each { | key, value | 
    props.push("#{key.to_s}=\"#{value}\"")
  }
  return " #{props.join(" ")}" if props.length > 0
  return ""
end

#hash_to_qs(opt = {}) ⇒ Object

Turns a hash of key/value pairs in querystring like key1=value%201&key2=value2&key3=value3

All values are CGI.escaped for output



192
193
194
195
196
197
198
# File 'lib/doozer/view_helpers.rb', line 192

def hash_to_qs(opt={})
  props=[]
  opt.each { | key, value | 
    props.push("#{key.to_s}=#{CGI::escape(value.to_s)}") 
  }
  props.join("&")
end

#img(path, prop = {}) ⇒ Object

Creates an img tag.

path - the src of the image tag

prop - a hash of image tag attributes



95
96
97
# File 'lib/doozer/view_helpers.rb', line 95

def img(path, prop={})
  "<img src=\"#{static_url(path)}\"#{hash_to_props(prop)} />"
end

#ipObject

Returns the ip address of the server

Automatically accounts for proxied requests and returns HTTP_X_FORWARDED_FOR if present.



236
237
238
239
240
241
242
# File 'lib/doozer/view_helpers.rb', line 236

def ip
  if addr = @env['HTTP_X_FORWARDED_FOR']
    addr.split(',').last.strip
  else
    @env['REMOTE_ADDR']
  end
end

#javascript(path, prop = {}) ⇒ Object

Creates a script tag.

path - the src of the javascript tag

prop - a hash of script tag attributes.

> Defaults to: :type=>‘text/javascript’



132
133
134
135
# File 'lib/doozer/view_helpers.rb', line 132

def javascript(path, prop={})
  prop[:type] = 'text/javascript' if prop[:type].nil?
  "<script #{hash_to_props(prop)} src=\"#{static_url(path)}\"></script>"
end

Creates an html anchor tag.

text - the text of the anchor tag

opt - a hash of options which are passed to url(opt)

prop - a hash of anchor tag attributes to add to the link



75
76
77
# File 'lib/doozer/view_helpers.rb', line 75

def link(text='', opt={}, prop={})
  "<a href=\"#{url(opt)}\"#{hash_to_props(prop)}>#{text}</a>"
end

#metatagsObject

Creates metatags

retuns a differnt metatag for each key/value added to @view hash. See Doozer::Controller#meta for adding examples.



157
158
159
160
161
162
163
164
165
# File 'lib/doozer/view_helpers.rb', line 157

def metatags
  #loop all metatags here...
  out=[]
  @view[:meta].each{ | key, value | 
    out.push("""<meta name=\"#{key.to_s}\" content=\"#{h(value)}\" />
            """)
  }
  out.join("")
end

#pathObject

Returns the request path



252
253
254
# File 'lib/doozer/view_helpers.rb', line 252

def path
    @env['REQUEST_PATH']
end

#rack_envObject

Returns the env setting the application was loaded under (:development, :deployment, or :test)



217
218
219
# File 'lib/doozer/view_helpers.rb', line 217

def rack_env
  Doozer::Configs.rack_env
end

#server_nameObject

Returns the domain name of the request



246
247
248
# File 'lib/doozer/view_helpers.rb', line 246

def server_name
  @env['SERVER_NAME']
end

#session?Boolean

Test if this person has a session with keys in it…

Returns:

  • (Boolean)


257
258
259
# File 'lib/doozer/view_helpers.rb', line 257

def session?
  @session.empty?
end

#static_url(path) ⇒ Object

Creates a url for static files

hahses the file date and appends it to the end of the file for cache busting…



143
144
145
# File 'lib/doozer/view_helpers.rb', line 143

def static_url(path)
  Doozer::Configs.static_url(path)
end

#stylesheet(path, prop = {}) ⇒ Object

Creates a stylesheet link tag.

path - the href of the link tag

prop - a hash of link tag attributes.

> Defaults to :rel=>‘stylesheet’, :type=>‘text/css’, :media=>‘all’



106
107
108
109
110
111
112
# File 'lib/doozer/view_helpers.rb', line 106

def stylesheet(path, prop={})
  #<link rel="stylesheet" type="text/css" media="all" href="/css/style.css" />
  prop[:rel] = 'stylesheet' if prop[:rel].nil?
  prop[:type] = 'text/css' if prop[:type].nil?
  prop[:media] = 'all' if prop[:media].nil?
  "<link #{hash_to_props(prop)} href=\"#{static_url(path)}\" />"
end

#titleObject

Render the page title

retuns page title if it was set in a controller



150
151
152
# File 'lib/doozer/view_helpers.rb', line 150

def title
  return @view[:title] ? @view[:title] : ""
end

#url(opt) ⇒ Object

Returns a url from a hash of options. Expects option keys as symbols.

:name - the route.name to parse additional options with

:base_url - the base url of the request

> route tokens are replace the key/values pairs in the options. if a route token is found in the options it is replaced with a ‘MISSING-key’ string.

> all remaining options are added as key=value query string parameters

You can also pass a opt as a string which just passed it through.



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
# File 'lib/doozer/view_helpers.rb', line 20

def url(opt)
  url = ''
  if opt.kind_of? Hash
    # name (if present) trumps controller/action (if present)
    if opt[:name]
      # TODO: this needs to be only in dev mode
      name = opt.delete(:name)
      route = Doozer::Routing::Routes::get_by_name(name)
      return "MISSING-ROUTE-for-name-#{name}" if route .nil?
      url = "#{route.path}"

      # we need to swap out the tokens here and account for formats on the end of the path
      tokens = route.tokens
      tokens.last.gsub!(Regexp.compile("\.#{route.format.to_s}$"), '') if route.format != :html if not route.tokens.empty?
      tokens.each { |token|
        val = opt[token.to_sym]
        if val
          opt.delete(token.to_sym)
        else  
          val = "MISSING-#{token}" 
        end
        url.gsub!(/:#{token}/, val.to_s)
      }
    end

    # set base_url
    host = ""
    if opt[:base_url]
      host = opt.delete(:base_url)
    end
    # add qs pairs
    if not opt.empty?
      url += "?#{hash_to_qs(opt)}"
    end
    url = "#{host}#{url}"
  elsif opt.kind_of? String
    url = "#{opt}"
  end
  return url
end