2016-12-26 12:13:59 +08:00
|
|
|
class Mission < ActiveRecord:: Base
|
|
|
|
|
2016-12-27 20:15:05 +08:00
|
|
|
has_many :comments
|
|
|
|
|
2016-12-29 17:33:25 +08:00
|
|
|
|
2016-12-27 20:15:05 +08:00
|
|
|
validates :name, presence: true
|
|
|
|
validates :deadline, presence: true
|
|
|
|
validates :priority, presence: true
|
|
|
|
validates :status, presence: true
|
|
|
|
validates :content, presence: true
|
|
|
|
|
2016-12-29 17:33:25 +08:00
|
|
|
validates :project_id, presence: true
|
|
|
|
|
|
|
|
|
2016-12-27 20:15:05 +08:00
|
|
|
validate :priority_is_among_valid_values
|
|
|
|
validate :status_is_among_valid_values
|
|
|
|
|
2016-12-29 17:33:25 +08:00
|
|
|
validate :project_exists, on: :create
|
|
|
|
|
2016-12-27 20:15:05 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def priority_is_among_valid_values
|
|
|
|
|
|
|
|
if priority != 1 and priority != 2 and priority != 3
|
|
|
|
errors[:priority] = "priority is not among valid values"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def status_is_among_valid_values
|
|
|
|
|
2016-12-29 17:33:25 +08:00
|
|
|
if status != 1 and status != 2 and status != 3 and status != 4
|
2016-12-27 20:15:05 +08:00
|
|
|
errors[:status] = "status is not among valid values"
|
|
|
|
end
|
|
|
|
end
|
2016-12-29 17:33:25 +08:00
|
|
|
|
|
|
|
def project_exists
|
|
|
|
#puts project_id
|
|
|
|
project = Project.find_by(id: project_id)
|
|
|
|
#puts project.inspect
|
|
|
|
if project == nil
|
|
|
|
errors[:project_id] = "project_id is not exist"
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2016-12-26 12:13:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
end
|