sms-pager/app/models/person.rb

59 lines
1.3 KiB
Ruby

class Person < ActiveRecord::Base
authenticates_with_sorcery!
has_many :parenthoods
has_many :children, through: :parenthoods
has_many :authentications, dependent: :destroy
accepts_nested_attributes_for :authentications
accepts_nested_attributes_for :children, reject_if: :all_blank
validates :first_name, presence: true
validates :last_name, presence: true
validates :phone, presence: true
scope :with_name, lambda { |name|
where("first_name || ' ' || last_name LIKE ?", "%#{name}%")
}
scope :just_parents, lambda {
joins(:children)
.order(:first_name, :last_name)
.uniq
}
scope :pageable, -> { where('phone is not NULL').order(:first_name) }
scope :staff, -> { where(staff: true) }
scope :admins, -> { where(admin: true) }
def name
"#{first_name} #{last_name}"
end
def to_i
id
end
def active?
activation_state == "active"
end
def active
activation_state == 'active'
end
def active=(bool)
if bool
self.activation_state = 'active'
else
self.ctivation_state = nil
end
end
private
## SorceryCore expects the model to hold a crypted_password field
## Since we are only using external oAuth providers, faking this one out.
def crypted_password; end
end