Module: WordnikRubyHelpers::ViewHelpers

Defined in:
lib/wordnik_ruby_helpers.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) base_url



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

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

- (Object) convert_to_list_items(items, *args)

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/wordnik_ruby_helpers.rb', line 61

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
  raw out.join("\n")
end

- (Object) enable_typekit(kit_id)

Adds the javascript embed code for Typekit. Pass in your kit id



133
134
135
136
# File 'lib/wordnik_ruby_helpers.rb', line 133

def enable_typekit(kit_id)
  raw "<script type=\"text/javascript\" src=\"http://use.typekit.com/#{kit_id}.js\"></script>
  <script type=\"text/javascript\">try{Typekit.load();}catch(e){}</script>"
end

- (Object) errors_for(form)

Rails 3 did away with 'error_messages', so here's a custom redo..



139
140
141
142
143
144
145
# File 'lib/wordnik_ruby_helpers.rb', line 139

def errors_for(form)
return unless form.object.errors.present?
out = []
out << (:h2, "#{pluralize(form.object.errors.count, "error")} prohibited this record from being saved.")
out << (:ul, convert_to_list_items(form.object.errors.full_messages))
  (:div, out.join("\n").html_safe, :class => "errorExplanation")
end

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

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/wordnik_ruby_helpers.rb', line 80

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

- (Object) html_tag(element, content, properties = {})



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/wordnik_ruby_helpers.rb', line 6

def html_tag(element, content, properties={})
  
  # Extract class and id
  css = element.to_s.scan(/\.(\w+)/).flatten.sort.join(" ")
  id = element.to_s.scan(/\#(\w+)/).flatten.first
  
  # Clean up the element
  element = element.to_s.split('#').first.split('.').first
  
  properties[:class] = css unless css.blank?
  properties[:id] = id unless id.blank?
  (element, content, properties)
end

- (Object) image_url(source)

Absolute path to a local image



116
117
118
# File 'lib/wordnik_ruby_helpers.rb', line 116

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

- (Object) info_pair(label, value)

Output an easily styleable key-value pair



51
52
53
54
55
# File 'lib/wordnik_ruby_helpers.rb', line 51

def info_pair(label, value)
  value = (:span, "None", :class => "blank") if value.blank?
  label = (:span, "#{label}:", :class => "label")
  (:span, [label, value].join(" ").html_safe, :class => "info_pair")
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



108
109
110
111
112
113
# File 'lib/wordnik_ruby_helpers.rb', line 108

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

- (Object) list_model_columns(obj)

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



126
127
128
129
# File 'lib/wordnik_ruby_helpers.rb', line 126

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

- (Object) options_td(record_or_name_or_array, hide_destroy = false)

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



98
99
100
101
102
103
104
# File 'lib/wordnik_ruby_helpers.rb', line 98

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

- (Object) timeago(time, *args) Also known as: time_ago_in_words_or_date

Use words if within the last week, otherwise use date (show year if not this year) Good for SEO and humans.. see timeago.yarp.com

Usage: timeago(Time.now) timeago(time, :relative_for => 30.days)



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/wordnik_ruby_helpers.rb', line 26

def timeago(time, *args)
  time = Time.parse(time) unless time.class == Time
  
  defaults = {
    :relative_for => 1.week,
    :title => time.getutc.iso8601,
    :class => ""
  }
  options = defaults.merge(args.extract_options!)

  # Add a 'relative' CSS class unless it's way old
  if Time.now-time < options[:relative_for]
    options[:class] = options[:class].to_s.squeeze(' ').split(' ').push('relative').join(' ')
  end

  # Take `relative_for` option out so it doesn't become an HTML attribute.. 
  options.delete(:relative_for)

  html_tag(:abbr, time.strftime('%b %e, %Y'), options)
end

- (Object) value_or_default(value, default = "not specified")

Return a default message if value is blank



148
149
150
# File 'lib/wordnik_ruby_helpers.rb', line 148

def value_or_default(value, default="not specified")
  value.blank? ? (:span, default, :class => "blank") : value
end