Class: Aerogel::Admin::TableBuilder

Inherits:
Render::BlockHelper
  • Object
show all
Defined in:
lib/aerogel/admin/table_builder.rb

Overview

TableBuilder constructs and displays a table populated with data obtained from passed object.

Example:

table User.all do
  column :full_name
  column :roles
end

Defined Under Namespace

Classes: Column

Constant Summary collapse

DEFAULT_OPTIONS =
{
  class: 'table-striped',
  style: 'standard',
  html_params: {}
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, options = {}, &block) ⇒ TableBuilder

Returns a new instance of TableBuilder.



22
23
24
25
26
27
28
# File 'lib/aerogel/admin/table_builder.rb', line 22

def initialize( object, options = {}, &block )
  super( &block )
  self.object = object
  self.options = DEFAULT_OPTIONS.deep_merge( options )
  self.style = self.options[:style]
  self.columns = []
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



14
15
16
# File 'lib/aerogel/admin/table_builder.rb', line 14

def columns
  @columns
end

#objectObject

Returns the value of attribute object.



14
15
16
# File 'lib/aerogel/admin/table_builder.rb', line 14

def object
  @object
end

#optionsObject

Returns the value of attribute options.



14
15
16
# File 'lib/aerogel/admin/table_builder.rb', line 14

def options
  @options
end

#styleObject

Returns the value of attribute style.



14
15
16
# File 'lib/aerogel/admin/table_builder.rb', line 14

def style
  @style
end

Instance Method Details

#column(*args, &block) ⇒ Object



30
31
32
33
# File 'lib/aerogel/admin/table_builder.rb', line 30

def column( *args, &block )
  self.columns << Column.new( self, *args, &block )
  nil
end

#html_paramsObject

Renders html params for <table …>



45
46
47
48
49
50
51
# File 'lib/aerogel/admin/table_builder.rb', line 45

def html_params
  attrs = @options[:html_params].dup
  attrs.merge!({
    # :action => @options[:action]
  })
  attrs.map{|n, v| v.nil? ? "#{n}" : "#{n}=\"#{v}\""}.join(" ")
end

#template(name) ⇒ Object



35
36
37
# File 'lib/aerogel/admin/table_builder.rb', line 35

def template( name )
  "admin/table_builder/#{style}/#{name}".to_sym
end

#wrap(content) ⇒ Object



39
40
41
# File 'lib/aerogel/admin/table_builder.rb', line 39

def wrap( content )
  erb template("table.html"), locals: { table: self }, layout: false
end