|
class Mission < ActiveRecord::Base
extend FriendlyId
friendly_id :title, use: :slugged
validates_presence_of :title, :slug
belongs_to :owner, :class_name => "User"
has_many :mission_agents, :dependent => :destroy
has_many :agent_steps, :through => :mission_agents
accepts_nested_attributes_for :mission_agents, allow_destroy:true
mount_uploader :cover_img, MissionCoverUploader
process_in_background :cover_img
def agent_count
return self.mission_agents.count
end
def confirmed_agent_count
confirmed_agents = 0
self.mission_agents.each do |agent|
if agent.user != nil
confirmed_agents = confirmed_agents + 1
end
end
return confirmed_agents
end
def agent_count_stats
return String.new("#{self.confirmed_agent_count}/#{self.agent_count}")
end
def step_count
total_steps = 0
self.mission_agents.each do |agent|
total_steps = total_steps + agent.agent_steps.count
end
return total_steps
end
def completed_steps_count
completed_steps = 0
self.mission_agents.each do |agent|
agent.agent_steps.each do |step|
if step.completed
completed_steps = completed_steps + 1
end
end
end
return completed_steps
end
def step_count_stats
return String.new("#{self.completed_steps_count}/#{self.step_count}")
end
def is_agent(user)
if user
if self.mission_agents.find_all_by_user_id(user.id).count > 0
return true
end
end
return false
end
def check_for_completion
self.mission_agents.each do |agent|
agent.agent_steps.each do |step|
if step.completed == true || step.completed == nil
return false
end
end
end
self.update(status: 3)
return true
end
def days_left
if self.status == 3 || self.status == 4
return '0'
elsif self.status == 1 && self.end_date == nil
return '?'
elsif self.end_date
time_dif = self.end_date - Time.now
days_left = time_dif / 60 / 60 / 24
return days_left.round.to_s
else
return '∞'
end
end
end
|