Class: Renalware::HD::Sessions::SaveSession

Inherits:
Object
  • Object
show all
Includes:
Wisper::Publisher
Defined in:
app/models/renalware/hd/sessions/save_session.rb

Overview

A generic service object which saves a (new or existing) HD::Session of any (STI) type.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(patient:, current_user:) ⇒ SaveSession

Returns a new instance of SaveSession.



11
12
13
14
# File 'app/models/renalware/hd/sessions/save_session.rb', line 11

def initialize(patient:, current_user:)
  @patient = patient
  @current_user = current_user
end

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user.



9
10
11
# File 'app/models/renalware/hd/sessions/save_session.rb', line 9

def current_user
  @current_user
end

#paramsObject (readonly)

Returns the value of attribute params.



9
10
11
# File 'app/models/renalware/hd/sessions/save_session.rb', line 9

def params
  @params
end

#patientObject (readonly)

Returns the value of attribute patient.



9
10
11
# File 'app/models/renalware/hd/sessions/save_session.rb', line 9

def patient
  @patient
end

#session_typeObject (readonly)

Returns the value of attribute session_type.



9
10
11
# File 'app/models/renalware/hd/sessions/save_session.rb', line 9

def session_type
  @session_type
end

Instance Method Details

#call(params:, id: nil, signing_off: false) ⇒ Object

Note that in the event of a #save failure due to a validation error, we must revert the session’s :type to the original in case they want to save the session as another type i.e. they tried to save as Closed (sign-off) but next time they want to save as Open (not signed off)

  • so we need make sure the hidden :type form value is not rendered as :closed

This is getting a bit confusing and might need some refactoring, for example by driving the :type to save as using the button on the form (which we already do using the sign-off name of the SignOff button - see signed_off?



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/renalware/hd/sessions/save_session.rb', line 24

def call(params:, id: nil, signing_off: false)
  @params = parse_params(params)
  session = find_or_build_session(id)
  session = update_session_attributes(session, signing_off)

  if session.save
    # Might be cleaner if something listened for this event and created this job there?
    UpdateRollingPatientStatisticsJob.perform_later(patient) unless session.open?
    broadcast(:save_success, session)
  else
    session.type = session_type # See method comment
    broadcast(:save_failure, session)
  end
end