Custom scopes for paginated lists

openid
Marcin Kulik 12 years ago
parent b4095bd372
commit 9ac2c49180

@ -9,27 +9,21 @@ class AsciicastsController < ApplicationController
respond_to :html, :json
def index
collection = Asciicast.
newest.
includes(:user).
page(params[:page]).
per(PER_PAGE)
@asciicasts = AsciicastDecorator.decorate(
Asciicast.newest_paginated(params[:page], PER_PAGE)
)
@category_name = "All Asciicasts"
@current_category = :all
@asciicasts = AsciicastDecorator.decorate(collection)
end
def popular
collection = Asciicast.
popular.
includes(:user).
page(params[:page]).
per(PER_PAGE)
@asciicasts = AsciicastDecorator.decorate(
Asciicast.popular_paginated(params[:page], PER_PAGE)
)
@category_name = "Popular Asciicasts"
@current_category = :popular
@asciicasts = AsciicastDecorator.decorate(collection)
render :index
end

@ -15,6 +15,14 @@ class Asciicast < ActiveRecord::Base
scope :popular, where("views_count > 0").order("views_count DESC")
scope :newest, order("created_at DESC")
scope(:newest_paginated, lambda do |page, per_page|
newest.includes(:user).page(page).per(per_page)
end)
scope(:popular_paginated, lambda do |page, per_page|
popular.includes(:user).page(page).per(per_page)
end)
before_create :assign_user, :unless => :user
attr_accessible :meta, :stdout, :stdout_timing, :stdin, :stdin_timing,

Loading…
Cancel
Save