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) 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]
- 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)
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) = 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 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
#options ⇒ Object (readonly)
Returns the value of attribute options.
23 24 25 |
# File 'lib/usefull_table/table_builder.rb', line 23 def @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) = 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
: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) = args. [:method_name] = :col [:name] = attribute [:type] = :column [:header_type] ||= [:label].nil? ? :sort : :plain [:body_type] ||= [:url].blank? ? :value : :link [:label] ||= attribute [:td_html] ||= '' [:wrap] ||= false @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
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/usefull_table/table_builder.rb', line 166 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)
193 194 195 196 197 198 199 200 201 202 |
# File 'lib/usefull_table/table_builder.rb', line 193 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 @options[:monitor][:visible] == true end |
#render_body ⇒ Object
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_header ⇒ Object
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_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
64 65 66 |
# File 'lib/usefull_table/table_builder.rb', line 64 def to_a @excel end |
#to_param ⇒ Object
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 |