Module: UsefullTableHelper

Defined in:
app/helpers/usefull_table_helper.rb

Instance Method Summary collapse

Instance Method Details

#export_for(object, params = nil, &block) ⇒ Object

Instantiate builder with data info and render arrays for every line

Usage

If you can use params to clone an existing table (builder.to_param) and return an array of Arrays $ array = export_for(@object, @params)

You can build a new table passing a block (see ::table_for



139
140
141
142
# File 'app/helpers/usefull_table_helper.rb', line 139

def export_for(object, params = nil, &block)
  builder = UsefullTable::TableBuilder.new(object, nil, nil, self,  :params => params, &block)
  builder.to_a
end

#inline_field(object, id, method, value, id_relation, tag_options = {}, in_place_editor_options = {}) ⇒ Object

Draw inline edit field



145
146
147
148
149
150
151
152
153
154
155
# File 'app/helpers/usefull_table_helper.rb', line 145

def inline_field(object, id, method, value, id_relation, tag_options = {}, in_place_editor_options = {})
  Rails::logger.info("table_for#inline_field : oject=#{object.inspect}, method=#{method.inspect}, id=#{id.inspect}")
  tag_options = { :tag => "span",
                        :id => "#{object.name.underscore.gsub("/","_")}_#{method}_#{id}_#{id_relation}_in_place_editor",
                        :class => "in_place_editor_field"}
  id_relation = id if id_relation.nil?
  in_place_editor_options[:url] = url_for({:action => "update", :controller=>"usefull_table/table", :id => id_relation})
  in_place_editor_options[:parameters] = { :class_name => object.name.underscore, :attribute_name => method}
  tag = (tag_options.delete(:tag), h(value),tag_options)
  return tag + in_place_editor(tag_options[:id], in_place_editor_options)
end

#table_for(obj, *args, &block) ⇒ Object

UsefullTable

table_for generate a full-optionals table, with excel export, columns ordering, links, inline edit and monitoring (ActsAsMonitor gem) but don’t warry because of a rich set of defaults, make its use very simple

Setup

Add this line to your application’s Gemfile: gem ‘usefull_table’

then execute

$ bundle install

or install it yourself as: $ sudo gem install usefull_table

copy icons, javascript and stylesheets: $ rails g usefull_table:install

Usage table_for

Write few lines in your controller app/controllers/home_controller.rb def index

@search = Item.search(params[:search])
...
respond_to do |format|
  format.html { @items = @search.paginate(:page => params[:page]) }
end
...

end

and in your view app/views/home/my_view.html.erb <%= table_for @items, @search, options = {} do |t| %>

<% t.show :url => Proc.new { |item| item_path(item)} %>
<% t.edit :url => Proc.new { |item| edit_item_path(item)}%>
<% t.destroy :url => Proc.new { |item| item_path(item)}, :link_options => {:method => delete, :confirm => "are you sure?"} %>
<% t.download :url => Proc.new { |item| download_item_path(item)} %>
<% t.col :name %>
<% t.col "user.name" %>
<% t.status %>

<% end %>

Options

default values in bold

Paginator

options[:visible] = true | false note: false if @items not present options[:class] = *“usefull_table_paginator”*

Container

options = *=> “usefull_table_container”*

Excel

options[:visible] = true | false options[:filter] = true | false note: false if @search not present options[:human] = true | false options[:worksheet] = *object.class.name.gsub(/::/,“#”)* note: class name with namespace separator # options[:url] = custom url

Table

options[:div_html] = *=> “usefull_table”* options[:header_html] = *=> “first_row”* options[:header_type] = :sort note: :human if @search not present (no sorting possible)

:plain    bare column name from ActiveRecord
:human  column name humanized by ActiveRecord
:nil      no column name

Localization

Uses standard ActiveRecord localization to render tables and columns names it:

activerecord:
  attributes:
    item:
     name: Name
     type: Type
    user:
     name: Name
 models:
   item:
     one: Item
     other: Items
   user:
     one: User
     other: Users

#config/usefull_table.it.yml it:

usefull_table:
  submit_excel: Excel
  header_error: Errore
  body_error: Errore

icons:
  show: "usefull_table_show.png"
  edit: "usefull_table_edit.png"
  destroy: "usefull_table_destroy.png"
  download: "usefull_table_download.png"


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/helpers/usefull_table_helper.rb', line 98

def table_for(obj, *args, &block)
  #Rails::logger.info("table_for START args=#{args.inspect}")
  unless obj.blank?
    search = args.shift if args[0].kind_of?(MetaSearch::Builder)
    #Rails::logger.info("table_for START(1) search=#{search.inspect}")
    options = args.extract_options!
    raise UsefullTable::MissingBlock unless block_given?
    
    if obj.kind_of?(MetaSearch::Builder)
      search = obj
      object = obj.relation
      search_attributes = search.search_attributes
    else
      object = obj
    end
               
    builder = UsefullTable::TableBuilder.new(object, search, options, self,  &block)
    options = builder.options
    
    out = ""
    out << monitor_tag_js if options[:monitor][:visible] == true
    out << stylesheet_link_tag('usefull_table.css')
    out << (:div, options[:html]) do
      ext = ''
      ext << usefull_table_paginator_for(object, options[:paginator])
      ext << usefull_table_export_for(object,search,builder, options[:export])
      ext << usefull_table_for(builder, object, search, options[:table])
      ext << usefull_table_paginator_for(object, options[:paginator])
      ext.html_safe
    end
    out.html_safe
  end
end