module KirguduBase module Security class User < ::KirguduBase::BaseModel ##################################### BELONGS ################################# include ::KirguduBase::Concerns::Models::CreatedBy include ::KirguduBase::Concerns::Models::UpdatedBy has_many :authentications, class_name: ::KirguduBase::Security::Authentication, foreign_key: :user_id, dependent: :destroy ##################################### VALIDATIONS ################################# validates_uniqueness_of :email, case_sensitive: false validates_presence_of :first_name, :email validates_presence_of :password, only: [:create, :password_set] validates_confirmation_of :password, only: [:create, :password_set] validates_length_of :email, maximum: 128, allow_blank: true validates_length_of :last_name, maximum: 128, allow_blank: true validates_length_of :first_name, maximum: 128, allow_blank: true validates_length_of :middle_name, maximum: 128, allow_blank: true validates_length_of :city_name, maximum: 128, allow_blank: true validates_length_of :phone1, :phone2, :middle_name, maximum: 20, allow_blank: true validates_email_format_of :email, allow_blank: true ##################################### SCOPES ################################# scope :with_status, lambda { |value| where { status_id == value } if value } scope :with_keyword, lambda { |value| if value value = value.gsub('%', '') like_value = "%#{value}%" where { (first_name.like like_value) | (last_name.like like_value) } end } def self.apply_filters_to_query(query, filters={}) filters ||= {} filters.merge!({ order: { last_name: :asc, first_name: :asc } }) super(query, filters) end ##################################### DATABASE METHODS ################################# def self.get_by_email(email) self.where(email: email.to_s).first if email end def self.create_system_user(email) new_user = User.new(first_name: "SYSTEM", email: email, password: ::ChupakabraTools::Security.get_password_hash(SecureRandom.uuid), is_system: true, uin: SecureRandom.uuid) new_user.save new_user end def update_password!(password) self.password = ::ChupakabraTools::Security.get_password_hash(password) self.save! end ##################################### METHODS ################################# def full_name nm = self.first_name if self.last_name nm = "#{self.last_name} #{nm}" end nm end def status_name # end def name_for_breadcrumbs full_name end def to_s self.full_name end def as_json(options = { }) super(merge_options_for_json(options,{ :methods => [:full_name], except: [:password] })) end end end end