Module: CommonViewHelpers::ViewHelpers
- Defined in:
- lib/common_view_helpers.rb
Instance Method Summary collapse
-
#base_url ⇒ Object
e.g.
-
#commify(num) ⇒ Object
Breaks up a BigNum, FixNum or Float with commas e.g., 532566 => 532,566 and 79593255.66 => 79,593,255.66.
-
#convert_to_list_items(items, *args) ⇒ Object
Give this helper an array, and get back a string of <li> elements.
-
#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.
-
#image_url(source) ⇒ Object
Absolute path to an image.
-
#info_pair(label, value) ⇒ Object
Output an easily styleable key-value pair.
-
#js_string(string) ⇒ Object
Outputs a JS friendly (escaped) string.
-
#link(name, options = {}, html_options = {}) ⇒ Object
This works just like link_to, but with one difference..
-
#list_model_columns(obj) ⇒ Object
Generate a list of column name-value pairs for an AR object.
-
#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.
-
#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).
-
#urlify(addr) ⇒ Object
Useful alongside link() or when linking to user submitted addresses.
Instance Method Details
#base_url ⇒ Object
e.g. localhost:3000, or productionserver.com
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) = {: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 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, ={}) 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 |
#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 = content_tag(:span, "None", :class => "blank") if value.blank? label = content_tag(:span, "#{label}:", :class => "label") content_tag(: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 |
#link(name, options = {}, html_options = {}) ⇒ Object
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, ={}, ={}) link_to_unless_current(name, , ) do [:class] = ([:class] || "").split(" ").push("active").join(" ") link_to(name, , ) 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]) } content_tag(: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 (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 |
#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,={}) return unless date if (Time.now-date)/60/60/24 < 7 time_ago_in_words(date) + " ago" elsif date.year == Time.now.year [:short_format] ? date.strftime([:short_format]) : date.strftime("%b %e").gsub(" ", " ") else [:long_format] ? date.strftime([: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 |