forgeplus/app/controllers/ratings_controller.rb

62 lines
1.6 KiB
Ruby
Raw Permalink Normal View History

2024-08-15 13:57:09 +08:00
class RatingsController < ApplicationController
RATE_TYPE = ["issue"]
2024-08-15 16:25:19 +08:00
SCORES = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2024-08-15 13:57:09 +08:00
before_action :require_login
2024-08-15 17:49:15 +08:00
before_action :check_create_params, :set_relate_object, only: [:create, :index]
before_action :check_score, only: [:create]
2024-08-15 13:57:09 +08:00
# GET /ratings
# GET /ratings.json
def index
2024-08-15 17:49:15 +08:00
project = @relate_object.project
@ratings =
if project.manager?(current_user)
2024-08-15 17:52:05 +08:00
@relate_object.ratings
2024-08-15 17:49:15 +08:00
else
2024-08-15 17:52:05 +08:00
@relate_object.ratings.where(rater_id: current_user)
2024-08-15 17:49:15 +08:00
end
2024-08-15 13:57:09 +08:00
end
# POST /ratings
# POST /ratings.json
def create
2024-08-15 16:25:19 +08:00
@rating = @relate_object.ratings.new(rating_params.merge(rater_id: current_user.id))
2024-08-15 13:57:09 +08:00
@rating.save!
end
private
def set_relate_object
2024-08-15 16:25:19 +08:00
@relate_object = params[:ratee_type].strip.classify.constantize.find_by(id: params[:ratee_id])
2024-08-15 13:57:09 +08:00
2024-08-15 16:25:19 +08:00
tip_exception("未发现id为#{params[:ratee_id]}的评分对象") if @relate_object.blank?
2024-08-15 13:57:09 +08:00
check_user_access!
end
def check_create_params
2024-08-15 16:04:35 +08:00
tip_exception("ratee_type参数有误.") unless RATE_TYPE.include?(params[:ratee_type]&.to_s&.downcase)
2024-08-15 13:57:09 +08:00
tip_exception("确少ratee_id参数") if params[:ratee_id].blank?
2024-08-15 17:49:15 +08:00
2024-08-15 16:25:19 +08:00
end
def check_score
tip_exception("评分只支持1-10分的整数值") unless SCORES.include?(params[:score].to_i)
2024-08-15 13:57:09 +08:00
end
def check_user_access!
2024-08-15 16:25:19 +08:00
return if current_user.admin_or_business? || @relate_object.project.all_collaborators.ids.include?(current_user.id)
2024-08-15 13:57:09 +08:00
2024-08-15 17:49:15 +08:00
tip_exception('您暂时无权限')
2024-08-15 13:57:09 +08:00
end
# Only allow a list of trusted parameters through.
def rating_params
2024-08-15 16:25:19 +08:00
params.require(:rating).permit(:score, :ratee_id)
2024-08-15 13:57:09 +08:00
end
end