list_controls

Simple list filtering & sorting for Rails controller.

Plays nicely with SearchLogic.

Example

Controller:

class ProductsController < ApplicationController

  enable_list_controls :default_filters => { 'state' => 'new' } 

  def index
    @products = Product.all :conditions => { :state => @filters.state }
  end

end

# Note: 
#
# Sort param stored in @filters.order, example 'ascend_by_title'
# Easy use with SearchLogic.

View:

<div id="filters">
  <%= form_for :filters, @filters, :html => { :method => :get } do |f| %>

    <%= f.label :state %>
    <%= f.select :state, states_for_filter %>

  <% end %>
</div>

<table>
  <tr>
    <th><%= sort @filters, :by => :id %></th>
    <th><%= sort @filters, :by => :title, :as => t(:product_title) %></th>
    <th><%= sort @filters, :by => :state, :as => t(:state) %></th>
  </tr>

  <% for product in @products %>
    <tr>
      <td><%= product.id %></td>
      <td><%= product.title %></td>
      <td><%= product.state %></td>
    </tr>
  <% end %>    
</table>

Copyright © 2009 Laurynas Butkus, released under the MIT license