diff --git a/app/models/asciicast.rb b/app/models/asciicast.rb index 16e948a..cfce4bd 100644 --- a/app/models/asciicast.rb +++ b/app/models/asciicast.rb @@ -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) diff --git a/app/models/like.rb b/app/models/like.rb new file mode 100644 index 0000000..e55f167 --- /dev/null +++ b/app/models/like.rb @@ -0,0 +1,4 @@ +class Like < ActiveRecord::Base + belongs_to :asciicast, :counter_cache => true + belongs_to :user +end diff --git a/app/models/user.rb b/app/models/user.rb index d02ef88..f762900 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/db/migrate/20120406114956_create_likes.rb b/db/migrate/20120406114956_create_likes.rb new file mode 100644 index 0000000..780318b --- /dev/null +++ b/db/migrate/20120406114956_create_likes.rb @@ -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 diff --git a/db/migrate/20120406115845_add_likes_count_to_asciicast.rb b/db/migrate/20120406115845_add_likes_count_to_asciicast.rb new file mode 100644 index 0000000..9f7a678 --- /dev/null +++ b/db/migrate/20120406115845_add_likes_count_to_asciicast.rb @@ -0,0 +1,6 @@ +class AddLikesCountToAsciicast < ActiveRecord::Migration + def change + add_column :asciicasts, :likes_count, :integer, :null => false, :default => 0 + add_index :asciicasts, :likes_count + end +end diff --git a/db/schema.rb b/db/schema.rb index 737b87b..0df105f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -36,10 +36,12 @@ ActiveRecord::Schema.define(:version => 20120409142151) do t.string "username" t.text "snapshot" t.integer "comments_count", :default => 0, :null => false + t.integer "likes_count", :default => 0, :null => 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", ["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" @@ -55,6 +57,17 @@ ActiveRecord::Schema.define(:version => 20120409142151) 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 diff --git a/spec/factories/likes.rb b/spec/factories/likes.rb new file mode 100644 index 0000000..258aed5 --- /dev/null +++ b/spec/factories/likes.rb @@ -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 diff --git a/spec/models/like_spec.rb b/spec/models/like_spec.rb new file mode 100644 index 0000000..3022944 --- /dev/null +++ b/spec/models/like_spec.rb @@ -0,0 +1,4 @@ +require 'spec_helper' + +describe Like do +end