Class: Spectabular::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/spectabular/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Table

Returns a new instance of Table.



7
8
9
10
11
# File 'lib/spectabular/table.rb', line 7

def initialize(options={})
  options.each do |key,value|
    self.send("#{key}=",value)
  end
end

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



4
5
6
# File 'lib/spectabular/table.rb', line 4

def collection
  @collection
end

#collection_nameObject

Returns the value of attribute collection_name.



4
5
6
# File 'lib/spectabular/table.rb', line 4

def collection_name
  @collection_name
end

#columnsObject

Returns the value of attribute columns.



4
5
6
# File 'lib/spectabular/table.rb', line 4

def columns
  @columns
end

#contextObject

Returns the value of attribute context.



4
5
6
# File 'lib/spectabular/table.rb', line 4

def context
  @context
end

#default_emptyObject



28
29
30
# File 'lib/spectabular/table.rb', line 28

def default_empty
  @default_empty ||= 'n/a'
end

#skip_sorting_rowObject

Returns the value of attribute skip_sorting_row.



4
5
6
# File 'lib/spectabular/table.rb', line 4

def skip_sorting_row
  @skip_sorting_row
end

Instance Method Details

#cell(column, record) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/spectabular/table.rb', line 48

def cell(column,record)
  if column.respond_to? :to_sym
    record.send column.to_sym
  else
    if column[:helper]
     column[:helper].respond_to?(:call) ? column[:helper].call(record) : context.send(column[:helper],record)
    elsif column[:value] 
      record.send column[:value]
    else
      nil
    end
  end
end

#empty?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/spectabular/table.rb', line 24

def empty?
  collection.blank?
end

#empty_messageObject



32
33
34
# File 'lib/spectabular/table.rb', line 32

def empty_message
  "No #{collection_name} added yet"
end

#headersObject



62
63
64
65
66
67
68
69
70
# File 'lib/spectabular/table.rb', line 62

def headers
  @headers ||= columns.map do |column|
    if column.respond_to? :to_sym
      column.to_s.humanize
    else
      column[:header].is_a?(Symbol) ? column[:header].to_s.humanize : column[:header].to_s
    end
  end
end

#rowsObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/spectabular/table.rb', line 36

def rows
  ordered_hash.tap do |collection_hash|
    collection.each do |record|
      column_hash = ordered_hash
        columns.each_with_index do |column,i|
          column_hash[headers[i].parameterize] = cell(column,record) || default_empty
        end
      collection_hash[record] = column_hash
    end
  end
end