Class: UsefullTable::TableBuilder
- Inherits:
-
Object
- Object
- UsefullTable::TableBuilder
- 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) int the form :column or “collection.column”
-
:type => :column | :link
-
:label => “wath you want” | column name if not specified
-
:header_type => :sort | :plain | :human | :nil
-
:body_type => :value (column value) | :plain (wathever you write as column name)
-
Constant Summary collapse
- DATE =
[:date, :datetime]
- LINK =
[:show, :edit, :destroy, :download, :link]
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#bool(attribute, *args) ⇒ Object
bool.
-
#col(attribute, *args) ⇒ Object
col Render column value.
-
#initialize(object, search, opt, template, *args) {|_self| ... } ⇒ TableBuilder
constructor
Initialize Builder with the following parameters: :params => Tabel.builder.new.to_param def initialize(object, search, options, template, data = nil, &block).
-
#label(body, *args) ⇒ Object
label Render static label.
-
#monitor(*args) ⇒ Object
monitor Render a tri-state icon to monitor model status * Red : Error * Yellow: Warning * Green: Status ok.
-
#render_body ⇒ Object
Body is pre_rendered because we need the renderengi order is followed.
-
#render_header ⇒ Object
Header is pre_rendered because we need the renderengi order is followed.
-
#status(*args) ⇒ Object
Deprecated.
-
#to_a ⇒ Object
Render table as Array, Columns Name in the first row Bodies follows Note: be sure to call after render_header and render_body.
- #to_param ⇒ Object
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)
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/usefull_table/table_builder.rb', line 31 def initialize(object, search, opt, template, *args, &block) = args. raise MissingBlock if block.nil? && [:params].blank? @object = object @search = search @template = template if [:params] #debugger @data = _decode_hash([:params][:data]).map{|e| e.with_indifferent_access } if [:params][:data].present? opt = _decode_hash([:params][:options]).with_indifferent_access else @data ||= [] end (opt) @excel = [] yield(self) if block #Rendering order is importanto 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
#options ⇒ Object (readonly)
Returns the value of attribute options.
21 22 23 |
# File 'lib/usefull_table/table_builder.rb', line 21 def 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
141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/usefull_table/table_builder.rb', line 141 def bool(attribute, *args) = args. [:method_name] = :bool [:name] = attribute [:type] = :bool [:header_type] ||= [:label].nil? ? :sort : :plain [:body_type] ||= [:url].blank? ? :value : :link [:data_type] = [:reverse] == true ? :Bool_reverse : :Bool [:label] ||= attribute @data << 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
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/usefull_table/table_builder.rb', line 111 def col(attribute, *args) = args. [:method_name] = :col [:name] = attribute [:type] = :column [:header_type] ||= [:label].nil? ? :sort : :plain [:body_type] ||= [:url].blank? ? :value : :link [:label] ||= attribute @data << 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
160 161 162 163 164 165 166 167 168 169 |
# File 'lib/usefull_table/table_builder.rb', line 160 def label(body, *args) = args. [:method_name] = :label [:name] = :label [:type] = :column [:header_type] ||= [:label].nil? ? :nil : :plain [:body_type] = :plain [:body] = body @data << 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)
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/usefull_table/table_builder.rb', line 187 def monitor(*args) = args. [:method_name] = :monitor [:name] = :status_flag [:type] = :column [:header_type] = :nil [:body_type] = :flag [:label] ||= I18n.t(:status_flag, :scope => "activerecord.attributes") @data << if [:monitor][:visible] == true end |
#render_body ⇒ Object
Body is pre_rendered because we need the renderengi order is followed
79 80 81 |
# File 'lib/usefull_table/table_builder.rb', line 79 def render_body @rendered_body end |
#render_header ⇒ Object
Header is pre_rendered because we need the renderengi order is followed
74 75 76 |
# File 'lib/usefull_table/table_builder.rb', line 74 def render_header @rendered_header end |
#status(*args) ⇒ Object
Deprecated
172 173 174 175 |
# File 'lib/usefull_table/table_builder.rb', line 172 def status(*args) Rails::logger.info("TableBuilder#status is deprecated, please use monitor.") monitor(*args) end |
#to_a ⇒ Object
Render table as Array, Columns Name in the first row Bodies follows Note: be sure to call after render_header and render_body
62 63 64 |
# File 'lib/usefull_table/table_builder.rb', line 62 def to_a @excel end |
#to_param ⇒ Object
228 229 230 231 232 233 234 |
# File 'lib/usefull_table/table_builder.rb', line 228 def to_param @params = {} @params[:data] = _encode_hash(_sanitize_data(@data)) @params[:options] = _encode_hash() @params end |