You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/app/models/hack.rb

94 lines
2.9 KiB

5 years ago
class Hack < ApplicationRecord
5 years ago
# status: 0 未发布; 1已发布
# diffcult: 难度 1简单2中等 3困难
5 years ago
# 编程题
validates_length_of :name, maximum: 60, message: "不能超过60个字符"
validates_length_of :description, maximum: 5000, message: "不能超过5000个字符"
5 years ago
validates_presence_of :description, message: "描述不能为空"
validates_presence_of :name, message: "名称不能为空"
5 years ago
# 测试集
has_many :hack_sets, ->{order("position asc")}, :dependent => :destroy
# 代码
has_many :hack_codes, :dependent => :destroy
has_many :hack_user_lastest_codes, :dependent => :destroy
5 years ago
has_many :discusses, as: :dis, dependent: :destroy
5 years ago
# 点赞
has_many :praise_treads, as: :praise_tread_object, dependent: :destroy
5 years ago
# 消息
has_many :tidings, as: :container
5 years ago
# 知识点
5 years ago
has_many :tag_discipline_containers, as: :container, dependent: :destroy
5 years ago
has_one :exercise_question, dependent: :destroy
belongs_to :user
5 years ago
belongs_to :sub_discipline
5 years ago
5 years ago
has_one :item_bank, as: :container, dependent: :destroy
5 years ago
scope :published, -> { where(status: 1) }
5 years ago
scope :unpublish, -> { where(status: 0) }
5 years ago
scope :opening, -> {where(open_or_not: 1)}
scope :mine, -> (author_id){ where(user_id: author_id) }
5 years ago
def language
if hack_codes.count == 1
hack_codes.first.language
else
5 years ago
hack_codes.pluck(:language).first
5 years ago
end
end
def exercise_course
exercise_question.present? ? exercise_question&.exercise&.course : nil
end
5 years ago
def code
if hack_codes.count == 1
5 years ago
#tran_base64_decode64(hack_codes.first.code)
hack_codes.first.code
5 years ago
else
5 years ago
#tran_base64_decode64(hack_codes.pluck(:code))
hack_codes.pluck(:code)
5 years ago
end
end
# 用于用户调试的第一个测试用例
def input_test_case
hack_sets.first&.input
end
# 管理员
5 years ago
def manager?(user)
user_id == user.id || user.admin_or_business?
end
5 years ago
# 复制fork
def fork
new_hack = Hack.new
new_hack.attributes = self.attributes.dup.except("id", "user_id", "status", "identifier", "comments_count", "praises_count",
"pass_num", "created_at", "updated_at", "hack_user_lastest_codes_count",
"open_or_not", "submit_num")
new_hack.user_id = User.current.id
new_hack.identifier = Util::UUID.generate_identifier(Hack, 8)
new_hack.fork_id = self.id
new_hack.save!
# 创建测试集与代码
hack_sets.each do |set|
new_hack.hack_sets.create!(input: set.input, output: set.output, position: set.position)
end
# 新建知识点
tag_discipline_containers.each do |tag|
new_hack.tag_discipline_containers.create!(tag_discipline_id: tag.tag_discipline_id)
end
hack_codes.each do |code|
new_hack.hack_codes.create!(code: code.code, language: code.language, modify_time: Time.now)
end
new_hack
end
5 years ago
end