Class: Renalware::Patients::PatientsController

Inherits:
BaseController show all
Includes:
PresenterHelper, Concerns::Pageable
Defined in:
app/controllers/renalware/patients/patients_controller.rb

Instance Method Summary collapse

Methods included from PresenterHelper

#present

Instance Method Details

#createObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/renalware/patients/patients_controller.rb', line 35

def create
  patient = Patient.new(patient_params)
  authorize patient

  if patient.save_by(current_user)
    # Reload in order to let pg generate the secure id
    redirect_to_patient_demographics(patient.reload)
  else
    flash.now[:error] = failed_msg_for("patient")
    render :new, locals: { patient: patient }
  end
end

#editObject



48
49
50
51
# File 'app/controllers/renalware/patients/patients_controller.rb', line 48

def edit
  authorize patient
  render locals: { patient: patient }
end

#indexObject



11
12
13
14
15
16
17
18
19
20
# File 'app/controllers/renalware/patients/patients_controller.rb', line 11

def index
  sort = params.dig(:q, :s)
  patient_search.sorts = sort if sort
  patients = patient_search.result.page(page).per(per_page)
  authorize patients
  render locals: {
    search: patient_search,
    patients: present(patients, PatientPresenter)
  }
end

#newObject



29
30
31
32
33
# File 'app/controllers/renalware/patients/patients_controller.rb', line 29

def new
  patient = Patient.new.tap(&:build_current_address)
  authorize patient
  render locals: { patient: patient }
end

#patientObject



68
69
70
71
72
73
74
# File 'app/controllers/renalware/patients/patients_controller.rb', line 68

def patient
  @patient ||= begin
    Renalware::Patient.find_by(secure_id: params[:id]).tap do |patient_|
      raise PatientNotFoundError unless patient_
    end
  end
end

#searchObject



22
23
24
25
26
27
# File 'app/controllers/renalware/patients/patients_controller.rb', line 22

def search
  skip_authorization
  query = Patients::SearchQuery.new(term: params[:term])
  patients = query.call.page(page).per(per_page)
  render json: simplify(patients).to_json
end

#showObject



63
64
65
66
# File 'app/controllers/renalware/patients/patients_controller.rb', line 63

def show
  authorize patient
  render locals: { patient: patient }
end

#updateObject



53
54
55
56
57
58
59
60
61
# File 'app/controllers/renalware/patients/patients_controller.rb', line 53

def update
  authorize patient
  if patient.update_by(current_user, patient_params)
    redirect_to_patient_demographics(patient)
  else
    flash.now[:error] = t(".failed", model_name: "patient")
    render :edit, locals: { patient: patient }
  end
end