From b68e02212da035e15d9f52edab8ee345b206bf97 Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Sun, 11 Mar 2012 15:29:31 +0100 Subject: [PATCH] Featured asciicasts --- app/controllers/home_controller.rb | 3 ++- app/models/asciicast.rb | 2 ++ .../20120311142204_add_featured_to_asciicast.rb | 7 +++++++ db/schema.rb | 14 ++++++++------ 4 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20120311142204_add_featured_to_asciicast.rb diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 0de180e..1e6efd3 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,6 +1,7 @@ class HomeController < ApplicationController def show @asciicasts = Asciicast.order("created_at DESC").limit(10) - @asciicast = @asciicasts.first + offset = (Asciicast.featured.count * rand).to_i + @asciicast = Asciicast.featured.offset(offset).first || @asciicasts.first end end diff --git a/app/models/asciicast.rb b/app/models/asciicast.rb index 841b839..3621b3d 100644 --- a/app/models/asciicast.rb +++ b/app/models/asciicast.rb @@ -10,6 +10,8 @@ class Asciicast < ActiveRecord::Base belongs_to :user has_many :comments, :order => :created_at, :dependent => :destroy + scope :featured, where(:featured => true) + before_create :assign_user, :unless => :user attr_accessible :meta, :stdout, :stdout_timing, :stdin, :stdin_timing diff --git a/db/migrate/20120311142204_add_featured_to_asciicast.rb b/db/migrate/20120311142204_add_featured_to_asciicast.rb new file mode 100644 index 0000000..0f8ceef --- /dev/null +++ b/db/migrate/20120311142204_add_featured_to_asciicast.rb @@ -0,0 +1,7 @@ +class AddFeaturedToAsciicast < ActiveRecord::Migration + def change + add_column :asciicasts, :featured, :boolean, :default => false + + add_index :asciicasts, :featured + end +end diff --git a/db/schema.rb b/db/schema.rb index a598a3f..8c4c465 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,30 +11,32 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120311134312) do +ActiveRecord::Schema.define(:version => 20120311142204) do create_table "asciicasts", :force => true do |t| t.integer "user_id" t.string "title" - t.float "duration", :null => false + t.float "duration", :null => false t.datetime "recorded_at" t.string "terminal_type" - t.integer "terminal_columns", :null => false - t.integer "terminal_lines", :null => false + t.integer "terminal_columns", :null => false + t.integer "terminal_lines", :null => false t.string "command" t.string "shell" t.string "uname" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.string "stdin" t.string "stdin_timing" t.string "stdout" t.string "stdout_timing" t.string "user_token" t.text "description" + t.boolean "featured", :default => false end add_index "asciicasts", ["created_at"], :name => "index_asciicasts_on_created_at" + add_index "asciicasts", ["featured"], :name => "index_asciicasts_on_featured" add_index "asciicasts", ["recorded_at"], :name => "index_asciicasts_on_recorded_at" add_index "asciicasts", ["user_id"], :name => "index_asciicasts_on_user_id" add_index "asciicasts", ["user_token"], :name => "index_asciicasts_on_user_token"