Class: Workflow::TasksController

Inherits:
ApplicationController
  • Object
show all
Includes:
Wizard::Controller
Defined in:
app/controllers/workflow/tasks_controller.rb

Instance Method Summary collapse

Methods included from Wizard::Controller::Concerns::Paths

#next_wizard_path, #previous_wizard_path, #wizard_path

Methods included from Wizard::Controller::Concerns::Steps

#current_step?, #future_step?, #jump_to, #last_step?, #next_step, #next_step?, #past_step?, #previous_step, #previous_step?, #skip_step, #step, #wizard_resource_class_name, #wizard_step_per_state, #wizard_steps

Instance Method Details

#cancelObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/workflow/tasks_controller.rb', line 47

def cancel
  if @task.cancel
    notice = I18n.t(
      "general.notifications.event_successful", 
      event: I18n.t("tasks.general.events.cancel")
    )
    redirect_to(
      tasks_workflow_user_index_path(@task.story), notice: notice
    ) and return
  else
    render 'edit' and return
  end
end

#current_step_is_next_step_after_eventObject



12
13
14
# File 'app/controllers/workflow/tasks_controller.rb', line 12

def current_step_is_next_step_after_event
  false
end

#editObject



43
44
45
# File 'app/controllers/workflow/tasks_controller.rb', line 43

def edit
  @story = @task.story
end

#indexObject



16
17
18
19
20
21
22
# File 'app/controllers/workflow/tasks_controller.rb', line 16

def index
  @story = Story.find(params[:story_id])
  @tasks = @story.tasks
  
  # increase level when there are more sub items for tasks_workflow_user_index_path(@task))
  @twitter_sidenav_level = 4
end

#nextObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/workflow/tasks_controller.rb', line 24

def next
  story = Story.find(params[:story_id])
  task = story.next_task_for_user(current_user)
  product_id = story.product_id.blank? ? 'no-name' : story.product_id
  
  if task.is_a?(String) 
    redirect_to(
      product_workflow_user_index_path(product_id), notice: task
    ) and return
  elsif task
    redirect_to edit_task_workflow_user_index_path(task) and return  
  else
    redirect_to(
      product_workflow_user_index_path(product_id), 
      notice: I18n.t('workflow.user.tasks.next.unavailable')
    ) and return
  end
end

#resourceObject



122
123
124
# File 'app/controllers/workflow/tasks_controller.rb', line 122

def resource
  @task ||= Task.find(params[:id]) if params[:id].present?
end

#skipObject



61
62
63
64
65
66
67
68
69
# File 'app/controllers/workflow/tasks_controller.rb', line 61

def skip
  if @task.cancel
    redirect_to(
      next_task_workflow_user_index_path(@task.story)
    ) and return
  else
    render 'edit' and return
  end
end

#updateObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/controllers/workflow/tasks_controller.rb', line 71

def update
  if params[:event] && params[:event].keys.select{|k| ['cancel', 'skip'].include?(k)}.any?
    send(params[:event].keys.first)
    return
  end
  
  @task.attributes = params[:task]
  method = params[:event] ? params[:event].keys.first : 'save'
  success = false
  
  if (@task.with_result? && @result.valid?) || (!@task.with_result? && @task.valid?)
    success = if params[:next_step] == '1' || (params[:event] && params[:event][:next])
      @task.send(step) 
    elsif (method == 'save' && can?(:update, @task)) || can?(method.to_sym, @task)
      @task.send(method)
    else
      false
    end
  end
  
  render 'edit' and return unless success
      
  @result.save if @task.with_result?
  
  if params[:event] && params[:event][:next]
    redirect_to(
      next_task_workflow_user_index_path(@task.story), 
      notice: I18n.t('general.form.successfully_updated')
    ) and return
  end
  
  notice = if method == 'save'
    I18n.t('general.form.successfully_updated')
  else
    I18n.t(
      "general.notifications.event_successful", 
      event: I18n.t("tasks.general.events.#{method}")
    )
  end
  
  if can? :edit, @task
    redirect_to(
      edit_task_workflow_user_index_path(@task), notice: notice
    )
  else
    redirect_to(
      tasks_workflow_user_index_path(@task.story), notice: notice
    )
  end
end