46 lines
1.3 KiB
Ruby
46 lines
1.3 KiB
Ruby
#coding=utf-8
|
|
class Challenge < ActiveRecord::Base
|
|
default_scope :order => 'position'
|
|
belongs_to :shixun,:touch=> true
|
|
belongs_to :user
|
|
has_many :challenge_samples, :dependent => :destroy
|
|
has_many :test_sets, :dependent => :destroy
|
|
has_many :challenge_tags, :dependent => :destroy
|
|
has_many :games
|
|
|
|
validates_presence_of :subject
|
|
validates_presence_of :score
|
|
validates_presence_of :task_pass
|
|
validates_length_of :subject, :maximum => 255
|
|
|
|
def game_difficulty
|
|
str = "简单"
|
|
case self.difficulty
|
|
when 1
|
|
str = "简单"
|
|
when 2
|
|
str = "中等"
|
|
when 3
|
|
str = "困难"
|
|
end
|
|
str
|
|
end
|
|
|
|
def next_challenge
|
|
challenge_count = Challenge.where(:shixun_id => self.shixun_id).count
|
|
render_404 if self.position ==challenge_count
|
|
Challenge.where(:position => (self.position + 1), :shixun_id => self.shixun).first
|
|
end
|
|
|
|
def last_challenge
|
|
render_404 if self.position < 2
|
|
position = self.position - 1
|
|
Challenge.where(:position => position, :shixun_id => self.shixun).first
|
|
end
|
|
|
|
def current_user_game
|
|
myshixun = Myshixun.where(:shixun_id => self.shixun.id, :user_id => User.current.id).first
|
|
game = Game.where(:myshixun_id => myshixun.id, :challenge_id => self.id).first
|
|
end
|
|
end
|