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



58
59
60
61
62
63
64
# File 'app/controllers/concerns/effective/wizard_controller/before_actions.rb', line 58

def assign_current_step
  if respond_to?(:current_user) && resource.respond_to?(:current_user=)
    resource.current_user = current_user
  end

  resource.current_step = step.to_sym
end

#assign_page_titleObject

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



68
69
70
# File 'app/controllers/concerns/effective/wizard_controller/before_actions.rb', line 68

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)



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

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

#authorize_resourceObject

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



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

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

#enforce_can_visit_stepObject

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/concerns/effective/wizard_controller/before_actions.rb', line 40

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

#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