Module: BasicHelpers
- Defined in:
- lib/basic_helpers.rb
Instance Method Summary collapse
- #dl(items, options = {}, &block) ⇒ Object
- #ol(items, options = {}, &block) ⇒ Object
-
#table(source, options = {}) ⇒ Object
Produces a basic HTML table given an enumerable
source. -
#table_for(source, attrs = nil, options = {}) ⇒ Object
Produces a simple table for a given collection of arbitrary objects.
- #ul(items, options = {}, &block) ⇒ Object
Instance Method Details
#dl(items, options = {}, &block) ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/basic_helpers.rb', line 149 def dl(items, = {}, &block) return nil if items.blank? items = items.map(&block) if block_given? content_tag(:dl, items.inject('') {|html,item| content_tag(:dt, item.first) + content_tag(:dd, item.last) }, ) end |
#ol(items, options = {}, &block) ⇒ Object
145 146 147 |
# File 'lib/basic_helpers.rb', line 145 def ol(items, = {}, &block) ul(items, .merge(:tag => :ol), &block) end |
#table(source, options = {}) ⇒ Object
Produces a basic HTML table given an enumerable source.
Examples
rows =
[
%w{John [email protected]},
%w{Doe [email protected]}
]
table(rows)
# <table>
# <tr>
# <td>John</td>
# <td>[email protected]</td>
# </tr>
# <tr>
# <td>Doe</td>
# <td>[email protected]</td>
# </tr>
# </table>
table([%w{Name E-mail}] + rows, :headers => true)
table(rows, :headers => %w{Name E-mail})
# <table>
# <thead>
# <tr>
# <th>Name</th>
# <th>E-mail</th>
# </tr>
# </thead>
# <tr>
# <td>John</td>
# <td>[email protected]</td>
# </tr>
# <tr>
# <td>Doe</td>
# <td>[email protected]</td>
# </tr>
# </table>
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/basic_helpers.rb', line 44 def table(source, = {}) return nil if source.blank? html = '' if headers = .delete(:headers) html << content_tag(:thead, content_tag(:tr, (headers == true ? source.delete_at(0) : headers).map {|col| content_tag(:th, col) } ) ) end source.inject(html) do |buffer,row| buffer << content_tag(:tr, row.map {|col| content_tag(:td, *(Array(col))) }) end content_tag(:table, html, ) end |
#table_for(source, attrs = nil, options = {}) ⇒ Object
Produces a simple table for a given collection of arbitrary objects.
Examples
@people =
[
Person.new('John', '[email protected]'),
Person.new('Doe', '[email protected]')
]
table(:people, %w{name email})
# <table>
# <tr>
# <td>John</td>
# <td>[email protected]</td>
# </tr>
# <tr>
# <td>Doe</td>
# <td>[email protected]</td>
# </tr>
# </table>
table(rows, [:name, :email], :headers => true)
# <table>
# <thead>
# <tr>
# <th>Name</th>
# <th>Email</th>
# </tr>
# </thead>
# <tr>
# <td>John</td>
# <td>[email protected]</td>
# </tr>
# <tr>
# <td>Doe</td>
# <td>[email protected]</td>
# </tr>
# </table>
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/basic_helpers.rb', line 106 def table_for(source, attrs = nil, = {}) source = instance_variable_get("@#{source}") if source.kind_of?(Symbol) if attrs.blank? rows = source.map {|x| [x.to_s] } else rows = source.map do |x| attrs.map do |att| case att when Proc att.call(x) else x.send(att) end end end end [:headers] = attrs.map {|att| att.to_s.humanize } if [:headers] == true table(rows, ) end |
#ul(items, options = {}, &block) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/basic_helpers.rb', line 129 def ul(items, = {}, &block) return nil if items.blank? tag = .delete(:tag) || :ul contents = if block_given? items.inject([]) do |html,item| html << content_tag(:li, *yield(item)) end.join("\n") else "\n<li>#{items.join("</li>\n<li>")}</li>\n" end content_tag(tag, contents, ) end |