Skip to content

Instantly share code, notes, and snippets.

@agibralter
Created December 17, 2008 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agibralter/37226 to your computer and use it in GitHub Desktop.
Save agibralter/37226 to your computer and use it in GitHub Desktop.
class Post < ActiveRecord::Base
filter :active, :conditions => { :active => true }
filter :mine, lambda { |user_id| {:conditions => {:user_id => user_id}} }
sort :date_desc, :order => "date DESC"
sort :date_asc, :order => "date ASC"
end
class PostsController < ApplicationController
def index
@posts = Post.paginate_filter_and_sort(:page => params[:page], :per_page => 50, :filters => params[:filter], :sort => params[:sort], :filter_params => {:mine => current_user.id})
end
# vs: (and this doesn't even include multiple filters or sorting)
def index
if params[:filter] && params[:filter] == 'mine'
@posts = Post.mine(current_user.id).paginate(...)
elsif params[:filter] && params[:filter] == 'active'
@posts = Post.active.paginate(...)
else
@posts = Post.paginate(...)
end
end
end
# /posts?page=57&filters=active&sort=date_desc
# /posts?page=57&filters=mine,active&sort=date_desc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment