# Tabulatr2 - Index Tables made easy [![Gem Version](https://badge.fury.io/rb/tabulatr2.png)](http://badge.fury.io/rb/tabulatr2) [![Code Climate](https://codeclimate.com/github/metaminded/tabulatr2.png)](https://codeclimate.com/github/metaminded/tabulatr2) [![Travis CI](https://api.travis-ci.org/metaminded/tabulatr2.png)](https://travis-ci.org/metaminded/tabulatr2) ## Requirements * Ruby 2.0.0 or higher * Rails 4.0.0 or higher * [Bootstrap from Twitter](http://getbootstrap.com) ## Installation Require tabulatr2 in your Gemfile: ```ruby gem 'tabulatr2' ``` After that run `bundle install`. Also add `//= require tabulatr` to your application js file and `*= require tabulatr` to your CSS asset pipeline. Make sure to add it after including the `bootstrap` assets. In order to get the provided `i18n` language files run `rails g tabulatr:install` ## Example ![example](https://cloud.githubusercontent.com/assets/570608/5580201/661c63c0-9047-11e4-9993-f71a0f1f4c00.png) ## The DSL `Tabulatr` provides an easy to use DSL to define the data to be used in your table. It is defined in `TabulatrData` classes. If you have a `User` model you would create a `UserTabulatrData` class. ```ruby class UserTabulatrData < Tabulatr::Data end ``` Instead of creating this class by hand you can also generate a `TabulatrData` for a given class by running `rails g tabulatr:table User` ### Columns Let's say you want to display each user's `first_name` and `last_name` attribute: ```ruby class UserTabulatrData < Tabulatr::Data column :first_name column :last_name end ``` That's it. It'll work, but let's assume you would like to display the full name in one single column. No worries! `Tabulatr` got you covered: ```ruby class UserTabulatrData < Tabulatr::Data column :full_name, table_column_options: {header: 'The full name'} do |user| link_to "#{user.first_name} #{user.last_name}", user end end ``` As you can see you just need to provide a block to the `column` call and you can format the cell into whatever you want. You can even use all those fancy Rails helpers! Unfortunately, we can't sort and filter this column right now because `Tabulatr` would look for a `full_name` column in your DB table but there is no such thing. Bummer! Enter `sort_sql` and `filter_sql`: #### Sorting / Filtering ```ruby class UserTabulatrData < Tabulatr::Data column :full_name, sort_sql: "users.first_name || '' || users.last_name", filter_sql: "users.first_name || '' || users.last_name" end ``` With these two options you can provide whatever SQL you would like to have executed when filtering or sorting this particular column. If instead you want to disable sorting and filtering at all, you can do that too: ```ruby class UserTabulatrData < Tabulatr::Data column :full_name, table_column_options: {sortable: false, filter: false} end ``` Also you are able to change generated filter form field for this column. Say for example you want to have an `exact` filter instead of a `LIKE` filter: ```ruby class UserTabulatrData < Tabulatr::Data column :first_name, table_column_options: {filter: :exact} end ``` Following is a table with all the standard mappings for filter fields, which means these filters get created if you don't overwrite it: Data type | Generated form field / SQL --------- | ------------------------- integer, float, decimal | String input field, exact match string, text | String input field, LIKE match date, time, datetime, timestamp | Select field, with options like 'last 30 days', 'today', ... boolean | Select field, options are 'Yes', 'No' and 'Both' ### Associations To display associations you would use the `association` method with the association name and the attribute on the association as it's first two arguments: ```ruby class UserTabulatrData < Tabulatr::Data association :citizenship, :name end ``` Associations take the same arguments as the `column` method: ```ruby class UserTabulatrData < Tabulatr::Data association :citizenship, :name do record.citizenship.name.upcase end association :posts, :text, table_column_options: {filter: false, sortable: false} do |user| user.posts.count end end ``` ### Checkbox To provide a checkbox for each row which is needed by batch actions just call `checkbox`: ```ruby class UserTabulatrData < Tabulatr::Data checkbox end ``` ### Actions To provide a column that's not sortable and filterable but renders something nice: ```ruby class UserTabulatrData < Tabulatr::Data action do |r| link_to "edit", edit_product_path(r) end end ``` ### Buttons To render a fancy button group: ```ruby class UserTabulatrData < Tabulatr::Data buttons do |b,r| b.button :eye, product_path(r), class: 'btn-success' b.button :pencil, edit_product_path(r), class: 'btn-warning' b.submenu do |s| s.button :star, star_product_path(r), label: 'Dolle Sache' s.divider s.button :'trash-o', product_path(r), label: 'Löschen', confirmation: 'echt?', class: 'btn-danger', method: :delete end end end ``` ### Accessing controller You may want to show actions and/or buttons depending on a condition, e.g., user's role. You can access controller helpers with `@controller`. Here is how to check, if you are using [Pundit](https://github.com/elabs/pundit) (or any other authorization gem): ```ruby class UserTabulatrData < Tabulatr::Data buttons do |b,r| if @controller.policy(r).show? b.button :eye, product_path(r), class: 'btn-success' end end end ``` ### Search The DSL provides you with a `search` method to define a custom fuzzy search method which is not bound to a specific column. ```ruby class UserTabulatrData < Tabulatr::Data search do |query| "users.first_name LIKE '#{query}' OR users.last_name LIKE '#{query}' OR users.address LIKE '#{query}'" end # This call could also be written as: # search :first_name, :last_name, :address end ``` ### Custom filters You're also able to create custom filters with the `filter` method to create more advanced filters which are independent of the displayed columns. ```ruby class UserTabulatrData < Tabulatr::Data filter :age_range do |relation, value| if value == 'upto_18' relation.where("birthday > ?", Date.today-18.years) elsif value == 'over_18' relation.where("birthday <= ?", Date.today-18.years) end end end ``` This code will look for a partial to render in `tabulatr/filter/_age_range.*`. You can override this path by specifying the `partial` argument of the `filter` method. It will call it's block with the ActiveRecord::Relation and the submitted value after the user submits the filter form. ```erb # tabulatr/filter/_age_range.html.erb