Skip to content

Instantly share code, notes, and snippets.

@wolfieorama
Created March 23, 2017 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolfieorama/c8c071ad6185af787ef4ca97162a72e4 to your computer and use it in GitHub Desktop.
Save wolfieorama/c8c071ad6185af787ef4ca97162a72e4 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
include UserState
include Filterable
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
validates :first_name, :last_name, :email, :phone_number,
:postal_code, :physical_address, :alternative_email, presence: true
validates :email, uniqueness: true
validates :postal_code, length: { is: 5, message: 'Must be 5 digits' }
validates :phone_number, length: { is: 10, message:
'Must a 10 digit valid Mobile Number' }
validates :email, format: {
with: /\A[a-zA-Z0-9_.+-]+@(?:(?:[a-zA-Z0-9-]+\.)?
[a-zA-Z]+\.)?(bobo|boboku)\.org\z/,
message: 'Must be a samadc.org or a samasource.org account'
}
has_many :attendances, dependent: :destroy
has_many :incidents
has_many :weekly_performance_reviews
has_many :job_queues
has_many :memberships
has_many :teams, through: :memberships
has_many :teams, foreign_key: 'team_leader_id'
has_many :pip_records
has_many :pip_stages, through: :pip_records
has_many :jobs, foreign_key: 'account_manager_id'
scope :has_active_team, lambda {
joins(:memberships).merge(Membership.active_membership)
}
enum level: { Admin: 0, TeamLeader: 1, QualityAnalyst: 2, Agent: 3,
HumanResource: 4, AccountManager: 5 }
scope :active_users, -> { where(active: true) }
scope :inactive_users, -> { where(active: false) }
scope :state, ->(state) { where state: state }
scope :agents_name, ->(user_id) { where id: user_id if user_id.present? }
scope :email, ->(email) { where('Email like ?', "#{email}%") }
scope :level, ->(level) { where level: level }
scope :account_managers, -> { where(level: 5) }
def current_team
memberships.active_membership.first.try(:team)
end
def name
"#{first_name} #{middle_name}. #{last_name}".titleize
end
def account_manager?
role? 'AccountManager'
end
def human_resource?
role? 'HumanResource'
end
def role?(role)
level == role && active?
end
def teamleader?
role?('TeamLeader') || role?('QualityAnalyst')
end
def qualityanalyst?
role? 'QualityAnalyst'
end
def admin?
role? 'Admin'
end
def agent?
role? 'Agent'
end
def active?
active == true
end
def inproduction?
state == 'onproduction'
end
def active_for_authentication?
# remember to call the super
# then put our own check to determine "active" state using
# our own "is_active" column
super && active?
end
def last_user_event
user_state_transitions.where(event: 'to_production').last
end
def build_list
has_existing_wpr = WeeklyPerformanceReview
.where(['user_id = ? and created_at >= ?', id, 1.weeks
.ago.to_date]).count.positive?
return unless has_existing_wpr == false
send_reminder
end
def notify_pip_status
Resque.enqueue(SendPipUpdate, id)
end
def notify_pip_warning
Resque.enqueue(SendPipWarning, id)
end
def send_reminder
Resque.enqueue(SendWprReminder, id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment