Class: DatashiftJourney::JourneyPlansController

Inherits:
ApplicationController show all
Includes:
ValidateState
Defined in:
app/controllers/datashift_journey/journey_plans_controller.rb

Instance Method Summary collapse

Methods included from ValidateState

#back_button_cache_buster, #back_button_param_list, #validate_state

Instance Method Details

#back_a_stateObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/datashift_journey/journey_plans_controller.rb', line 36

def back_a_state
  respond_to do |format|
    logger.debug("BACK Requested - current [#{journey_plan.state}] - previous [#{journey_plan.previous_state_name}]")

    journey_plan.back!

    if journey_plan.save

      logger.debug("Successfully back a step - state now [#{journey_plan.state}]")

      format.html do
        redirect_to(datashift_journey.journey_plan_state_path(journey_plan.state, journey_plan)) && return
      end

    else
      format.html { render :edit }
    end
  end
end

#createObject



24
25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/datashift_journey/journey_plans_controller.rb', line 24

def create
  # TOFIX - Validation from params is now broken
  # NoMethodError (undefined method `each_with_index' for #<ActionController::Parameters:0x000055eacfa2c370>):
  # result = form.validate(params)
  # logger.debug("VALIDATION FAILED - Form Errors [#{form.errors.inspect}]") unless result
  journey_plan = journey_plan_class.new

  form_fields_to_data_nodes

  move_next(journey_plan)
end

#editObject



56
57
58
59
# File 'app/controllers/datashift_journey/journey_plans_controller.rb', line 56

def edit
  logger.debug "Editing journey_plan [#{journey_plan.inspect}]"
  render locals: { journey_plan: @journey_plan, form: @reform }
end

#form_fields_to_data_nodesObject

UPDATE



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/controllers/datashift_journey/journey_plans_controller.rb', line 85

def form_fields_to_data_nodes
  form_params = params.fetch(@reform.params_key, {})

  data_nodes = form_params["data_nodes"] # =>{"form_field"=>{"0"=>"name", "1"=>"namespace"}, "field_value"=>{"0"=>"dfsdf", "1"=>"ghfghf"}}}

  if data_nodes.present?

    fields = data_nodes["form_field"]
    values = data_nodes["field_value"]

    fields.each do |idx, name|
      ff = Collector::FormField.where(name: name, form_definition: @reform.definition).first
      next unless ff

      # Ensure when user goes back and changes a value we reflect the changed value
      Collector::DataNode.find_or_initialize_by(plan: journey_plan, form_field: ff).tap do |node|
        node.field_value = values[idx]
        node.save
      end

    end
  end
end

#newObject



17
18
19
20
21
22
# File 'app/controllers/datashift_journey/journey_plans_controller.rb', line 17

def new
  # Find and create the form object, backing the current states view
  reform = FormObjectFactory.form_object_for(journey_plan)

  render locals: { journey_plan: journey_plan, form: reform }
end

#updateObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/datashift_journey/journey_plans_controller.rb', line 61

def update

  # result = form.validate(params)
  #
  # redirect_to(form.redirection_url) && return if form.redirect?
  #
  # if result && form.save
  #   logger.debug("SUCCESS - Updated #{journey_plan.inspect}")
  #
  #   move_next(journey_plan)
  # else
  #   logger.debug("FAILED - Form Errors [#{form.errors.inspect}]")
  #
  #   render :edit, locals: { journey_plan: journey_plan, form: form }
  # end

  form_fields_to_data_nodes

  logger.debug("UPDATED Plan [#{journey_plan.inspect}] - Move to Next")
  puts journey_plan.inspect

  move_next(journey_plan) && return
end