-
Notifications
You must be signed in to change notification settings - Fork 76
Customizing Filters
In this document we will be using 3 models: User, Event and EventUser so we can demonstrate some amazing filtering capabilities of the gem. Here are the table definitions of the models.
Users Table
create_table :users do |t| t.string :first_name t.string :last_name t.date :birthday t.string :sex t.timestamps end
Events Table
create_table :events do |t| t.integer :creator_id t.string :type t.string :name t.string :headline t.datetime :start_time t.datetime :end_time t.timestamps end add_index :events, [:creator_id]
Event Users Table
create_table :event_users do |t| t.integer :event_id t.integer :user_id t.timestamps end add_index :event_users, [:event_id] add_index :event_users, [:user_id] add_index :event_users, [:event_id, :user_id]
Here are the actual model objects with their associations:
User Model
class User < ActiveRecord::Base has_many :events has_many :events_users end
Event Model
class Event < ActiveRecord::Base belongs_to :user, :class_name => 'User', :foreign_key => :creator_id has_many :event_users end
EventUser Model
class EventUser < ActiveRecord::Base belongs_to :event belongs_to :user end
We will also use a samples_controller with an index action. The controller code looks like this:
class SamplesController < ApplicationController def index # All examples assume you are placing the code in here end end
The index.html.erb will simply look like this:
<%= will_filter_tag(@users)%>
In some cases we will use @users in other cases @events or @event_users
Creating a basic filter is just a matter of adding the following lines of code to your controller:
class OrdersController < ApplicationController def index @users = User.filter(:params => params) end end
And adding a filter display tags to your page: (index.html.erb)
<%= will_filter_tag(@users) %>
If you would like to display the will_filter table, you can add the following tag right below the filter tag: (index.html.erb)
<%= will_filter_table_tag(@orders) %>
Note: Since we are not going to be looking at the table tag here, assume it is always present on the page.
This will create the following filter on the page: