parent
61d288cd5a
commit
a6baf21c2c
@ -0,0 +1,2 @@
|
||||
// Place all the behaviors and hooks related to the matching controller here.
|
||||
// All this logic will automatically be available in application.js.
|
@ -0,0 +1,3 @@
|
||||
// Place all the styles related to the homework_banks controller here.
|
||||
// They will automatically be included in application.css.
|
||||
// You can use Sass (SCSS) here: http://sass-lang.com/
|
@ -0,0 +1,63 @@
|
||||
class HomeworkBanksController < ApplicationController
|
||||
before_action :require_login
|
||||
before_action :find_bank
|
||||
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
|
||||
@bank.update_attributes(name: params[:name], description: params[:description], reference_answer: params[:reference_answer])
|
||||
|
||||
# 作业描述的附件
|
||||
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])
|
||||
tip_exception(403, "无权限") unless (current_user.certification_teacher? && (@bank.is_public || @bank.user_id == current_user.id)) || current_user.admin?
|
||||
end
|
||||
|
||||
def bank_admin
|
||||
tip_exception(403, "无权限") unless (current_user.certification_teacher? && @bank.user_id == current_user.id) || current_user.admin?
|
||||
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("min_num参数不能为空") if params[:homework_bank][:min_num].blank?
|
||||
tip_exception("max_num参数不能为空") 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
|
@ -0,0 +1,2 @@
|
||||
module HomeworkBanksHelper
|
||||
end
|
@ -0,0 +1,9 @@
|
||||
json.(@bank, :id, :name, :description, :homework_type, :is_public, :min_num, :max_num, :base_on_project, :reference_answer)
|
||||
|
||||
json.attachments @bank_attachments do |attachment|
|
||||
json.partial! "attachments/attachment_simple", locals: {attachment: attachment}
|
||||
end
|
||||
|
||||
json.reference_attachments @reference_attachments do |attachment|
|
||||
json.partial! "attachments/attachment_simple", locals: {attachment: attachment}
|
||||
end
|
@ -0,0 +1,5 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe HomeworkBanksController, type: :controller do
|
||||
|
||||
end
|
@ -0,0 +1,15 @@
|
||||
require 'rails_helper'
|
||||
|
||||
# Specs in this file have access to a helper object that includes
|
||||
# the HomeworkBanksHelper. For example:
|
||||
#
|
||||
# describe HomeworkBanksHelper do
|
||||
# describe "string concat" do
|
||||
# it "concats two strings with spaces" do
|
||||
# expect(helper.concat_strings("this","that")).to eq("this that")
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
RSpec.describe HomeworkBanksHelper, type: :helper do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
Loading…
Reference in new issue