Skip to content

Commit

Permalink
Add likes model, controller and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
riggraz committed Sep 27, 2019
1 parent e1d8dbc commit 970cd69
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/assets/stylesheets/likes.scss
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/
33 changes: 33 additions & 0 deletions app/controllers/likes_controller.rb
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
2 changes: 2 additions & 0 deletions app/helpers/likes_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module LikesHelper
end
6 changes: 6 additions & 0 deletions app/models/like.rb
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
1 change: 1 addition & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class Post < ApplicationRecord
belongs_to :board
belongs_to :user
belongs_to :post_status, optional: true
has_many :likes, dependent: :destroy
has_many :comments, dependent: :destroy

validates :title, presence: true, length: { in: 4..64 }
Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class User < ApplicationRecord
:confirmable

has_many :posts, dependent: :destroy
has_many :likes, dependent: :destroy
has_many :comments, dependent: :destroy

enum role: [:user, :moderator, :admin]
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ en:
post:
create: 'Post create error: %{message}'
update: 'Post update error: %{message}'
like:
create: 'Like create error: %{message}'
comment:
create: 'Comment create error: %{message}'
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@

devise_for :users

resources :boards, only: [:show]
resources :posts, only: [:index, :create, :show, :update] do
resource :likes, only: [:create, :destroy]
resources :comments, only: [:index, :create]
end
resources :boards, only: [:show]
resources :post_statuses, only: [:index]
end
12 changes: 12 additions & 0 deletions db/migrate/20190927094233_create_likes.rb
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
14 changes: 13 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_09_18_151626) do
ActiveRecord::Schema.define(version: 2019_09_27_094233) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -36,6 +36,16 @@
t.index ["user_id"], name: "index_comments_on_user_id"
end

create_table "likes", force: :cascade do |t|
t.bigint "user_id", null: false
t.bigint "post_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["post_id"], name: "index_likes_on_post_id"
t.index ["user_id", "post_id"], name: "index_likes_on_user_id_and_post_id", unique: true
t.index ["user_id"], name: "index_likes_on_user_id"
end

create_table "post_statuses", force: :cascade do |t|
t.string "name", null: false
t.string "color", null: false
Expand Down Expand Up @@ -81,6 +91,8 @@
add_foreign_key "comments", "comments", column: "parent_id"
add_foreign_key "comments", "posts"
add_foreign_key "comments", "users"
add_foreign_key "likes", "posts"
add_foreign_key "likes", "users"
add_foreign_key "posts", "boards"
add_foreign_key "posts", "post_statuses"
add_foreign_key "posts", "users"
Expand Down
6 changes: 6 additions & 0 deletions spec/factories/likes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryBot.define do
factory :like do
user
post
end
end
19 changes: 19 additions & 0 deletions spec/models/like_spec.rb
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
18 changes: 18 additions & 0 deletions spec/routing/likes_routing_spec.rb
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

0 comments on commit 970cd69

Please sign in to comment.