Data model for "Like"

openid
Marcin Kulik 12 years ago
parent bd85ecda47
commit 0cbd732980

@ -9,6 +9,7 @@ class Asciicast < ActiveRecord::Base
belongs_to :user
has_many :comments, :order => :created_at, :dependent => :destroy
has_many :likes, :dependent => :destroy
scope :featured, where(:featured => true)

@ -0,0 +1,4 @@
class Like < ActiveRecord::Base
belongs_to :asciicast, :counter_cache => true
belongs_to :user
end

@ -1,5 +1,9 @@
class User < ActiveRecord::Base
has_many :user_tokens, :dependent => :destroy
has_many :asciicasts, :dependent => :destroy
has_many :likes, :dependent => :destroy
validates :provider, :presence => true
validates :uid, :presence => true
validates :nickname, :presence => true
@ -8,9 +12,6 @@ class User < ActiveRecord::Base
:nickname,
:message => "Sorry, but your nickname is already taken"
has_many :user_tokens, :dependent => :destroy
has_many :asciicasts, :dependent => :destroy
def self.create_with_omniauth(auth)
user = new
user.provider = auth["provider"]

@ -0,0 +1,14 @@
class CreateLikes < ActiveRecord::Migration
def change
create_table :likes do |t|
t.integer :asciicast_id, :null => false
t.integer :user_id, :null => false
t.timestamps
end
add_index :likes, :asciicast_id
add_index :likes, :user_id
add_index :likes, [:user_id, :asciicast_id]
end
end

@ -0,0 +1,6 @@
class AddLikesCountToAsciicast < ActiveRecord::Migration
def change
add_column :asciicasts, :likes_count, :integer
add_index :asciicasts, :likes_count
end
end

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20120403165915) do
ActiveRecord::Schema.define(:version => 20120406115845) do
create_table "asciicasts", :force => true do |t|
t.integer "user_id"
@ -34,10 +34,12 @@ ActiveRecord::Schema.define(:version => 20120403165915) do
t.text "description"
t.boolean "featured", :default => false
t.string "username"
t.integer "likes_count"
end
add_index "asciicasts", ["created_at"], :name => "index_asciicasts_on_created_at"
add_index "asciicasts", ["featured"], :name => "index_asciicasts_on_featured"
add_index "asciicasts", ["likes_count"], :name => "index_asciicasts_on_likes_count"
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"
@ -53,6 +55,17 @@ ActiveRecord::Schema.define(:version => 20120403165915) do
add_index "comments", ["asciicast_id"], :name => "index_comments_on_asciicast_id"
add_index "comments", ["user_id"], :name => "index_comments_on_user_id"
create_table "likes", :force => true do |t|
t.integer "asciicast_id", :null => false
t.integer "user_id", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "likes", ["asciicast_id"], :name => "index_likes_on_asciicast_id"
add_index "likes", ["user_id", "asciicast_id"], :name => "index_likes_on_user_id_and_asciicast_id"
add_index "likes", ["user_id"], :name => "index_likes_on_user_id"
create_table "user_tokens", :force => true do |t|
t.integer "user_id", :null => false
t.string "token", :null => false

@ -0,0 +1,8 @@
# Read about factories at http://github.com/thoughtbot/factory_girl
FactoryGirl.define do
factory :like do
asciicast_id 1
user_id 1
end
end

@ -0,0 +1,4 @@
require 'spec_helper'
describe Like do
end
Loading…
Cancel
Save