Skip to content
Snippets Groups Projects
Commit 0d3dafa1 authored by Israel Barreto Sant'Anna's avatar Israel Barreto Sant'Anna
Browse files

Created Rating Controller, Policy and Test. Modified ReviewRatingSerializer to return rating name.

parent 672bf010
No related branches found
No related tags found
No related merge requests found
class V1::RatingsController < ApplicationController
include ::DeletedObjectsController
before_action :set_rating, only: [:show, :update, :destroy]
before_action :authenticate_user!, only: [:create, :update, :destroy]
# GET v1/ratings
# GET v1/ratings.json
def index
render json: Rating.all
end
# GET v1/ratings/1
# GET v1/ratings/1.json
def show
render json: @rating
end
# POST v1/ratings
# POST v1/ratings.json
def create
rating = Rating.new(rating_params)
if rating.save
render json: rating, status: :created
else
render json: rating.errors, status: :unprocessable_entity
end
end
# PUT/PATCH /v1/ratings/1
# PUT/PATCH /v1/ratings/1.json
def update
if @rating.update(rating_params)
render json: @rating, status: :ok
else
render json: @rating.errors, status: :unprocessable_entity
end
end
# DELETE v1/ratings/1
# DELETE v1/ratings/1.json
def destroy
@rating.destroy
render nothing: true, status: :ok
end
private
def deleted_resource
Rating
end
def set_rating
@rating = Rating.find(params[:id])
end
def rating_params
params.require(:rating).permit(:name, :description)
end
end
class RatingPolicy < ApplicationPolicy
def index?
record
end
def show?
record
end
def create?
record if user.is_admin?
end
def update?
record if user.is_admin?
end
def destroy?
record if user.is_admin?
end
end
class ReviewRatingSerializer < ActiveModel::Serializer
attributes :review_id, :rating_id, :value
def rating_name
object.rating.name
end
attributes :id, :review_id, :rating_id, :rating_name, :value
end
class ReviewSerializer < ActiveModel::Serializer
attributes :id, :name, :description, :pros, :cons, :review_ratings, :rates_count, :created_at, :updated_at, :reviewable, :user
attributes :id, :name, :description, :pros, :cons, :rates_count, :created_at, :updated_at, :reviewable, :user
has_many :review_ratings
end
......@@ -103,6 +103,7 @@ Rails.application.routes.draw do
resources :object_types, except: [:new, :edit]
resources :roles, except: [:new, :edit]
resources :scores, only: [:index, :show, :update]
resources :ratings, except: [:new, :edit]
post '/package', to: 'packages#link'
end
......
require 'test_helper'
class V1::RatingControllerTest < ActionController::TestCase
tests V1::RatingsController
include Devise::TestHelpers
test 'should get index' do
skip('Unsolved issue: ArgumentError: wrong number of arguments (2 for 0)')
end
test 'should user show rating' do
auth_application
usability = ratings(:usability)
get :show, id: usability.id
assert_response :ok
end
test 'should user post rating to create and return :unauthorized' do
post :create, rating: {name: 'my rating',description: 'testing rating'}
assert_response :unauthorized
end
test 'should user post rating to create and return :created code' do
auth_request users(:jack)
post :create, rating: {name: 'my rating',description: 'testing rating'}
assert_response :created
end
test 'should user put rating to update and return :ok' do
auth_request users(:jack)
usability = ratings(:usability)
put :update, id: usability.id , rating: { description: 'testing rating'}
assert_response :ok
end
test 'should user put rating to update and return :unauthorized' do
usability = ratings(:usability)
put :update, id: usability.id , rating: { description: 'testing rating'}
assert_response :unauthorized
end
test 'should user delete rating to destroy and return :ok' do
auth_request users(:jack)
usability = ratings(:usability)
delete :destroy, id: usability.id
assert_response :ok
end
test 'should user delete rating to destroy and return :unauthorized' do
usability = ratings(:usability)
delete :destroy, id: usability.id
assert_response :unauthorized
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment