|
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :invitable, :database_authenticatable, :async, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :password, presence: true, length: {minimum: 5, maximum: 120}, on: :create
validates :password, length: {minimum: 5, maximum: 120}, on: :update, allow_blank: true
has_many :posts
has_many :mission_agents
mount_uploader :avatar, AvatarUploader
process_in_background :avatar
def send_reset_password_instructions
super if invitation_token.nil?
end
def full_name
name = self.first_name.to_s + ' ' + self.last_name.to_s
return name
end
after_create do
after_signup_tasks
end
def after_signup_tasks
# Add user to subscription list
if Subscription.find_by_email(self.email) == nil
Subscription.create(first_name: self.first_name, last_name: self.last_name, email: self.email)
end
# Save user current language
self.update(language: I18n.locale.to_s)
end
def incomplete_step_count
total = 0
self.mission_agents.each do |agent|
total = total + agent.incomplete_step_count
end
return total
end
def mission_count
return self.mission_agents.count
end
def completed_missions_count
total = 0
self.mission_agents.each do |agent|
if agent.mission.status == 3
total = total + 1
end
end
return total
end
def directing_missions_count
return Mission.where(owner_id: self.id).count
end
def directed_missions_count
return Mission.where(owner_id: self.id).where(status: 3).count
end
end
|