Skip to content
Snippets Groups Projects
Commit b72b6aff authored by Mateus Rambo Strey's avatar Mateus Rambo Strey
Browse files

Merge branch 'fix-review-controller' into 'master'

Fix review controller, and added ratings for review



See merge request !248
parents 9cbb3517 8fe5e4a0
No related branches found
No related tags found
No related merge requests found
......@@ -4,7 +4,9 @@ class V1::ReviewsController < ApplicationController
# GET /v1/collections/1/reviews
def index
render json: reviewable.reviews
render json: ActiveModel::ArraySerializer.new(
reviewable.reviews,
each_serializer: ReviewSerializer)
end
# GET /v1/collections/1/reviews/1
......@@ -16,12 +18,34 @@ class V1::ReviewsController < ApplicationController
# POST /v1/learning_objects/1/reviews.json
def create
review = reviewable.reviews.new(review_params.merge(user: current_user))
errors = process_creation review
if review.save
# If review has any errors, status 422
if errors.any?
render json: errors, status: :unprocessable_entity
else
render json: review, status: :created
end
end
def process_creation(review)
# Store errors
errors = []
# If review saved, without errors ...
if review.save
# ... build ratings objects from parameters
review.review_ratings.each do |r|
r.review = review
# ... and store if
r.save
errors << r.errors
end
else
render json: review.errors, status: :unprocessable_entity
errors << review.errors
end
errors = errors.map { |e| e.messages }.inject(:merge)
end
# DELETE /v1/learning_objects/1/reviews/2
......@@ -56,7 +80,7 @@ class V1::ReviewsController < ApplicationController
# Never trust parameters from the scary internet, only allow the white list through.
def review_params
params.require(:review).permit(:name, :description, :pros, :cons)
params.require(:review).permit(:name, :description, :pros, :cons, review_ratings_attributes: [:rating_id, :value])
end
def set_review
......
......@@ -22,13 +22,14 @@ class Review < ActiveRecord::Base
has_many :review_ratings
has_many :ratings, through: :review_ratings
has_many :rates
has_many :complaints, as: :complaintable
validates_presence_of :user, :reviewable
validates_presence_of :user, :reviewable, :review_ratings
validates_inclusion_of :reviewable_type, in: %w(LearningObject Collection), message: 'Only LearningObjects and Collections are reviewable.'
validates_uniqueness_of :user, scope: [:reviewable_id, :reviewable_type]
accepts_nested_attributes_for :review_ratings
default_scope { includes(:user) }
def rating_values
......
......@@ -15,7 +15,7 @@ class ReviewRating < ActiveRecord::Base
belongs_to :rating
validates_inclusion_of :value, in: Array(1..5), message: "Review Rating must be between 1 and 5."
validates_presence_of :review, :rating
validates_presence_of :rating
validates_uniqueness_of :rating, scope: :review
def self.default_names
......
class ReviewRatingSerializer < ActiveModel::Serializer
attributes :review_id, :rating_id, :value
end
class ReviewSerializer < ActiveModel::Serializer
attributes :id, :name, :description, :pros, :cons, :rates_count, :created_at, :updated_at, :reviewable, :user
attributes :id, :name, :description, :pros, :cons, :review_ratings, :rates_count, :created_at, :updated_at, :reviewable, :user
end
......@@ -34,10 +34,15 @@ class V1::ReviewsControllerTest < ActionController::TestCase
object_param_name = "#{object.class.to_s.downcase}_id".to_sym
object_param_name = 'learning_object_id' if LearningObject == object.class
post :create, review: {
name: 'Test of review',
description: 'testing',
pros: 'my pros', cons: 'my cons'
name: 'Test of review',
description: 'testing',
pros: 'my pros',
cons: 'my cons',
review_ratings_attributes: [
{ rating_id: ratings(:usability).id, value: 3 },
{ rating_id: ratings(:application_context).id, value: 3 },
{ rating_id: ratings(:content).id, value: 3 }
]
}, object_param_name => object.id
end
end
......@@ -16,7 +16,6 @@ class ReviewRatingTest < ActiveSupport::TestCase
should belong_to :review
should belong_to :rating
should validate_presence_of(:review)
should validate_presence_of(:rating)
should validate_inclusion_of(:value).in_array(Array(1..5)).with_message('Review Rating must be between 1 and 5.')
......
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