JuanVqz/doctors

View on GitHub
app/models/patient.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
94%
# frozen_string_literal: true

class Patient < User
  has_one :address, as: :addressable, dependent: :destroy
  has_and_belongs_to_many :doctors, join_table: 'doctors_patients'
  has_many :appointments, -> { order(created_at: :desc) }, dependent: :destroy
  has_many :hospitalizations, -> { order(created_at: :desc) }, dependent: :destroy

  has_one_attached :avatar

  accepts_nested_attributes_for :address, allow_destroy: true

  validates :email, uniqueness: true, allow_nil: true
  validates :name, :first_name, :birthday, presence: true

  delegate :subdomain, to: :hospital, prefix: true

  delegate :street, :number, :colony, :postal_code, :municipality,
           :state, :country, to: :address, prefix: true, allow_nil: true

  def self.search(query)
    where("unaccent(concat_ws(' ', name, first_name, last_name)) ILIKE unaccent(?)", "%#{query&.squish}%")
  end

  def email_required?
    false
  end

  def password_required?
    false
  end

  def clinic_history?
    allergies.present? ||
      pathological_background.present? ||
      non_pathological_background.present? ||
      gyneco_obstetric_background.present? ||
      system_background.present? ||
      family_inheritance_background.present? ||
      physic_exploration.present? ||
      other_background.present?
  end
end