Skip to content

Commit

Permalink
Add hospital relationship to patient (#469)
Browse files Browse the repository at this point in the history
* Add hospital relationship to patient

* Update to use hospital instead of hospital_id
  • Loading branch information
JuanVqz authored Mar 2, 2024
1 parent 44d46fd commit 038b169
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 40 deletions.
6 changes: 0 additions & 6 deletions .standard_todo.yml

This file was deleted.

27 changes: 14 additions & 13 deletions app/controllers/patients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,20 @@ def set_patient
end

def patient_params
params.require(:patient).permit(
:name, :first_name, :last_name, :birthday, :height,
:weight, :blood_group, :occupation, :referred_by,
:place_of_birth, :sex, :cellphone, :marital_status,
:comments, :avatar, :allergies, :pathological_background,
:non_pathological_background, :gyneco_obstetric_background,
:system_background, :family_inheritance_background,
:physic_exploration, :other_background,
address_attributes: [
:id, :street, :number, :colony, :postal_code, :municipality,
:state, :country, :_destroy
]
)
params.require(:patient)
.permit(
:name, :first_name, :last_name, :birthday, :height,
:weight, :blood_group, :occupation, :referred_by,
:place_of_birth, :sex, :cellphone, :marital_status,
:comments, :avatar, :allergies, :pathological_background,
:non_pathological_background, :gyneco_obstetric_background,
:system_background, :family_inheritance_background,
:physic_exploration, :other_background,
address_attributes: [
:id, :street, :number, :colony, :postal_code, :municipality,
:state, :country, :_destroy
]
).with_defaults(hospital_id: current_user.hospital_id)
end

def pdf_name
Expand Down
1 change: 0 additions & 1 deletion app/models/doctor.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class Doctor < User
belongs_to :hospital
has_and_belongs_to_many :patients, join_table: "doctors_patients"
has_many :appoinments
has_many :hospitalizations, -> { order(created_at: :desc) }
Expand Down
2 changes: 1 addition & 1 deletion app/models/hospital.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ class Hospital < ApplicationRecord

enum plan: {basic: 0, medium: 1}

has_one :address, as: :addressable, dependent: :destroy
has_many :doctors, dependent: :destroy
has_many :patient_referrals, dependent: :destroy
has_many :patients, dependent: :destroy
has_one :address, as: :addressable, dependent: :destroy

accepts_nested_attributes_for :address, allow_destroy: true

Expand Down
3 changes: 1 addition & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class User < ApplicationRecord
default_scope { where(hospital_id: Hospital.current_id) }

belongs_to :hospital
enum role: {patient: 0, doctor: 1, admin: 2}

validates :role, presence: true
Expand Down
1 change: 1 addition & 0 deletions spec/factories/patients.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
role { "patient" }
type { "Patient" }
confirmed_at { Time.zone.now }
association :hospital, factory: :hospital

after :build do |patient|
if patient.doctors.nil?
Expand Down
1 change: 1 addition & 0 deletions spec/models/patient_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "rails_helper"

RSpec.describe Patient do
it { should belong_to :hospital }
it { should have_one(:address).dependent(:destroy) }
it { should have_and_belong_to_many :doctors }
it { should have_many(:appoinments).dependent(:destroy) }
Expand Down
4 changes: 2 additions & 2 deletions spec/requests/appoinments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

RSpec.describe "appoinments", type: :request do
let(:hospital) { create(:hospital, :basic) }
let(:doctor) { create(:doctor, hospital_id: hospital.id) }
let(:doctor) { create(:doctor, hospital: hospital) }
let(:patient) do
create(:patient, doctors: [doctor], hospital_id: hospital.id)
create(:patient, doctors: [doctor], hospital: hospital)
end
let(:appoinment) do
create(:appoinment, doctor: doctor, patient: patient)
Expand Down
4 changes: 2 additions & 2 deletions spec/requests/hospitalizations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

RSpec.describe "hospitalizations", type: :request do
let(:hospital) { create(:hospital, :basic) }
let(:doctor) { create(:doctor, hospital_id: hospital.id) }
let(:doctor) { create(:doctor, hospital: hospital) }
let(:patient) do
create(:patient, doctors: [doctor], hospital_id: hospital.id)
create(:patient, doctors: [doctor], hospital: hospital)
end
let(:referred_doctor) { create(:referred_doctor, doctor: doctor) }
let(:hospitalization) do
Expand Down
4 changes: 2 additions & 2 deletions spec/requests/patients_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

RSpec.describe "patients", type: :request do
let(:hospital) { create(:hospital, :basic) }
let(:doctor) { create(:doctor, hospital_id: hospital.id) }
let(:doctor) { create(:doctor, hospital: hospital) }
let(:patient) do
create(:patient, doctors: [doctor], hospital_id: hospital.id)
create(:patient, doctors: [doctor], hospital: hospital)
end

before do
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/referred_doctors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
doctor: nil
}
end
let(:doctor) { create(:doctor, :admin, hospital_id: hospital.id) }
let(:doctor) { create(:doctor, :admin, hospital: hospital) }

before do
allow(Hospital).to receive(:current_id).and_return hospital.id
Expand Down
2 changes: 1 addition & 1 deletion spec/support/feature/authentication_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def sign_in_doctor hospital
end

def sign_in_admin_doctor hospital
@admin = create(:doctor, hospital: hospital, role: "admin")
@admin = create(:doctor, hospital: hospital, role: :admin)
visit new_user_session_path
expect(page).to have_current_path(new_user_session_path)

Expand Down
6 changes: 3 additions & 3 deletions spec/support/feature/subdomain_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module Feature
module SubdomainHelpers
def set_capybara_subdomain
Capybara.app_host = "http://#{@hospital.subdomain}.lvh.me"
def capybara_subdomain(subdomain)
Capybara.app_host = "http://#{subdomain}.lvh.me"
end

def create_hospital_plan_medium
@hospital = create(:hospital, :medium)
set_capybara_subdomain
capybara_subdomain(@hospital.subdomain)
end
end
end
10 changes: 5 additions & 5 deletions spec/system/appoinments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
scenario "creates an appoinment from the show page" do
create_hospital_plan_medium
sign_in_admin_doctor @hospital
@patient = create(:patient)
@patient = create(:patient, hospital: @hospital)

visit patients_path
expect(page).to have_content "Buscar"
Expand All @@ -29,8 +29,8 @@
create_hospital_plan_medium
sign_in_admin_doctor @hospital

@other_patient = create(:patient, name: "Zác", doctors: [@admin])
@patient = create(:patient, name: "Zac", doctors: [@admin])
@other_patient = create(:patient, name: "Zác", doctors: [@admin], hospital: @hospital)
@patient = create(:patient, name: "Zac", doctors: [@admin], hospital: @hospital)

visit patients_path
expect(page).to have_content "Buscar"
Expand All @@ -50,10 +50,10 @@
end
end

scenario "can action some pages", js: true do
scenario "can action some pages" do
create_hospital_plan_medium
sign_in_admin_doctor @hospital
patient = create(:patient, doctors: [@admin])
patient = create(:patient, doctors: [@admin], hospital: @hospital)
appoinment = create(:appoinment, patient: patient, doctor: @admin)

visit appoinments_path(appoinment)
Expand Down
2 changes: 1 addition & 1 deletion spec/system/hospitalizations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
scenario "from patient list" do
create_hospital_plan_medium
sign_in_admin_doctor @hospital
@patient = create(:patient)
@patient = create(:patient, hospital: @hospital)
@referred_doctor = create(:referred_doctor, doctor: @admin)

visit patients_path
Expand Down

0 comments on commit 038b169

Please sign in to comment.