-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10f1c23
commit 925652e
Showing
4 changed files
with
91 additions
and
0 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,28 @@ | ||
class ReviewsController < ApplicationController | ||
before_action :authenticate_user! | ||
before_action :set_review, only: [:update, :destroy] | ||
|
||
def create | ||
@review = current_user.reviews.create!(subject_id: params[:subject_id], rating: params[:rating]) | ||
|
||
redirect_to subject_path(@review.subject) | ||
end | ||
|
||
def update | ||
@review.update!(rating: params[:rating]) | ||
|
||
redirect_to subject_path(@review.subject) | ||
end | ||
|
||
def destroy | ||
@review.destroy! | ||
|
||
redirect_to subject_path(@review.subject) | ||
end | ||
|
||
private | ||
|
||
def set_review | ||
@review = current_user.reviews.find(params[: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
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,60 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe ReviewsController, type: :request do | ||
let(:user) { create(:user) } | ||
let(:subject) { create(:subject) } | ||
let(:review) { create(:review, user: user, subject: subject) } | ||
|
||
before do | ||
# https://github.com/heartcombo/devise/issues/5705 | ||
Rails.application.reload_routes_unless_loaded | ||
end | ||
|
||
before do | ||
sign_in user | ||
end | ||
|
||
describe 'POST #create' do | ||
it 'creates a new review' do | ||
expect { | ||
post reviews_path, params: { subject_id: subject.id, rating: 5 } | ||
}.to change(Review, :count).by(1) | ||
end | ||
|
||
it 'redirects to the subject page' do | ||
post reviews_path, params: { subject_id: subject.id, rating: 5 } | ||
|
||
expect(response).to redirect_to(subject_path(subject)) | ||
end | ||
end | ||
|
||
describe 'PATCH #update' do | ||
it 'updates the review' do | ||
patch review_path(review), params: { rating: 4 } | ||
|
||
expect(review.reload.rating).to eq(4) | ||
end | ||
|
||
it 'redirects to the subject page' do | ||
patch review_path(review), params: { rating: 4 } | ||
|
||
expect(response).to redirect_to(subject_path(review.subject)) | ||
end | ||
end | ||
|
||
describe 'DELETE #destroy' do | ||
it 'deletes the review' do | ||
review | ||
|
||
expect { | ||
delete review_path(review) | ||
}.to change(Review, :count).by(-1) | ||
end | ||
|
||
it 'redirects to the subject page' do | ||
delete review_path(review) | ||
|
||
expect(response).to redirect_to(subject_path(review.subject)) | ||
end | ||
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