From f870a7544aec8edbabe1fb616063366ec498698c Mon Sep 17 00:00:00 2001 From: Luis Carlos Date: Thu, 17 Oct 2019 23:35:29 -0500 Subject: [PATCH] Feat/doctor patient permissions (#53) * Viewable Patients view * Simple table added for index view * Controller for viewable patients CRD methods * Patients admin permissions can be admin from views --- .../viewable_patients_controller.rb | 52 +++++++++++++++++++ app/models/doctor.rb | 4 ++ app/views/shared/_doctors_menu.html.erb | 3 ++ app/views/viewable_patients/_form.html.erb | 31 +++++++++++ app/views/viewable_patients/index.html.erb | 35 +++++++++++++ app/views/viewable_patients/new.html.erb | 1 + config/routes.rb | 5 +- 7 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 app/controllers/viewable_patients_controller.rb create mode 100644 app/views/viewable_patients/_form.html.erb create mode 100644 app/views/viewable_patients/index.html.erb create mode 100644 app/views/viewable_patients/new.html.erb diff --git a/app/controllers/viewable_patients_controller.rb b/app/controllers/viewable_patients_controller.rb new file mode 100644 index 0000000..7584569 --- /dev/null +++ b/app/controllers/viewable_patients_controller.rb @@ -0,0 +1,52 @@ +# Patient Viewable Patients Controller controlls or actions regarding Patient Medical records + +class ViewablePatientsController < ApplicationController + before_action :check_patient + + def index + @viewable_patients = current_doctor.shared_patients + end + + def new + @viewable_patient = ViewablePatient.new + @patients = patients + @doctors = doctors + end + + def create + @viewable_patient = ViewablePatient.new(viewable_patient_params) + if @viewable_patient.save + flash[:success] = 'Permiso registrado exitosamente' + redirect_to viewable_patients_path + else + flash.now[:error] = 'Hubo un error con el registro, + verifica los campos del formulario' + render :new + end + end + + def destroy + @viewable_patient = ViewablePatient.find(params[:id]) + @viewable_patient.destroy + flash[:destroy] = 'Permiso eliminado exitosamente' + redirect_to viewable_patients_path + end + + private + + def viewable_patient_params + params.require(:viewable_patient).permit(:doctor_id, :patient_id) + end + + def check_patient + redirect_to root_path unless current_patient.nil? + end + + def patients + current_doctor.patients.map { |p| ["#{p.first_name.titleize} #{p.last_name.titleize}", p.id] } + end + + def doctors + Doctor.where.not(id: current_doctor.id).map { |d| ["#{d.first_name.titleize} #{d.last_name.titleize}", d.id] } + end +end \ No newline at end of file diff --git a/app/models/doctor.rb b/app/models/doctor.rb index 529ecb3..618b593 100644 --- a/app/models/doctor.rb +++ b/app/models/doctor.rb @@ -19,4 +19,8 @@ def viewable_patients def has_patient patient return viewable_patients.include?patient end + + def shared_patients + ViewablePatient.all.map { |permit| permit if permit.patient.doctor == self }.compact + end end diff --git a/app/views/shared/_doctors_menu.html.erb b/app/views/shared/_doctors_menu.html.erb index b53073a..1266706 100644 --- a/app/views/shared/_doctors_menu.html.erb +++ b/app/views/shared/_doctors_menu.html.erb @@ -10,6 +10,9 @@ +