Skip to content

Instantly share code, notes, and snippets.

@agibralter
Created April 22, 2009 05:20
Show Gist options
  • Save agibralter/99601 to your computer and use it in GitHub Desktop.
Save agibralter/99601 to your computer and use it in GitHub Desktop.
class Comment < ActiveRecord::Base
belongs_to :post
named_scope :by_quality, {:order => 'quality DESC'}
named_scope :by_created_at, {:order => 'created_at DESC'}
end
class CommentsController < ApplicationController
before_filter :find_post
def index
@comments = case params[:filters]
when 'by_quality' : @post.comments_by_quality(params[:page])
when 'by_created_at' : @post.comments_by_created_at(params[:page])
else
@post.comments_by_quality(params[:page])
end
end
private
def find_post
@post = Post.find(params[:post_id])
end
end
class CommentsHelper
TYPE_MAP = {
:by_quality => "order by quality",
:by_created_at => "order by date"
}
def link_to_filtered(comments, type)
# WHAT WOULD BE A GOOD WAY OF DOING THIS "current_type" method on the comments array?
if comments.current_type == type
TYPE_MAP[type]
else
link_to TYPE_MAP[type], post_comments_path(:filters => type)
end
end
end
# So that in the view I can do:
# link_to_filtered(@comments, :by_quality)
# link_to_filtered(@comments, :by_created_at)
class Post < ActiveRecord::Base
has_many :comments
def comments_by_quality(page)
comments.by_quality.paginate(:page => page, :per_page => 30)
end
def comments_by_created_at(page)
comments.by_created_at.paginate(:page => page, :per_page => 30)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment