Module: Effective::WizardController::BeforeActions

Included in:
Effective::WizardController
Defined in:
app/controllers/concerns/effective/wizard_controller/before_actions.rb

Instance Method Summary collapse

Instance Method Details

#assign_current_stepObject

before_action :assign_current_step, only: [:show, :update] Assign the current step to resource



80
81
82
# File 'app/controllers/concerns/effective/wizard_controller/before_actions.rb', line 80

def assign_current_step
  resource.current_step = step.to_sym
end

#assign_page_titleObject

before_action :assign_page_title, only: [:show, :update] Assign page title



86
87
88
# File 'app/controllers/concerns/effective/wizard_controller/before_actions.rb', line 86

def assign_page_title
  @page_title ||= resource_wizard_step_title(resource, step)
end

#assign_required_stepsObject

before_action :assign_required_steps, only: [:show, :update] Assign the required steps to Wickeds (dynamic steps)



37
38
39
# File 'app/controllers/concerns/effective/wizard_controller/before_actions.rb', line 37

def assign_required_steps
  self.steps = resource.required_steps
end

#assign_resourceObject

before_action :assign_resource, only: [:show, :update] Assigns the resource



20
21
22
# File 'app/controllers/concerns/effective/wizard_controller/before_actions.rb', line 20

def assign_resource
  self.resource ||= find_wizard_resource
end

#assign_resource_current_userObject



24
25
26
27
# File 'app/controllers/concerns/effective/wizard_controller/before_actions.rb', line 24

def assign_resource_current_user
  return unless respond_to?(:current_user)
  resource.current_user ||= current_user
end

#authorize_resourceObject

before_action :authorize_resource, only: [:show, :update] Authorize the resource



31
32
33
# File 'app/controllers/concerns/effective/wizard_controller/before_actions.rb', line 31

def authorize_resource
  EffectiveResources.authorize!(self, action_name.to_sym, resource)
end

#clear_flash_successObject



97
98
99
# File 'app/controllers/concerns/effective/wizard_controller/before_actions.rb', line 97

def clear_flash_success
  @_delete_flash_success = true
end

#enforce_can_visit_stepObject

before_action :enforce_can_visit_step, only: [:show, :update] Make sure I have permission for this step



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/concerns/effective/wizard_controller/before_actions.rb', line 62

def enforce_can_visit_step
  return if step == 'wicked_finish'
  return if resource.can_visit_step?(step)

  next_step = wizard_steps.reverse.find { |step| resource.can_visit_step?(step) }
  raise('There is no wizard step to visit. Make sure can_visit_step?(step) returns true for at least one step') unless next_step

  if Rails.env.development?
    Rails.logger.info "  \e[31m\e[1mFAILED\e[0m\e[22m" # bold red
    Rails.logger.info "  Unable to visit step :#{step}. Last can_visit_step? is :#{next_step}. Change the acts_as_wizard model's can_visit_step?(step) function to change this."
  end

  flash[:success] = "You have been redirected to the #{resource_wizard_step_title(resource, next_step)} step."
  redirect_to wizard_path(next_step)
end

#ready_checkoutObject



90
91
92
93
94
95
# File 'app/controllers/concerns/effective/wizard_controller/before_actions.rb', line 90

def ready_checkout
  return unless step == :checkout
  return unless resource.class.try(:acts_as_purchasable_wizard?)

  resource.ready!
end

#redirect_if_blank_stepObject

before_action :redirect_if_blank_step, only: [:show] When I visit /resources/1, redirect to /resources/1/build/step



7
8
9
10
11
12
13
14
15
16
# File 'app/controllers/concerns/effective/wizard_controller/before_actions.rb', line 7

def redirect_if_blank_step
  if params[:id].present? && params[resource_name_id].blank?
    params[resource_name_id] = params[:id]

    assign_resource()

    step = (resource.first_uncompleted_step || resource_wizard_steps.last)
    redirect_to resource_wizard_path(resource, step)
  end
end

#redirect_if_existingObject

Allow only 1 in-progress wizard at a time



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/concerns/effective/wizard_controller/before_actions.rb', line 44

def redirect_if_existing
  return if step == 'wicked_finish'
  return if resource.blank?
  return if resource.try(:done?)
  return unless resource_scope.respond_to?(:in_progress_for) || resource_scope.respond_to?(:in_progress)

  in_progress = resource_scope.try(:in_progress_for, current_user) || resource_scope.try(:in_progress)
  existing = in_progress.order(:id).where.not(id: resource).first

  return unless existing.present?
  return if resource.persisted? && (existing.id > resource.id) # Otherwise we get an infinite loop

  flash[:success] = "You have been redirected to your in-progress wizard"
  redirect_to resource_wizard_path(existing, existing.next_step)
end