Class: AwesomeTables::AwesomeTable

Inherits:
Object
  • Object
show all
Defined in:
lib/awesome_tables/awesome_table.rb

Constant Summary collapse

@@tables =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_type, data = [], options = {}) ⇒ AwesomeTable

Returns a new instance of AwesomeTable.



12
13
14
15
16
17
18
19
20
# File 'lib/awesome_tables/awesome_table.rb', line 12

def initialize(table_type, data = [], options = {})
  @data = data
  @paginate = true if @data.is_a? WillPaginate::Collection
  @columns = []
  @template = options[:template] ||= 'awesome_tables/base'
  # is this the best way to do this?
  raise AwesomeTables::UnknownTableTypeException if @@tables[table_type].blank?
  @@tables[table_type].call(self)
end

Instance Attribute Details

#captionObject

Returns the value of attribute caption.



5
6
7
# File 'lib/awesome_tables/awesome_table.rb', line 5

def caption
  @caption
end

#columnsObject

Returns the value of attribute columns.



5
6
7
# File 'lib/awesome_tables/awesome_table.rb', line 5

def columns
  @columns
end

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/awesome_tables/awesome_table.rb', line 5

def data
  @data
end

#partialObject

Returns the value of attribute partial.



5
6
7
# File 'lib/awesome_tables/awesome_table.rb', line 5

def partial
  @partial
end

Class Method Details

.register(table_type, &block) ⇒ Object



7
8
9
10
# File 'lib/awesome_tables/awesome_table.rb', line 7

def self.register(table_type, &block)
  raise AwesomeTables::NoBlockGivenException unless block_given?
  @@tables[table_type] = block
end

Instance Method Details

#caption?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/awesome_tables/awesome_table.rb', line 22

def caption?
  !@caption.blank?
end

#column(*args) ⇒ Object

example column colls t.column :created_at, :display_created_at t.column :body, :partial => ‘awesome_tables/posts/body’ t.column :comments, :comment_count, :with_image => ‘comments.png’ t.column :full_name



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/awesome_tables/awesome_table.rb', line 38

def column(*args)
  name = args[0].to_s.titleize
  if args[1].nil?
    method = args[0]
  elsif args[1].is_a? Symbol
    method = args[1]
    options = args[2]
  else
    raise AwesomeTables::NoPartialProvidedException unless args[1].keys.include?(:partial)
    method = args[1].delete(:partial)
    options = args[1]
  end
  @columns << { :header => name, :method => method, :options => options ||= {} }
end

#paginate?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/awesome_tables/awesome_table.rb', line 26

def paginate?
  @paginate
end

#set_caption(caption) ⇒ Object



30
31
32
# File 'lib/awesome_tables/awesome_table.rb', line 30

def set_caption(caption)
  @caption = caption
end