renholm / listing_fu

Lists with a dash of ferret

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ListingFu

ListingFu is a compact DSL for defining and searching in lists of content in your Rails app, it uses ferret as backend to work its fast magic.

Model

We define our different filters we want to use in our app inside the model:

class Person < ActiveRecord::Base
  has_many :memberships
  has_many :groups, :through => :memberships

  has_one :account

  listing_filter :name => :name, :age => :age, :account_name => lambda { account.name }, :groups => lambda { groups.join(' ') }
end

View

In our view we can now use our defined filters to filter the listing to find the content we want:

<% filters(@people) do |filters| %>
  <%= filters.text_filter :name %>
  <%= filters.options_filter :age, [12, 13, 14] %>
<% end %>

This is the actual listing code that renders a table with clickable headers to do the sorting:

<% listing(@people) do |listing| %>
  <%= listing.column :name %>

  <% listing.column :age do |person| %>
    <%= "My age is #{person.age}" %>
  <% end %>

  <% listing.actions do |person| %>
    <td><%= link_to 'Show', person %></td>
    <td><%= link_to 'Edit', edit_person_path(person) %></td>
    <td><%= link_to 'Destroy', person, :confirm => 'Are you sure?', :method => :delete %></td>
  <% end %>
<% end %>

Controller

Then in our controller we just use the listing method with the parameters from the view

@people = Person.listing params, :per_page => 50

Copyright © 2009 Joakim Ekberg, released under the MIT license

About

Lists with a dash of ferret

License:MIT License