Rails plugin for sorting and filter index results
Ruby
Switch branches/tags
Nothing to show
Latest commit 75ea971 Feb 3, 2010 @eLafo README updated
Permalink
Failed to load latest commit information.
index-filter
lib
MIT-LICENSE
README.rdoc
init.rb
install.rb
uninstall.rb

README.rdoc

sortable_index_filters

This plugin gives us two functionalities

-filters for narrowing index scope
-ordenation for index elements

Installation

script/plugin install git://github.com/eLafo/sortable_index_filters.git

Instructions

Filters

It creates two objects:

an object @conditions to be used in index action with our favourite find method which accepts :conditions parameter,
 such as find or paginate -from will paginate-.

an object @filter which is the result of parsing params[:filter]

In order to setup a filter for index action, you should to insert the next line in your controller

has_index_filters :method => :option

You can declare as many method as you wish Available options are:

:like             - it creates a LIKE condition
:equal            - it creates a = condition
:less             - it creates a < condition
:less_or_equal    - it creates a <= condition
:greater          - it creates a > condition
:greater_or_equal - it creates a >= condition

For example, the next line

has_index_filters :code => :like, :client_id => :equal

finally will create next conditions:

code LIKE '%CODE_PARAMETER%' AND client_id = CLIENT_ID_PARAMETER

In the index view, you should insert a form for submiting filter parameters. The form's fields should be named filter for this plugin to work.

For the previous example, the fields would be

- filter[code]
- filter[client_id]

and the view could be like this:

<%=  form_tag foos_url, :method => :get %>
      <%= text_field_tag 'filter[code]', (@filter[:code] rescue '')   %>
      <%= text_field_tag 'filter[client_id], (@filter[:client_id] rescue '') %>
  <%= submit_tag 'filter' %>
</form>

Ordenation

If you want your index elements to be sortable by fields, you must include this line in your controller

has_sortable_fields

Controller will expect two params:

index_order_field: The field which index results are sorted by
index_order_direction: The sorting direction -ASC or DESC

And will generate an instance variable @index-order which can be used with find, will paginate, etc methods like this:

@foos = Foo..paginate(
  :page => params[:page],
  :per_page => PAGINATION_PER_PAGE,
  :conditions => @conditions, #this variable is the one generated by has_index_filters method
  :order => @index_order #this is the variable generated by has_sortable_fields
)

You can use the helper

method sortable_field(name, field, options = {}, html_options = {})

It creates a link to index url with index_order_field and index_order_direction params

name: name for the link
field: field for index elements to be sorted by

Example

<%= sortable_field('code', :code) %></th>

Notes

This plugin is based on my mentor Daniel Mata's code http://danimataonrails.blogspot.com

Copyright

(C) 2010 Javier Lafora Rey

License

This code is available under the MIT license.