Module: BasicHelpers

Defined in:
lib/basic_helpers.rb

Instance Method Summary collapse

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, options = {}, &block)
  return nil if items.blank?

  items = items.map(&block) if block_given?

  (:dl, items.inject('') {|html,item| (:dt, item.first) + (:dd, item.last) }, options)
end

#ol(items, options = {}, &block) ⇒ Object



145
146
147
# File 'lib/basic_helpers.rb', line 145

def ol(items, options = {}, &block)
  ul(items, options.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, options = {})
  return nil if source.blank? 

  html = ''

  if headers = options.delete(:headers)
    html << (:thead,
      (:tr,
        (headers == true ? source.delete_at(0) : headers).map {|col| (:th, col) }
      )
    )
  end

  source.inject(html) do |buffer,row|
    buffer << (:tr, row.map {|col| (:td, *(Array(col))) })
  end

  (:table, html, options)
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, options = {})
  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

  options[:headers] = attrs.map {|att| att.to_s.humanize } if options[:headers] == true

  table(rows, options)
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, options = {}, &block)
  return nil if items.blank?

  tag = options.delete(:tag) || :ul

  contents = if block_given?
    items.inject([]) do |html,item|
      html << (:li, *yield(item))
    end.join("\n")
  else
    "\n<li>#{items.join("</li>\n<li>")}</li>\n"
  end

  (tag, contents, options)
end