Class: SortingTableFor::TableBuilder
- Inherits:
-
Object
- Object
- SortingTableFor::TableBuilder
- Includes:
- ActionView::Helpers
- Defined in:
- lib/sorting_table_for/table_builder.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#caption(*args, &block) ⇒ Object
Create a tag caption to set a title to the table It can be called with or without a block.
-
#column(*args, &block) ⇒ Object
Create a cell of column, to have more control.
-
#columns(*args, &block) ⇒ Object
Create the content of table with the values of collection (td) around tbody and tr.
-
#footer(*args, &block) ⇒ Object
Create a cell of footer, to have more control.
-
#footers(*args, &block) ⇒ Object
Create a footer around tfoot and tr.
-
#header(*args, &block) ⇒ Object
Create a cell of header, to have more control.
-
#headers(*args, &block) ⇒ Object
Create a header with the name of the columns (th) around thead and tr.
-
#initialize(collection, object_or_array, template, options, params) ⇒ TableBuilder
constructor
A new instance of TableBuilder.
Constructor Details
#initialize(collection, object_or_array, template, options, params) ⇒ TableBuilder
Returns a new instance of TableBuilder.
30 31 32 33 34 35 |
# File 'lib/sorting_table_for/table_builder.rb', line 30 def initialize(collection, object_or_array, template, , params) @collection, @@object_or_array, @@template, @@options, @@params = collection, object_or_array, template, , params I18n.(params, model_name(@collection.first), @@options[:i18n]) @lines = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (protected)
Send method to ActionView
574 575 576 |
# File 'lib/sorting_table_for/table_builder.rb', line 574 def method_missing(name, *args, &block) @@template.send(name, *args, &block) end |
Instance Method Details
#caption(*args, &block) ⇒ Object
Create a tag caption to set a title to the table It can be called with or without a block. The two exemples are equivalent:
# Without block
<% sorting_table_for @users do |table| %>
<%= table.caption 'hello' %>
<% end %>
# With block
<% sorting_table_for @users do |table| %>
<%= table.caption do %>
'hello'
<% end %>
<% end %>
# Output:
<table class='sorting_table_for'>
<caption>hello</caption>
</table>
Quick
When called without a block or a value, caption is set with I18n translation.
# Exemple of i18n_default_scope:
SortingTableFor::TableBuilder.i18n_default_scope = [:controller, :action]
# Ouput:
I18n.t(:table_caption, :scope => [:current_controller, :current_action]) => en.current_controller.current_action.table_caption
Options
-
:position - To set the position of the caption: :top, :bottom, :left, :right (default: :top)
-
:html - Hash options: class, id, …
Values
All the values won’t be interpreted.
556 557 558 559 560 561 562 563 564 |
# File 'lib/sorting_table_for/table_builder.rb', line 556 def (*args, &block) @caption[:option], @caption[:html] = ( args. ) if block_given? @caption[:value] = capture(&block) else @caption[:value] = (args.empty?) ? I18n.t(:table_caption) : args.first; end end |
#column(*args, &block) ⇒ Object
Create a cell of column, to have more control. It can be called with or without a block. The three exemples are equivalent:
# Without block
<% sorting_table_for @users do |table| %>
<%= table.columns do %>
<%= table.column :username %>
<% end %>
<% end %>
# With current object
<% sorting_table_for @users do |table| %>
<%= table.columns do |user| %>
<%= table.column user.username %>
<% end %>
<% end %>
# With a block and current object
<% sorting_table_for @users do |table| %>
<%= table.columns do |user| %>
<%= table.column do %>
<%= user.username %>
<% end %>
<% end %>
<% end %>
Options
-
:html - Hash options: class, id, …
-
:as - Force to render a type (:date, :time, :currency)
-
:format - Set the I18n localization format for :date or :time (:default, :short, …)
-
:action - Set an action
-
:caption - set caption on td
# Exemple:
<% sorting_table_for @users do |table| %>
<%= table.colums do |user| %>
<%= table.column :username, :html => { :class => 'my_class', :id => 'my_id' }
<%= table.column user.username, :action => :show %>
<%= table.column user.created_at, :as => :date, :format => :short %>
<%= table.column :action => :edit %>
<% end %>
<% end %>
Values
the values given to column can be anything. Only the symbols are set with I18n translation. If you give other types (string, image, …) there won’t be interpreted.
With block the value won’t be interpreted.
392 393 394 395 396 397 398 399 400 |
# File 'lib/sorting_table_for/table_builder.rb', line 392 def column(*args, &block) if block_given? block = capture(&block) @lines.last.add_cell(@current_object, args, nil, block) else @lines.last.add_cell(@current_object, args) end nil end |
#columns(*args, &block) ⇒ Object
Create the content of table with the values of collection (td) around tbody and tr. It can be called with or without a block, or with a list of columns. By default it will add the default actions (edit, delete). The method columns return the current object of the collection. These three exemples are equivalent:
# With a list:
<% sorting_table_for @users do |table| %>
<%= table.columns :username, :firstname %>
<% end %>
# With a block:
<% sorting_table_for @users do |table| %>
<%= table.columns do %>
<%= table.column :username %>
<%= table.column :firstname %>
<% end %>
<% end %>
# With a block and current object:
<% sorting_table_for @users do |table| %>
<%= table.columns do |user| %>
<%= table.column user.username %>
<%= table.column user.firstname %>
<% end %>
<% end %>
# Output:
<table class='sorting_table_for'>
<tbody>
<tr>
<td>...</td>
<td>...</td>
<td><a href='/users/1/edit'>Edit<a></td>
<td><a href='/users/1'>Delete</a></td>
</tr>
...
</tbody>
</table>
Quick Columns
When called without a block or a list, the columns are rendered for each column in the model’s database table and the default actions (edit, delete).
<% sorting_table_for @users do |table| %>
<%= table.columns %>
<% end %>
Options
-
:html - Hash options: class, id, …
-
:actions - Set actions to render
-
:only - Columns only to render
-
:except - Columns to not render
-
:caption - set caption on td
# Exemples:
<% sorting_table_for @users do |table| %>
<%= table.columns :username, :actions => [:edit, :delete] %>
<% end %>
<% sorting_table for @users do |table| %>
<%= table.columns :except => [:created_at, :updated_at] %>
<% end %>
I18n
For each action, the name is set with the I18n translation.
# Exemple of i18n_default_scope:
SortingTableFor::TableBuilder.i18n_default_scope = [:controller, :action]
# Ouput:
I18n.t(:edit, :scope => [:current_controller, :current_action]) => en.current_controller.current_action.edit
Actions
The option ‘default_actions’ contains the actions to add by default to the table. The link to actions is set by the collection sent to the method sorting_table_for. The name of action is set with I18n translation.
# Exemple of default_actions:
SortingTableFor::TableBuilder.default_actions = [:edit]
# Ouput:
link_to( I18n.t(:edit, :scope => [:current_controller, :current_action]), [:edit, user]) => /users/1/edit
For action :delete the builder set a confirm message with I18n translation.
# the I18n confirm message:
I18n.t(:confirm_delete, :scope => [:current_controller, :current_action])
Total entries
The first tr of the columns are the number of results in the collection. The line appears if option ‘show_total_entries’ is set to true.
# Exemple of option:
SortingTableFor::TableBuilder.show_total_entries = true
# Exemple:
<% sorting_table_for @users do |table| %>
<%= table.columns :username, :firstname %>
<% end %>
# Ouput:
<table class='sorting_table_for'>
<tbody>
<tr>
<td colspan='2' class='total-entries'>Total Entries: 42</td>
</tr>
<tr>
<td>...</td>
<td>...</td>
</tr>
...
</tbody>
</table>
A colspan is added by default with the number of columns. Total entries are compatible with the plugin ‘will_paginate’. The total entries text if defined by I18n translation.
# I18n translation:
I18n.t(:total_entries, :scope => :sorting_table_for, :value => total_entries)
Values
the values given to columns can be anything. Only the symbols are the values of the collection. If you give other types (string, image, …) there won’t be interpreted.
326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/sorting_table_for/table_builder.rb', line 326 def columns(*args, &block) , = ( args. ) @collection.each do |object| @current_object = object if block_given? @lines << FormatLine.new(args, , ) yield(object) else @lines << FormatLine.new(args, , , object) end end render_tbody end |
#footer(*args, &block) ⇒ Object
Create a cell of footer, to have more control. It can be called with or without a block. The three exemples are equivalent:
# With a block:
<% sorting_table_for @users do |table| %>
<%= table.footers do %>
<%= table.footer :username %>
<%= table.footer :firstname %>
<% end %>
<% end %>
# With a block:
<% sorting_table_for @users do |table| %>
<%= table.footers do %>
<%= table.footer do %>
<%= :username %>
<% end %>
<%= table.footer do %>
<%= :firstname %>
<% end %>
<% end %>
<% end %>
Options
-
:html - Hash options: class, id, …
-
:caption - set caption on td
# Exemples:
<% sorting_table_for @users do |table| %>
<%= table.columns :username, :cation => 5 %>
<% end %>
I18n
Add a value on scope for footer. Only with symbol.
# Exemple of i18n_add_header_action_scope:
SortingTableFor::TableBuilder.i18n_add_footer_action_scope = :footer
# Ouput:
I18n.t(:edit, :scope => [:current_controller, :current_action]) => en.current_controller.current_action.footer.edit
With block the value won’t be interpreted.
506 507 508 509 510 511 512 513 514 |
# File 'lib/sorting_table_for/table_builder.rb', line 506 def (*args, &block) if block_given? block = capture(&block) @footer_line.add_cell(@collection.first, args, nil, block) else @footer_line.add_cell(@collection.first, args) end nil end |
#footers(*args, &block) ⇒ Object
Create a footer around tfoot and tr. It can be called with or without a block. These two exemples are equivalent:
# With a list:
<% sorting_table_for @users do |table| %>
<%= table.footers :username, :firstname %>
<% end %>
# With a block:
<% sorting_table_for @users do |table| %>
<%= table.footers do %>
<%= table.footer :username %>
<%= table.footer :firstname %>
<% end %>
<% end %>
# Output:
<table class='sorting_table_for'>
<tfoot>
<tr>
<td>...</td>
<td>...</td>
</tr>
</tfoot>
</table>
Options
-
:html - Hash options: class, id, …
-
:caption - set caption on td
# Exemples:
<% sorting_table_for @users do |table| %>
<%= table.columns :username, :cation => 5 %>
<% end %>
I18n
Add a value on scope for footer. Only with symbol.
# Exemple of i18n_add_header_action_scope:
SortingTableFor::TableBuilder.i18n_add_footer_action_scope = :footer
# Ouput:
I18n.t(:edit, :scope => [:current_controller, :current_action]) => en.current_controller.current_action.footer.edit
449 450 451 452 453 454 455 456 457 458 |
# File 'lib/sorting_table_for/table_builder.rb', line 449 def (*args, &block) , = ( args. ) if block_given? @footer_line = FormatLine.new(args, , , nil, :tfoot) capture(&block) else @footer_line = FormatLine.new(args, , , @collection.first, :tfoot) if !args.empty? end render_tfoot end |
#header(*args, &block) ⇒ Object
Create a cell of header, to have more control. It can be called with or without a block. The three exemples are equivalent:
# Without block (I18n and sorting)
<% sorting_table_for @users do |table| %>
<%= table.headers do %>
<%= table.header :username %>
<% end %>
<% end %>
# With string (no I18n and no sorting)
<% sorting_table_for @users do |table| %>
<%= table.headers do %>
<%= table.header 'hello_my_header' %>
<% end %>
<% end %>
# With a block and image (no I18n and no sorting)
<% sorting_table_for @users do |table| %>
<%= table.headers do %>
<%= table.header do %>
<%= image_tag('rails.png') %>
<% end %>
<% end %>
<% end %>
Options
-
:sort - true of false to add or not sorting
-
:html - Hash options: class, id, …
-
:caption - set caption on td
Values
the values given to header can be anything. Only the symbols are set with I18n translation. If you give other types (string, image, …) there won’t have sorting and I18n translation.
With block there won’t have sorting and translation.
184 185 186 187 188 189 190 191 192 |
# File 'lib/sorting_table_for/table_builder.rb', line 184 def header(*args, &block) if block_given? block = capture(&block) @header_line.add_cell(@collection.first, args, nil, block) else @header_line.add_cell(@collection.first, args) end nil end |
#headers(*args, &block) ⇒ Object
Create a header with the name of the columns (th) around thead and tr. It can be called with or without a block, or with a list of columns. By default it will add the default actions (edit, delete). Also by default it will add a link to sort to the column. These two exemples are equivalent:
# With a block:
<% sorting_table_for @users do |table| %>
<%= table.headers do %>
<%= table.header :username %>
<%= table.header :firstname %>
<% end %>
<% end %>
# With a list:
<% sorting_table_for @users do |table| %>
<%= table.headers :username, :firstname %>
<% end %>
# Output:
<table class='sorting_table_for'>
<thead>
<tr>
<th class='cur-sort-not'><a href='/my_link?table_sort[username]=asc'>...</a></th>
<th class='cur-sort-not'><a href='/my_link?table_sort[firstname]=asc'>...</a></th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
</table>
Quick Headers
When called without a block or a list, the headers are rendered for each column in the model’s database table and the default actions (edit, delete), and the links to sort.
<% sorting_table_for @users do |table| %>
<%= table.headers %>
<% end %>
Options
-
:sort - true of false to add or not sorting
-
:html - Hash options: class, id, …
-
:caption - set caption on td
# Html option:
<% sorting_table_for @users do |table| do %>
<%= table.headers :username, :firstname, :html => { :class => 'my_class', :id => 'my_id' }
<% end %>
# Sort option:
<% sorting_table_for @users do |table| do %>
<%= table.headers :sort => false %>
<% end %>
I18n
For each column contained in the model’s database table, the name is set with the I18n translation. The translation are scoped by option ‘i18n_default_scope’ defined in your options.
# Exemple of I18n options for header:
SortingTableFor::TableBuilder.i18n_default_scope = [:controller, :action]
# Ouput:
I18n.t(:username, :scope => [:current_controller, :current_action]) => en.current_controller.current_action.username
Actions
The option ‘default_actions’ contained the actions to add by default to the table. The header’s actions are not sortable. The name of action is set with I18n translation. It’s possible to add a value in the scope for header by option ‘i18n_add_header_action_scope’
# Exemple of default_actions:
SortingTableFor::TableBuilder.default_actions = [:edit]
SortingTableFor::TableBuilder.i18n_add_header_action_scope = :header
# Ouput:
I18n.t(:edit, :scope => [:current_controller, :current_action, :header]) => en.current_controller.current_action.header.edit
Sorting
The link for sorting is set if the column is contained in the model’s database table. the link for sorting is set with the current url, the builder adds a param ‘table_sort’.
# Exemple for column username:
current url: /my_link
param sort: /my_link?table_sort[username]=asc
the name of the param is set by option: TableSortingFor::TableBuilder.params_sort_table
Values
the values given to headers could be anything. Only the symbols are set with I18n translation. If you give other types (string, image, …) there won’t have sorting and I18n translation.
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/sorting_table_for/table_builder.rb', line 133 def headers(*args, &block) , = ( args. ) if block_given? @header_line = FormatLine.new(args, , , nil, :thead) capture(&block) else @header_line = FormatLine.new(args, , , @collection.first, :thead) end render_thead end |