-
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add likes model, controller and tests
- Loading branch information
Showing
13 changed files
with
118 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Place all the styles related to the likes controller here. | ||
// They will automatically be included in application.css. | ||
// You can use Sass (SCSS) here: http://sass-lang.com/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class LikesController < ApplicationController | ||
before_action :authenticate_user! | ||
|
||
def create | ||
like = Like.new(like_params) | ||
|
||
if like.save | ||
render json: like, status: :created | ||
else | ||
render json: { | ||
error: I18n.t('errors.likes.create', message: like.errors.full_messages) | ||
}, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def destroy | ||
like = Like.where(like_params) | ||
|
||
if like.destroy | ||
render json: {}, status: :no_content | ||
else | ||
render json: { | ||
error: I18n.t('errors.likes.destroy', message: like.errors.full_messages) | ||
}, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
private | ||
|
||
def like_params | ||
params.permit(:post_id).merge(user_id: current_user.id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module LikesHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class Like < ApplicationRecord | ||
belongs_to :user | ||
belongs_to :post | ||
|
||
validates :user_id, uniqueness: { scope: :post_id } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CreateLikes < ActiveRecord::Migration[6.0] | ||
def change | ||
create_table :likes do |t| | ||
t.references :user, null: false, foreign_key: true | ||
t.references :post, null: false, foreign_key: true | ||
|
||
t.timestamps | ||
end | ||
|
||
add_index :likes, [:user_id, :post_id], unique: true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FactoryBot.define do | ||
factory :like do | ||
user | ||
post | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Like, type: :model do | ||
let(:like) { FactoryBot.build(:like) } | ||
|
||
it 'is valid' do | ||
expect(like).to be_valid | ||
end | ||
|
||
it 'must have a user_id' do | ||
like.user = nil | ||
expect(like).to be_invalid | ||
end | ||
|
||
it 'must have a post_id' do | ||
like.post = nil | ||
expect(like).to be_invalid | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe 'likes routing', :aggregate_failures, type: :routing do | ||
it 'routes likes' do | ||
expect(post: '/posts/1/likes').to route_to( | ||
controller: 'likes', action: 'create', post_id: "1" | ||
) | ||
expect(delete: '/posts/1/likes').to route_to( | ||
controller: 'likes', action: 'destroy', post_id: "1" | ||
) | ||
|
||
expect(get: '/posts/1/likes').not_to be_routable | ||
expect(get: '/posts/1/likes/1').not_to be_routable | ||
expect(get: '/posts/1/likes/new').not_to be_routable | ||
expect(get: '/posts/1/likes/1/edit').not_to be_routable | ||
expect(patch: '/posts/1/likes/1').not_to be_routable | ||
end | ||
end |