Created
December 17, 2008 20:56
-
-
Save agibralter/37226 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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