Module: DatashiftJourney::ValidateState

Extended by:
ActiveSupport::Concern
Included in:
JourneyPlansController
Defined in:
app/controllers/concerns/datashift_journey/validate_state.rb

Instance Method Summary collapse

Instance Method Details

#back_button_cache_busterObject



9
10
11
12
13
# File 'app/controllers/concerns/datashift_journey/validate_state.rb', line 9

def back_button_cache_buster
  response.headers['Cache-Control'] = 'no-cache, no-store, max-age=0, must-revalidate'
  response.headers['Pragma'] = 'no-cache'
  response.headers['Expires'] = 'Fri, 01 Jan 1990 00:00:00 GMT'
end

#back_button_param_listObject



54
55
56
# File 'app/controllers/concerns/datashift_journey/validate_state.rb', line 54

def back_button_param_list
  @back_button_param_list ||= [:state, :id, :action, :controller]
end

#validate_stateObject

Currently the following Situations need special processing, carried out here.

* REFRESH - users hits refresh & resubmits a form - check state associated with view

It is not expected to redirect or halt processing chain - it is simply to ensure state is manged correctly, for situations outside standard state flow/processing

rubocop:disable Style/GuardClause



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/concerns/datashift_journey/validate_state.rb', line 23

def validate_state
  current_index = journey_plan.current_state_index

  view_state = params[:rendered_state]

  view_state_idx = journey_plan.state_index(view_state) # nil for bad states

  logger.debug 'STATE and Param INFO:'
  logger.debug "  Current State   \t[#{journey_plan.state.inspect}] IDX [#{current_index}]"
  logger.debug "  Rendered State  \t[#{view_state}] IDX [#{view_state_idx}]"
  logger.debug "  Redirect to     \t[#{params[:redirect_to]}]"

  if view_state && view_state_idx && (view_state_idx < current_index)
    logger.info("Probable User refresh, resetting state #{view_state} [IDX (#{view_state_idx})]")
    journey_plan.state = view_state
  end

  if view_state.nil? && params[:state] && back_button_param_list.all? { |p| params.key?(p) }

    transitions = journey_plan.transitions_for

    back_event = transitions.find { |t| t.event == :back }

    if back_event && back_event.from == journey_plan.state && back_event.to == params[:state]
      logger.debug("User CLICKED back Button - resetting state to [#{params[:state]}] from #{journey_plan.state}")
      journey_plan.update!(state: params[:state])
    end

  end
end