Module: CommonViewHelpers::ViewHelpers

Defined in:
lib/common_view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#base_urlObject



87
88
89
# File 'lib/common_view_helpers.rb', line 87

def base_url
  "#{request.protocol}#{request.host_with_port}"
end

#commify(num) ⇒ Object

Breaks up a BigNum, FixNum or Float with commas e.g., 532566 => 532,566 and 79593255.66 => 79,593,255.66



119
120
121
122
123
124
# File 'lib/common_view_helpers.rb', line 119

def commify(num)
  num.to_s =~ /([^\.]*)(\..*)?/
    int, dec = $1.reverse, $2 ? $2 : ""
    while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2,\3'); end
    int.reverse + dec
end

#convert_to_list_items(items, *args) ⇒ Object

Give this helper an array, and get back a string of <li> elements. The first item gets a class of first and the last, well.. last. This makes it easier to apply CSS styles to lists, be they ordered or unordered. zeke.tumblr.com/post/98025647/a-nice-little-view-helper-for-generating-list-items



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/common_view_helpers.rb', line 27

def convert_to_list_items(items, *args)
  default_options = {:stripe => true}
  options = default_options.merge(args.extract_options!)
  out = []
  items.each_with_index do |item, index|
    css = []
    css << "first" if items.first == item
    css << "last" if items.last == item
    css << "even" if options[:stripe] && index%2 == 1
    css << "odd" if options[:stripe] && index%2 == 0 # (First item is odd (1))
    out << (:li, item, :class => css.join(" "))
  end
  out.join("\n")
end

#generate_table(collection, headers = nil, options = {}) ⇒ Object

Build an HTML table For collection, pass an array of arrays For headers, pass an array of label strings for the top of the table All other options will be passed along to the table content_tag



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/common_view_helpers.rb', line 46

def generate_table(collection, headers=nil, options={})
  return if collection.blank?
  thead = (:thead) do
    (:tr) do
      headers.map {|header| (:th, header)}
    end
  end unless headers.nil?
  tbody = (:tbody) do
    collection.map do |values|
      (:tr) do
        values.map {|value| (:td, value)}
      end
    end
  end
  (:table, [thead, tbody].compact.join("\n"), options)
end

#image_url(source) ⇒ Object

Absolute path to an image



82
83
84
# File 'lib/common_view_helpers.rb', line 82

def image_url(source)
  base_url + image_path(source)
end

#info_pair(label, value) ⇒ Object

Output an easily styleable key-value pair



17
18
19
20
21
# File 'lib/common_view_helpers.rb', line 17

def info_pair(label, value)
  value = (:span, "None", :class => "blank") if value.blank?
  label = (:span, "#{label}:", :class => "label")
  (:span, [label, value].join(" "), :class => "info_pair")
end

#js_string(string) ⇒ Object

Outputs a JS friendly (escaped) string



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/common_view_helpers.rb', line 98

def js_string(string)
  js_escape_map = {
          '\\'    => '\\\\',
          '</'    => '<\/',
          "\r\n"  => '\n',
          "\n"    => '\n',
          "\r"    => '\n',
          '"'     => '\\"',
          "'"     => "\\'" }

  string.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { js_escape_map[$1] }
end

This works just like link_to, but with one difference.. If the link is to the current page, a class of ‘active’ is added



74
75
76
77
78
79
# File 'lib/common_view_helpers.rb', line 74

def link(name, options={}, html_options={})
  link_to_unless_current(name, options, html_options) do
    html_options[:class] = (html_options[:class] || "").split(" ").push("active").join(" ")
    link_to(name, options, html_options)
  end
end

#list_model_columns(obj) ⇒ Object

Generate a list of column name-value pairs for an AR object



92
93
94
95
# File 'lib/common_view_helpers.rb', line 92

def list_model_columns(obj)
  items = obj.class.columns.map{ |col| info_pair(col.name, obj[col.name]) }
  (:ul, convert_to_list_items(items), :class => "model_columns")
end

#options_td(record_or_name_or_array, hide_destroy = false) ⇒ Object

Pass in an ActiveRecord object, get back edit and delete links inside a TD tag



64
65
66
67
68
69
70
# File 'lib/common_view_helpers.rb', line 64

def options_td(record_or_name_or_array, hide_destroy = false)
  items = []
  items << link_to('Edit', edit_polymorphic_path(record_or_name_or_array))
  items << link_to('Delete', polymorphic_path(record_or_name_or_array), :confirm => 'Are you sure?', :method => :delete, :class => "destructive") unless hide_destroy
  list = (:ul, convert_to_list_items(items))
  (:td, list, :class => "options")
end

#time_ago_in_words_or_date(date, options = {}) ⇒ Object

Use words if within the last week, otherwise use date (show year if not this year)



5
6
7
8
9
10
11
12
13
14
# File 'lib/common_view_helpers.rb', line 5

def time_ago_in_words_or_date(date,options={})
  return unless date
  if (Time.now-date)/60/60/24 < 7
    time_ago_in_words(date) + " ago"
  elsif date.year == Time.now.year
    options[:short_format] ? date.strftime(options[:short_format]) : date.strftime("%b %e").gsub("  ", " ")
  else
    options[:long_format] ? date.strftime(options[:long_format]) : date.strftime("%b %e, %Y").gsub("  ", " ")
  end
end

#urlify(addr) ⇒ Object

Useful alongside link() or when linking to user submitted addresses



113
114
115
# File 'lib/common_view_helpers.rb', line 113

def urlify(addr)
  (addr.blank? || addr.starts_with?('http')) ? addr : "http://#{addr}"
end