Module: ScaffoldMarkup::Helpers::TableHelper

Includes:
TwitterBootstrapMarkup
Included in:
Builders::PageBuilder
Defined in:
lib/scaffold_markup/helpers/table_helper.rb

Instance Method Summary collapse

Instance Method Details

#table(collection, attributes, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/scaffold_markup/helpers/table_helper.rb', line 6

def table(collection, attributes, options={})
  _self = self

  column_names = collection.is_a?(ActiveRecord::Relation) ? attributes.map { |a| collection.klass.human_attribute_name(a) } : attributes
  column_names.insert(0, '') if options[:actions]

  html = Table.striped.condensed do
    append _self.table_head(column_names)
    append _self.table_body(collection, attributes, options[:actions] || [])
  end.to_s

  if collection.respond_to?(:current_page)
    html << Tag.new(:div, template.paginate(collection, :theme => 'twitter_bootstrap')).pull_right.to_s
  end

  html.html_safe
end

#table_actions(actions, element) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/scaffold_markup/helpers/table_helper.rb', line 52

def table_actions(actions, element)
  _self = self
  #TODO: DropdownButton tiene que soportar no recibir texto y dejar en blanco por default
  DropdownButton.mini('') do
    actions.each do |action|
      case action
        when :show
          #TODO: El append de DropdownButton tiene que soportar bloques como parametro
          append(Link.new(_self.url.resource(element)) do
            append Icon.new('list')
            append '&nbsp;Show'
          end)
        when :edit
          append(Link.new(_self.url.edit_resource(element)) do
            append Icon.new('pencil')
            append '&nbsp;Edit'
          end)
        when :remove
          append(Link.new(_self.url.resource(element), 'data-confirm' => 'Are you sure?', 'data-method' => :delete) do
            append Icon.new('trash')
            append '&nbsp;Remove'
          end)
        else
          append action
      end
    end
  end.html_safe
end

#table_body(collection, attributes, actions = []) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/scaffold_markup/helpers/table_helper.rb', line 36

def table_body(collection, attributes, actions=[])
  _self = self
  Tag.new(:tbody) do
    collection.each do |element|
      append do
        Tag.new(:tr) do
          append Tag.new(:td, _self.table_actions(actions, element), :class => 'actions')
          attributes.each do |attribute|
            append Tag.new(:td, element.send(attribute))
          end
        end
      end
    end
  end.html_safe
end

#table_head(attribute_names) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/scaffold_markup/helpers/table_helper.rb', line 24

def table_head(attribute_names)
  Tag.new(:thead) do
    append do
      Tag.new(:tr) do
        attribute_names.each do |name|
          append Tag.new(:th, name)
        end
      end
    end
  end.html_safe
end