public
Description: An easy way to add links with default filters for HTML tables in your Rails app.
Homepage: http://flaviogranero.com
Clone URL: git://github.com/flaviogranero/filter_table.git
name age message
file .gitignore Thu Feb 26 11:10:36 -0800 2009 first commit [flaviogranero]
file MIT-LICENSE Thu Feb 26 11:10:36 -0800 2009 first commit [flaviogranero]
file README.textile Thu Feb 26 11:10:36 -0800 2009 first commit [flaviogranero]
file Rakefile Sat Feb 28 10:54:15 -0800 2009 fixed problem with filter value with a composed... [flaviogranero]
file TODO.textile Thu Feb 26 11:10:36 -0800 2009 first commit [flaviogranero]
file init.rb Thu Feb 26 11:10:36 -0800 2009 first commit [flaviogranero]
directory lib/ Sat Feb 28 10:54:15 -0800 2009 fixed problem with filter value with a composed... [flaviogranero]
directory rails/ Thu Feb 26 11:10:36 -0800 2009 first commit [flaviogranero]
directory test/ Sat Feb 28 10:54:15 -0800 2009 fixed problem with filter value with a composed... [flaviogranero]
README.textile

Filter Table

An easy way to include links with default filters for HTML tables in your Rails app.

Install

script/plugin install git://github.com/flaviogranero/filter_table.git

Controller


  class CarsController < ApplicationController
    filter_attributes :status

    def index
      @cars = Car.paginate :page => params[:page], :conditions => filter_conditions
    end
  end

The filter_attributes defines a filter_conditions method that gets called in your action.
This method returns the query conditions based in params of request.

View


  <h1>Cars</h1>
  <div>
    <h2>Filters</h2>
    <%= filter_links_for :status, :values => {'Normal' => 0, 'Broken' => 1, 'Running away' => 2} %>
  </div>
  <table>
    <tr>
      <th>Name</th>
      <th>Status</th>
      <th>Category</th>
    </tr>
    <% @cars.each do |car| %>
      <tr>
        <td><%=h car.name %></td>
        <td><%= car.status %></td>
        <td><%= car.category %></td>
      </tr>
    <% end %>
  </table>

The filter_links_for generates a html list containing a link for each filter value. Also, is allowed the param :title to informe a display name for the list, and the list item active has a css class “filter_active”, but you can change this using the :active_class param.
The param :values can receive an array of values or, if you want to create a list based on distinct values existent in database, use the keywork :auto. See the example below, with filter links for category attribute:


  <h1>Cars</h1>
  <div>
    <h2>Filters</h2>
    <%= filter_links_for :status, :values => {'Normal' => 0, 'Broken' => 1, 'Running away' => 2} %>
    <%= filter_links_for :category, :values => :auto, :model => 'Car' %>
  </div>
  <table>
    <tr>
      <th>Name</th>
      <th>Status</th>
      <th>Category</th>
    </tr>
    <% @cars.each do |car| %>
      <tr>
        <td><%=h car.name %></td>
        <td><%= car.status %></td>
        <td><%= car.category %></td>
      </tr>
    <% end %>
  </table>

At this case, a :model param is required and a query will be created to get all the values.

Version 0.1
-————

February 26, 2009

Author
-——

Flávio Granero – http://flaviogranero.com

Copyright © 2009 Flávio Granero, released under the MIT license