Skip to content

Commit 4aed952

Browse files
committed
Merge pull request bywo#17 from byronmwong/feature/likes
create PostLike model and tests
2 parents 287ddde + 4deeced commit 4aed952

File tree

6 files changed

+74
-2
lines changed

6 files changed

+74
-2
lines changed

app/models/post.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
class Post < ActiveRecord::Base
22
belongs_to :user
33
belongs_to :group
4+
has_many :post_likes, inverse_of: :post
5+
6+
def likes
7+
post_likes
8+
end
9+
10+
def add_like!(user)
11+
likes.create!(user: user)
12+
end
13+
14+
def remove_like!(user)
15+
likes.find_by(user_id: user.id).destroy
16+
end
417
end

app/models/post_like.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class PostLike < ActiveRecord::Base
2+
belongs_to :post
3+
belongs_to :user
4+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class CreatePostLikes < ActiveRecord::Migration
2+
def change
3+
create_table :post_likes do |t|
4+
t.belongs_to :post, index: true
5+
t.belongs_to :user, index: true
6+
7+
t.timestamps
8+
end
9+
end
10+
end

db/schema.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended that you check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(version: 20140422120201) do
14+
ActiveRecord::Schema.define(version: 20140427230320) do
1515

1616
# These are extensions that must be enabled in order to support this database
1717
enable_extension "plpgsql"
@@ -36,6 +36,16 @@
3636
t.datetime "updated_at"
3737
end
3838

39+
create_table "post_likes", force: true do |t|
40+
t.integer "post_id"
41+
t.integer "user_id"
42+
t.datetime "created_at"
43+
t.datetime "updated_at"
44+
end
45+
46+
add_index "post_likes", ["post_id"], name: "index_post_likes_on_post_id", using: :btree
47+
add_index "post_likes", ["user_id"], name: "index_post_likes_on_user_id", using: :btree
48+
3949
create_table "posts", force: true do |t|
4050
t.string "content"
4151
t.string "type"

spec/models/post_like_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require 'spec_helper'
2+
3+
describe PostLike do
4+
pending "add some examples to (or delete) #{__FILE__}"
5+
end

spec/models/post_spec.rb

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
require 'spec_helper'
22

33
describe Post do
4-
pending "add some examples to (or delete) #{__FILE__}"
4+
let(:post) { FactoryGirl.create(:song_post) }
5+
6+
subject { post }
7+
8+
it { should respond_to(:group) }
9+
it { should respond_to(:user) }
10+
it { should respond_to(:post_likes) }
11+
it { should respond_to(:likes) }
12+
13+
describe 'managing likes' do
14+
let(:user) { FactoryGirl.create(:user) }
15+
before { @like = post.add_like!(user) }
16+
17+
describe 'add new like' do
18+
its(:likes) { should include(@like) }
19+
end
20+
21+
describe 'new like' do
22+
subject { @like }
23+
its(:user) { should == user }
24+
end
25+
26+
describe 'remove like' do
27+
before { post.remove_like!(user) }
28+
its(:likes) { should_not include(@like) }
29+
30+
it 'throws when removing unexisting like' do
31+
expect { post.remove_like!(user) }.to raise_error()
32+
end
33+
end
34+
end
535
end

0 commit comments

Comments
 (0)