Class: Tabletastic::TableBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/tabletastic/table_builder.rb

Constant Summary collapse

@@default_hidden_columns =
%w[created_at updated_at created_on updated_on lock_version version]
@@destroy_confirm_message =
"Are you sure?"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, klass, template) ⇒ TableBuilder

Returns a new instance of TableBuilder.



10
11
12
13
# File 'lib/tabletastic/table_builder.rb', line 10

def initialize(collection, klass, template)
  @collection, @klass, @template = collection, klass, template
  @table_fields = []
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



8
9
10
# File 'lib/tabletastic/table_builder.rb', line 8

def collection
  @collection
end

#klassObject (readonly)

Returns the value of attribute klass.



8
9
10
# File 'lib/tabletastic/table_builder.rb', line 8

def klass
  @klass
end

#table_fieldsObject (readonly)

Returns the value of attribute table_fields.



8
9
10
# File 'lib/tabletastic/table_builder.rb', line 8

def table_fields
  @table_fields
end

Instance Method Details

#action_cells(actions, prefix = nil) ⇒ Object

Used internally to build up cells for common CRUD actions



95
96
97
98
99
100
101
102
# File 'lib/tabletastic/table_builder.rb', line 95

def action_cells(actions, prefix = nil)
  return if actions.blank?
  actions = [actions] if !actions.respond_to?(:each)
  actions = [:show, :edit, :destroy] if actions == [:all]
  actions.each do |action|
    action_link(action.to_sym, prefix)
  end
end

Dynamically builds links for the action



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/tabletastic/table_builder.rb', line 105

def action_link(action, prefix)
  html_class = "actions #{action.to_s}_link"
  block = lambda do |resource|
    compound_resource = [prefix, resource].compact
    compound_resource.flatten! if prefix.kind_of?(Array)
    case action
    when :show
      @template.link_to("Show", compound_resource)
    when :destroy
      @template.link_to("Destroy", compound_resource,
                        :method => :delete, :confirm => @@destroy_confirm_message)
    else # edit, other resource GET actions
      @template.link_to(action.to_s.titleize,
                        @template.polymorphic_path(compound_resource, :action => action))
    end
  end
  self.cell(action, :heading => "", :cell_html => {:class => html_class}, &block)
end

#bodyObject



77
78
79
80
81
82
83
84
85
86
# File 'lib/tabletastic/table_builder.rb', line 77

def body
  (:tbody) do
    @collection.inject("\n") do |rows, record|
      rowclass = @template.cycle("odd","even")
      rows += @template.(:tr, record, :class => rowclass) do
        cells_for_row(record)
      end + "\n"
    end
  end
end

#cell(*args, &proc) ⇒ Object

individually specify a column, which will build up the header, and method or block to call on each resource in the array

Should always be called within the block of data

For example:

t.cell :blah

will simply call blah on each resource

You can also provide a block, which allows for other helpers or custom formatting. Since by default erb will just call to_s on an any element output, you can more greatly control the output:

t.cell(:price) {|resource| number_to_currency(resource)}

would output something like:

<td>$1.50</td>


57
58
59
60
61
62
63
64
65
# File 'lib/tabletastic/table_builder.rb', line 57

def cell(*args, &proc)
  options = args.extract_options!
  options.merge!(:klass => klass)
  args << options
  @table_fields << TableField.new(*args, &proc)
  # Since this will likely be called with <%= %> (aka 'concat'), explicitly return an 
  # empty string; this suppresses unwanted output
  return ""
end

#cells_for_row(record) ⇒ Object



88
89
90
91
92
# File 'lib/tabletastic/table_builder.rb', line 88

def cells_for_row(record)
  @table_fields.inject("") do |cells, field|
    cells + (:td, field.cell_data(record), field.cell_html)
  end
end

#data(*args, &block) ⇒ Object

builds up the fields that the table will include, returns table head and body with all data

Can be used one of three ways:

  • Alone, which will try to detect all content columns on the resource

  • With an array of methods to call on each element in the collection

  • With a block, which assumes you will use cell method to build up the table



25
26
27
28
29
30
31
32
33
34
# File 'lib/tabletastic/table_builder.rb', line 25

def data(*args, &block) # :yields: tablebody
  options = args.extract_options!
  if block_given?
    yield self
  else
    @table_fields = args.empty? ? orm_fields : args.collect {|f| TableField.new(f.to_sym)}
  end
  action_cells(options[:actions], options[:action_prefix])
  ["\n", head, "\n", body, "\n"].join("").html_safe
end

#headObject



67
68
69
70
71
72
73
74
75
# File 'lib/tabletastic/table_builder.rb', line 67

def head
  (:thead) do
    (:tr) do
      @table_fields.inject("") do |result,field|
        result + (:th, field.heading, field.heading_html)
      end
    end
  end
end