Skip to content

Commit

Permalink
Create Reviews controller
Browse files Browse the repository at this point in the history
  • Loading branch information
PaoloCappelli committed Nov 20, 2024
1 parent 10f1c23 commit 925652e
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
28 changes: 28 additions & 0 deletions app/controllers/reviews_controller.rb
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
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@
resource :user_onboardings, only: :update

resources :current_optional_subjects, only: :index
resources :reviews, only: [:create, :update, :destroy]
end
60 changes: 60 additions & 0 deletions spec/controllers/reviews_controller_spec.rb
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
2 changes: 2 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")

config.include Devise::Test::IntegrationHelpers, type: :request
end

Shoulda::Matchers.configure do |config|
Expand Down

0 comments on commit 925652e

Please sign in to comment.