Module: WordnikRubyHelpers::ViewHelpers
- Defined in:
- lib/wordnik_ruby_helpers.rb
Instance Method Summary (collapse)
-
- (Object) base_url
e.g.
-
- (Object) convert_to_list_items(items, *args)
Give this helper an array, and get back a string of elements.
-
- (Object) enable_typekit(kit_id)
Adds the javascript embed code for Typekit.
-
- (Object) errors_for(form)
Rails 3 did away with 'error_messages', so here's a custom redo..
-
- (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.
- - (Object) html_tag(element, content, properties = {})
-
- (Object) image_url(source)
Absolute path to a local image.
-
- (Object) info_pair(label, value)
Output an easily styleable key-value pair.
-
- (Object) link(name, options = {}, html_options = {})
This works just like link_to, but with one difference..
-
- (Object) list_model_columns(obj)
Generate a list of column name-value pairs for an AR object.
-
- (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.
-
- (Object) timeago(time, *args)
(also: #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..
-
- (Object) value_or_default(value, default = "not specified")
Return a default message if value is blank.
Instance Method Details
- (Object) base_url
e.g. localhost:3000, or productionserver.com
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) = {:stripe => true} = .merge(args.) out = [] items.each_with_index do |item, index| css = [] css << "first" if items.first == item css << "last" if items.last == item css << "even" if [:stripe] && index%2 == 1 css << "odd" if [:stripe] && index%2 == 0 # (First item is odd (1)) out << content_tag(: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 << content_tag(:h2, "#{pluralize(form.object.errors.count, "error")} prohibited this record from being saved.") out << content_tag(:ul, convert_to_list_items(form.object.errors.)) content_tag(: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, ={}) return if collection.blank? thead = content_tag(:thead) do content_tag(:tr) do headers.map {|header| content_tag(:th, header)} end end unless headers.nil? tbody = content_tag(:tbody) do collection.map do |values| content_tag(:tr) do values.map {|value| content_tag(:td, value)} end end end content_tag(:table, [thead, tbody].compact.join("\n"), ) 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? content_tag(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 = content_tag(:span, "None", :class => "blank") if value.blank? label = content_tag(:span, "#{label}:", :class => "label") content_tag(:span, [label, value].join(" ").html_safe, :class => "info_pair") end |
- (Object) link(name, options = {}, html_options = {})
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, ={}, ={}) link_to_unless_current(name, , ) do [:class] = ([:class] || "").split(" ").push("active").join(" ") link_to(name, , ) 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]) } content_tag(: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 (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 = content_tag(:ul, convert_to_list_items(items)) content_tag(: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 => "" } = defaults.merge(args.) # Add a 'relative' CSS class unless it's way old if Time.now-time < [:relative_for] [:class] = [:class].to_s.squeeze(' ').split(' ').push('relative').join(' ') end # Take `relative_for` option out so it doesn't become an HTML attribute.. .delete(:relative_for) html_tag(:abbr, time.strftime('%b %e, %Y'), ) 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? ? content_tag(:span, default, :class => "blank") : value end |