Randomize "featured" and "other by user" lists

This commit is contained in:
Marcin Kulik 2013-11-28 23:17:02 +01:00
parent 2962fb8c5f
commit 9b80151511
3 changed files with 5 additions and 4 deletions

View File

@ -27,7 +27,7 @@ class HomeController < ApplicationController
end
def featured_asciicasts
asciicast_repository.latest_featured_limited(3).decorate
asciicast_repository.random_featured_limited(3).decorate
end
def asciicast_repository

View File

@ -58,7 +58,7 @@ class AsciicastDecorator < ApplicationDecorator
def other_by_user
if user
AsciicastDecorator.decorate_collection(
user.asciicasts.where('id <> ?', model.id).limit(3)
user.asciicasts.where('id <> ?', model.id).order('RANDOM()').limit(3)
)
else
[]

View File

@ -19,9 +19,10 @@ class Asciicast < ActiveRecord::Base
scope :featured, -> { where(featured: true) }
scope :by_recency, -> { order("created_at DESC") }
scope :by_random, -> { order("RANDOM()") }
scope :latest_limited, -> (n) { by_recency.limit(n).includes(:user) }
scope :latest_featured_limited, -> (n) {
featured.by_recency.limit(n).includes(:user)
scope :random_featured_limited, -> (n) {
featured.by_random.limit(n).includes(:user)
}
attr_accessible :title, :description, :time_compression