Class: Renalware::HD::SessionsController

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

Instance Method Summary collapse

Methods included from PresenterHelper

#present

Methods inherited from BaseController

#patient

Instance Method Details

#createObject



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

def create
  save_session
end

#destroyObject



59
60
61
62
63
64
65
66
# File 'app/controllers/renalware/hd/sessions_controller.rb', line 59

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



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

def edit
  session = Session.for_patient(patient).find(params[:id])
  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



14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/renalware/hd/sessions_controller.rb', line 14

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

#newObject



34
35
36
37
38
39
40
# File 'app/controllers/renalware/hd/sessions_controller.rb', line 34

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



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

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



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

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



76
77
78
79
80
# File 'app/controllers/renalware/hd/sessions_controller.rb', line 76

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

#showObject



27
28
29
30
31
32
# File 'app/controllers/renalware/hd/sessions_controller.rb', line 27

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



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

def update
  save_session
end