Table Me 0.0.1
Adam Rensel's Code
Widget table helper for Rails
Usage
Controller
A table must first be created in your controller using the table_me
method.
table_me(collection, options)
The collection can be two things, first an ActiveRecord::Relation, which is the result of some sort of active record query ex:
table_me( User.where(subscribed: true) )
Keep in mind that doing User.all
just returns an array of objects, not the a relation.
In order to do the equivalent of the .all
just pass in the ActiveRecord class:
table_me( User )
Possible options available for this method are:
name
- Label for the the table
per_page
- The amount of items per page of the table
View
In your view you can create the table from the one initialized in the controller, the first parameter of table_for is the name set in table_me. By default the class name is used if a name isn't set.
Columns
table_for :user
Now, this will list all columns from the database in your table, which you may not always want to do. You can pass a block of columns to be more specific:
table_for :user do |t|
t.column :id
t.column :email
t.column :created_at
end
This will give you a user table with the columns id, email, and created_at.
What if you want to customize the output of the column? Each column can also take a block of content:
table_for :user do |t|
t.column :id
t.column :email do |c|
"<h1>c.email</h1>"
end
t.column :created_at
end
Now, when a block is used to alter the content of a column, the sorting is lost, since the table can no longer assume what is in the column. You need to set a sort_on
param to tell the column what to sort by. For example:
table_for :user do |t|
t.column :id
t.column :email, sort_on: :email do |c|
"<h1>c.email</h1>"
end
t.column :created_at
end
There is also a color highlighting helper called highlight_cell
. So lets say that you want to have a visual que for if a user is an admin:
table_for :user do |t|
t.column :id
t.column :admin do |c|
highlight_cell c.admin, green: true
end
t.column :created_at
end
This will put a green box around true in the column. But what if you want to change that true to the word 'Admin' and lets put a red box around all the non admins and make them say 'peons':
table_for :user do |t|
t.column :id
t.column :admin do |c|
highlight_cell c.admin, green: [true, 'Admin'], red: [false, 'peon']
end
t.column :created_at
end
Filters
You can add basic filter fields to the table by using the filter method. Right now, only one filter can be applied and the filters are search fields. I would like to eventually add different types for different types of data. I would like to eventually add in the ability for multiple filter types with a single search button, but the basic form is all I need at the moment. Ajax enabled filtering would be freaking great as well.
Filter usage:
table_for :user do |t|
t.filter :email
t.filter :name
t.column :id
t.column :email
t.column :name
end
There is also a light theme, which can be switched by adding a class of 'light' to the table_for:
table_for :user, class: 'light'
This looks a little like this:
Things to add
- I would like some sort of scope sorting built in.
- More advanced filtering other then just search fields. Multiple filters at one time.
- Ajax filters instead of having to reload the page. The url will still need to be modified so state can be preserved if the url is copy and pasted.
- The way queries are handled with the filter search is sloppy, it needs to be refactored.
- Tests need to be refactored pretty badly.