Skip to content
Snippets Groups Projects
Commit aeefd7a8 authored by Lucas Ernesto Kindinger's avatar Lucas Ernesto Kindinger
Browse files

Fiz review controller, and added ratings for review

parent 282833dd
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,8 @@ class V1::ReviewsController < ApplicationController ...@@ -4,7 +4,8 @@ class V1::ReviewsController < ApplicationController
# GET /v1/collections/1/reviews # GET /v1/collections/1/reviews
def index def index
render json: reviewable.reviews render json: ActiveModel::ArraySerializer.new(reviewable.reviews,
each_serializer: ReviewSerializer)
end end
# GET /v1/collections/1/reviews/1 # GET /v1/collections/1/reviews/1
...@@ -17,13 +18,29 @@ class V1::ReviewsController < ApplicationController ...@@ -17,13 +18,29 @@ class V1::ReviewsController < ApplicationController
def create def create
review = reviewable.reviews.new(review_params.merge(user: current_user)) review = reviewable.reviews.new(review_params.merge(user: current_user))
if review.save if process_creation review
render json: review, status: :created render json: review, status: :created
else else
render json: review.errors, status: :unprocessable_entity render json: review.errors, status: :unprocessable_entity
end end
end end
def process_creation(review)
# Status of creation
status = true
# Save review first, without ratings
if review.save
# Build ratings objects from parameters
review.review_ratings.each do |r|
r.review_id = review.id
status &&= r.save
end
end
status
end
# DELETE /v1/learning_objects/1/reviews/2 # DELETE /v1/learning_objects/1/reviews/2
# DELETE /v1/learning_objects/1/reviews/2.json # DELETE /v1/learning_objects/1/reviews/2.json
def destroy def destroy
...@@ -56,7 +73,7 @@ class V1::ReviewsController < ApplicationController ...@@ -56,7 +73,7 @@ class V1::ReviewsController < ApplicationController
# Never trust parameters from the scary internet, only allow the white list through. # Never trust parameters from the scary internet, only allow the white list through.
def review_params 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 end
def set_review def set_review
......
...@@ -22,9 +22,10 @@ class Review < ActiveRecord::Base ...@@ -22,9 +22,10 @@ class Review < ActiveRecord::Base
has_many :review_ratings has_many :review_ratings
has_many :ratings, through: :review_ratings has_many :ratings, through: :review_ratings
has_many :rates has_many :rates
has_many :complaints, as: :complaintable has_many :complaints, as: :complaintable
accepts_nested_attributes_for :review_ratings
validates_presence_of :user, :reviewable validates_presence_of :user, :reviewable
validates_inclusion_of :reviewable_type, in: %w(LearningObject Collection), message: 'Only LearningObjects and Collections are reviewable.' 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] validates_uniqueness_of :user, scope: [:reviewable_id, :reviewable_type]
......
...@@ -15,7 +15,7 @@ class ReviewRating < ActiveRecord::Base ...@@ -15,7 +15,7 @@ class ReviewRating < ActiveRecord::Base
belongs_to :rating belongs_to :rating
validates_inclusion_of :value, in: Array(1..5), message: "Review Rating must be between 1 and 5." 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 :review, :rating
validates_uniqueness_of :rating, scope: :review validates_uniqueness_of :rating, scope: :review
def self.default_names def self.default_names
......
class ReviewSerializer < ActiveModel::Serializer 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 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