Class: Renalware::HD::SessionsController

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

Overview

rubocop:disable Metrics/ClassLength

Instance Method Summary collapse

Methods included from PresenterHelper

#present

Methods inherited from BaseController

#patient

Instance Method Details

#createObject



54
55
56
# File 'app/controllers/renalware/hd/sessions_controller.rb', line 54

def create
  save_session
end

#destroyObject



79
80
81
82
83
84
85
86
# File 'app/controllers/renalware/hd/sessions_controller.rb', line 79

def destroy
  session = Session.for_patient(patient).find(params[:id])
  authorize session
  session.destroy!
  regenerate_rolling_hd_statistics
  message = t(".success", model_name: "HD session")
  redirect_to patient_hd_dashboard_path(patient), notice: message
end

#editObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/renalware/hd/sessions_controller.rb', line 58

def edit
  session = Session.for_patient(patient).find(params[:id])
  session.prescription_administrations.each do |a|
    if a.administrator_authorised?
      a.administrator_authorisation_token = a.administered_by&.auth_token
    end
    if a.witness_authorised?
      a.witness_authorisation_token = a.witnessed_by&.auth_token
    end
  end
  authorize session
  render :edit, locals: locals(session)
rescue Pundit::NotAuthorizedError
  flash[:warning] = t(".session_is_immutable")
  redirect_to patient_hd_session_path(session, patient_id: patient)
end

#indexObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/renalware/hd/sessions_controller.rb', line 15

def index
  query = sessions_query
  sessions = query
    .call
    .eager_load(
      :hospital_unit,
      :patient,
      :signed_on_by,
      :signed_off_by,
      prescription_administrations: [
        {
          prescription: [:medication_route, :drug]
        },
        :administered_by,
        :reason
      ])
    .page(page)
    .per(per_page || 15)
  authorize sessions
  presenter = CollectionPresenter.new(sessions, SessionPresenter, view_context)
  @q = query.search
  render :index, locals: { sessions: presenter }
end

#newObject



46
47
48
49
50
51
52
# File 'app/controllers/renalware/hd/sessions_controller.rb', line 46

def new
  session = SessionFactory.new(patient: patient,
                               user: current_user,
                               type: params[:type]).build
  authorize session
  render :new, locals: locals(session)
end

#save_failure(session) ⇒ Object



102
103
104
105
106
# File 'app/controllers/renalware/hd/sessions_controller.rb', line 102

def save_failure(session)
  flash.now[:error] = t(".failed", model_name: "HD session")
  action = action_name.to_sym == :create ? :new : :edit
  render action, locals: locals(session)
end

#save_sessionObject



88
89
90
91
92
93
94
# File 'app/controllers/renalware/hd/sessions_controller.rb', line 88

def save_session
  command = Sessions::SaveSession.new(patient: patient, current_user: current_user)
  command.subscribe(self)
  command.call(params: session_params,
               id: params[:id],
               signing_off: params[:signoff].present?)
end

#save_success(_session) ⇒ Object



96
97
98
99
100
# File 'app/controllers/renalware/hd/sessions_controller.rb', line 96

def save_success(_session)
  url = patient_hd_dashboard_path(patient)
  message = t(".success", model_name: "HD session")
  redirect_to url, notice: message
end

#showObject



39
40
41
42
43
44
# File 'app/controllers/renalware/hd/sessions_controller.rb', line 39

def show
  session = Session.for_patient(patient).find(params[:id])
  authorize session
  presenter = SessionPresenter.new(session, view_context)
  render :show, locals: { session: presenter, patient: patient }
end

#updateObject



75
76
77
# File 'app/controllers/renalware/hd/sessions_controller.rb', line 75

def update
  save_session
end