Class: UsefullTable::TableBuilder

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

Overview

Builder as the name suggest builds rows and columns

@data

column1, column2 …

where column is an hash with the following options:

  • :nome => column name (ActiveRecord) in the form :column or “collection.column”

  • :type => :column | :link

  • :label => “what you want” | column name if not specified

  • :header_type => :sort | :plain | :human | :nil

  • :body_type => :value (column value) | :plain (whatever you write as column name)

  • :td_html => html <td> tag attributes

Constant Summary collapse

DATE =
[:date, :datetime]
[:show, :edit, :destroy, :download, :link]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, search, opt, template, *args) {|_self| ... } ⇒ TableBuilder

Initialize Builder with the following parameters: :params => Tabel.builder.new.to_param def initialize(object, search, options, template, data = nil, &block)

Yields:

  • (_self)

Yield Parameters:

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/usefull_table/table_builder.rb', line 33

def initialize(object, search, opt, template, *args, &block)
  options = args.extract_options!
  
  raise MissingBlock if block.nil? && options[:params].blank?
  
  @object = object
  @search = search
  @template = template
  
  if options[:params]
    #debugger 
    @data = _decode_hash(options[:params][:data]).map{|e| e.with_indifferent_access } if options[:params][:data].present?
    opt = _decode_hash(options[:params][:options]).with_indifferent_access  
  else
    @data ||= []
  end
  
  _manage_options(opt)
  
  @excel = []
  yield(self) if block
  
  #Rendering order is important so body and header are pre-rendered by initilizer and returned at will.
  @rendered_header = _render_header
  @rendered_body = _render_body
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



23
24
25
# File 'lib/usefull_table/table_builder.rb', line 23

def options
  @options
end

Instance Method Details

#bool(attribute, *args) ⇒ Object

bool

<% t.bool :name %> #render column :name ( t.bool “name” is ok) as a green/red point <% t.bool “user.name” %> #render column name of the user collection in item (item.user.name) as a green/red point

Options

:header_type =>

*:sort*     #Header is MetaSearch#sort_link of columns_name
:human      #Header is plain text humanized with ActiveRecord column name
:nil          #No header

:label =>

"Custom Name"      #plain text without localization in header
:custom_name        #localized text in lazy context (.) in header

:url => “static_path” or Proc #Proc expose the object instance of the current row

:reverse => true #transofrm true => false



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/usefull_table/table_builder.rb', line 147

def bool(attribute, *args)
  options = args.extract_options!
  options[:method_name] = :bool
  options[:name] = attribute
  options[:type] = :bool
  options[:header_type] ||= options[:label].nil? ? :sort : :plain
  options[:body_type] ||= options[:url].blank? ? :value : :link
  options[:data_type] = options[:reverse] == true ? :Bool_reverse : :Bool
  options[:label] ||= attribute
  @data << options
end

#col(attribute, *args) ⇒ Object

col

Render column value

Usage #col

<% t.col :name %> #render column :name ( t.col “name” is ok) <% t.col “user.name” %> #render column name of the user collection in item (item.user.name)

Options

:header_type =>

*:sort*     #Header is MetaSearch#sort_link of columns_name
:human      #Header is plain text humanized with ActiveRecord column name
:nil          #No header

:label =>

"Custom Name"      #plain text without localization in header
:custom_name        #localized text in lazy context (.) in header

:data_type => #default get class name from object to render

:Date | :Time | :DateTime | :Currency

:url => “static_path” or Proc #Proc expose the object instance of the current row

:inline => true (default false) enable inline edit

:if => evaluate a Proc to show colum value or not



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/usefull_table/table_builder.rb', line 115

def col(attribute, *args)
  options = args.extract_options!
  options[:method_name] = :col
  options[:name] = attribute
  options[:type] = :column
  options[:header_type] ||= options[:label].nil? ? :sort : :plain
  options[:body_type] ||= options[:url].blank? ? :value : :link
  options[:label] ||= attribute
  options[:td_html] ||= ''
  options[:wrap] ||= false
  @data << options
end

#label(body, *args) ⇒ Object

label

Render static label

Usage

<% t.label object %> #render object.inspect <% t.label Proc.new {|item| item.name.capitalize} %> #Evaluate proc with item instance of the corresponding row



166
167
168
169
170
171
172
173
174
175
# File 'lib/usefull_table/table_builder.rb', line 166

def label(body, *args)
  options = args.extract_options!
  options[:method_name] = :label
  options[:name] = :label
  options[:type] = :column
  options[:header_type] ||= options[:label].nil? ? :nil : :plain
  options[:body_type] = :plain
  options[:body] = body
  @data << options
end

#monitor(*args) ⇒ Object

monitor

Render a tri-state icon to monitor model status

  • Red : Error

  • Yellow: Warning

  • Green: Status ok

Usage

<% t.monitor %>

Clicking the icon you get the comlete problem description pushed by Ajaxs script (no page reload)



193
194
195
196
197
198
199
200
201
202
# File 'lib/usefull_table/table_builder.rb', line 193

def monitor(*args)
  options = args.extract_options!
  options[:method_name] = :monitor
  options[:name] = :status_flag
  options[:type] = :column
  options[:header_type] = :nil
  options[:body_type] = :flag
  options[:label] ||= I18n.t(:status_flag, :scope => "activerecord.attributes")
  @data << options if @options[:monitor][:visible] == true
end

#render_bodyObject

Body is pre_rendered because we need the renderengi order is followed



81
82
83
# File 'lib/usefull_table/table_builder.rb', line 81

def render_body
  @rendered_body
end

#render_headerObject

Header is pre_rendered because we need the renderengi order is followed



76
77
78
# File 'lib/usefull_table/table_builder.rb', line 76

def render_header
  @rendered_header
end

#status(*args) ⇒ Object

Deprecated



178
179
180
181
# File 'lib/usefull_table/table_builder.rb', line 178

def status(*args)
  Rails::logger.info("TableBuilder#status is deprecated, please use monitor.")
  monitor(*args)
end

#to_aObject

Render table as Array, Columns Name in the first row Bodies follows Note: be sure to call after render_header and render_body



64
65
66
# File 'lib/usefull_table/table_builder.rb', line 64

def to_a
  @excel
end

#to_paramObject



234
235
236
237
238
239
# File 'lib/usefull_table/table_builder.rb', line 234

def to_param
  @params = {}
  @params[:data] = _encode_hash(_sanitize_data(@data))
  @params[:options] = _encode_hash(@options)
  @params
end