Module: ApplicationHelper

Defined in:
app/helpers/application_helper.rb

Overview

The methods added to this helper will be available to all templates in the application.

Instance Method Summary collapse

Instance Method Details

#base_urlObject



50
51
52
53
# File 'app/helpers/application_helper.rb', line 50

def base_url
  home_page_url = url_for :controller => 'admin', :action => 'create_system', :only_path => true
  home_page_url.sub(%r-/create_system/?$-, '')
end

#categories_menuObject

Creates a menu of categories



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/helpers/application_helper.rb', line 56

def categories_menu
  if @categories.empty?
    ''
  else 
    "<div id=\"categories\">\n" +
    '<strong>Categories</strong>:' +
    '[' + link_to_unless_current('Any', :web => @web.address, :action => @action_name) + "]\n" +
    @categories.map { |c| 
      link_to_unless_current(c, :web => @web.address, :action => @action_name, :category => c)
    }.join(', ') + "\n" +
    '</div>'
  end
end

#escape_preserving_linefeeds(text) ⇒ Object

Performs HTML escaping on text, but keeps linefeeds intact (by replacing them with <br/>)



71
72
73
# File 'app/helpers/application_helper.rb', line 71

def escape_preserving_linefeeds(text)
  h(text).gsub(/\n/, '<br/>')
end

#html_options(container, selected = nil) ⇒ Object

Accepts a container (hash, array, enumerable, your type) and returns a string of option tags. Given a container where the elements respond to first and last (such as a two-element array), the “lasts” serve as option values and the “firsts” as option text. Hashes are turned into this form automatically, so the keys become “firsts” and values become lasts. If selected is specified, the matching “last” or element will get the selected option-tag.

Examples (call, result):

html_options([["Dollar", "$"], ["Kroner", "DKK"]])
  <option value="$">Dollar</option>\n<option value="DKK">Kroner</option>

html_options([ "VISA", "Mastercard" ], "Mastercard")
  <option>VISA</option>\n<option selected>Mastercard</option>

html_options({ "Basic" => "$20", "Plus" => "$40" }, "$40")
  <option value="$20">Basic</option>\n<option value="$40" selected>Plus</option>


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/helpers/application_helper.rb', line 18

def html_options(container, selected = nil)
  container = container.to_a if Hash === container

  html_options = container.inject([]) do |options, element| 
    if element.respond_to?(:first) && element.respond_to?(:last)
      if element.last != selected
        options << "<option value=\"#{element.last}\">#{element.first}</option>"
      else
        options << "<option value=\"#{element.last}\" selected>#{element.first}</option>"
      end
    else
      options << ((element != selected) ? "<option>#{element}</option>" : "<option selected>#{element}</option>")
    end
  end
  
  html_options.join("\n")
end

Creates a hyperlink to a Wiki page, without checking if the page exists or not



37
38
39
40
41
42
# File 'app/helpers/application_helper.rb', line 37

def link_to_existing_page(page, text = nil, html_options = {})
  link_to(
      text || page.plain_name, 
      {:web => @web.address, :action => 'show', :id => page.name, :only_path => true},
      html_options)
end

Creates a hyperlink to a Wiki page, or to a “new page” form if the page doesn’t exist yet



45
46
47
48
# File 'app/helpers/application_helper.rb', line 45

def link_to_page(page_name, web = @web, text = nil, options = {})
  raise 'Web not defined' if web.nil?
  web.make_link(page_name, text, options.merge(:base_url => "#{base_url}/#{web.address}"))
end