class HomeworkBanksController < ApplicationController before_action :require_login before_action :find_bank, :bank_visit_auth before_action :bank_params, only: [:update] before_action :bank_admin, only: [:update, :destroy, :set_public] def show @bank_attachments = @bank.attachments.where(attachtype: 1) @reference_attachments = @bank.attachments.where(attachtype: 2) end def update ActiveRecord::Base.transaction do if @bank.homework_type == 1 @bank.update_attributes(name: params[:name], description: params[:description], reference_answer: params[:reference_answer]) elsif @bank.homework_type == 3 @bank.update_attributes(name: params[:name], description: params[:description], reference_answer: params[:reference_answer], base_on_project: params[:base_on_project], min_num: params[:min_num], max_num: params[:max_num]) end # 作业描述的附件 Attachment.associate_container(params[:attachment_ids], @bank.id, @bank.class) if params[:attachment_ids] # 作业参考答案的附件 Attachment.associate_container(params[:reference_attachment_ids], @bank.id, @bank.class, 2) if params[:reference_attachment_ids] normal_status(0, "更新成功") end end def destroy ActiveRecord::Base.transaction do @bank.homework_commons.update_all(homework_bank_id: nil) @bank.destroy! normal_status("删除成功") end end def set_public @bank.update_attributes(is_public: 1) normal_status("更新成功") end private def find_bank @bank = HomeworkBank.find_by!(id: params[:id]) end def bank_admin tip_exception(403, "无权限") unless @bank.user_id == current_user.id || current_user.admin_or_business? end def bank_params tip_exception("name参数不能为空") if params[:homework_bank][:name].blank? tip_exception("description参数不能为空") if params[:homework_bank][:description].blank? if @bank.homework_type == 3 tip_exception("base_on_project参数不能为空") if params[:homework_bank][:base_on_project].nil? tip_exception("最小人数不能为空") if params[:homework_bank][:min_num].blank? tip_exception("最大人数参数不能为空") if params[:homework_bank][:max_num].blank? tip_exception("最小人数不能小于1") if params[:homework_bank][:min_num].to_i < 1 tip_exception("最大人数不能小于最小人数") if params[:homework_bank][:max_num].to_i < params[:homework_bank][:min_num].to_i end params.require(:homework_bank).permit(:name, :description, :reference_answer) if @bank.homework_type == 1 params.require(:homework_bank).permit(:name, :description, :reference_answer, :min_num, :max_num, :base_on_project) if @bank.homework_type == 3 end end