Class: RailsExecution::TasksController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/rails_execution/tasks_controller.rb

Constant Summary collapse

LIMITED_FILTER_LIST =
100

Instance Method Summary collapse

Methods included from PolicyHelper

#can_close_task?, #can_create_task?, #can_edit_task?, #can_execute_task?, #can_remove_scheduled_job?, #can_schedule_task?, #display_decide?, #display_owner?, #display_reviewers?, #how_to_executable, #how_to_schedulable, #in_solo_mode?, #show_form_sidebar?

Methods included from BaseHelper

#current_owner, #normal_labels

Instance Method Details

#approveObject



150
151
152
153
154
155
156
157
158
159
# File 'app/controllers/rails_execution/tasks_controller.rb', line 150

def approve
  if ::RailsExecution::Services::Approvement.new(current_task, reviewer: current_owner).approve
    ::RailsExecution::Services::CreateScheduledJob.new(current_task).call if can_schedule_task?(current_task)
    ::RailsExecution.configuration.notifier.new(current_task).after_approve(current_owner)
    flash[:notice] = 'Your decision is updated!'
  else
    flash[:alert] = "Your decision isn't updated!"
  end
  redirect_to action: :show
end

#auto_suggestionsObject



190
191
192
193
194
# File 'app/controllers/rails_execution/tasks_controller.rb', line 190

def auto_suggestions
  return render json: [] if RailsExecution.configuration.auto_suggestions.empty?

  render json: RailsExecution::AutoSuggestionsService.new.call
end

#closedObject



120
121
122
123
124
125
# File 'app/controllers/rails_execution/tasks_controller.rb', line 120

def closed
  paging = ::RailsExecution::Services::Paging.new(page: params[:page], per_page: params[:per_page])
  closed_tasks = ::RailsExecution::Task.is_closed.preload(:owner, :labels)
  closed_tasks = ::RailsExecution::Tasks::FilterService.new(closed_tasks, params, current_owner&.id).call
  @tasks = paging.call(closed_tasks)
end

#completedObject



113
114
115
116
117
118
# File 'app/controllers/rails_execution/tasks_controller.rb', line 113

def completed
  paging = ::RailsExecution::Services::Paging.new(page: params[:page], per_page: params[:per_page])
  completed_tasks = ::RailsExecution::Task.is_completed.preload(:owner, :labels)
  completed_tasks = ::RailsExecution::Tasks::FilterService.new(completed_tasks, params, current_owner&.id).call
  @tasks = paging.call(completed_tasks)
end

#createObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/rails_execution/tasks_controller.rb', line 20

def create
  raise(::RailsExecution::AccessDeniedError, 'Create task') unless can_create_task?

  result = ::RailsExecution::Tasks::CreateService.new(params, current_owner).call
  @task = result.task
  if result.error.nil?
    ::RailsExecution::Services::CreateScheduledJob.new(@task).call if in_solo_mode? && can_schedule_task?(@task)
    flash[:notice] = 'Create request successfully!'
    redirect_to action: :index
  else
    render action: :new
  end
end

#destroyObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/rails_execution/tasks_controller.rb', line 56

def destroy
  unless can_close_task?(current_task)
    flash[:alert] = "You can't close this Task right now"
    redirect_to(:back) and return
  end

  if current_task.update(status: :closed)
    ::RailsExecution::Services::RemoveScheduledJob.new(current_task).call if can_remove_scheduled_job?(current_task)
    current_task.activities.create(owner: current_owner, message: 'Closed the task')
    ::RailsExecution.configuration.notifier.new(current_task).after_close
    redirect_to(action: :show) and return
  else
    flash[:alert] = "Has problem when close this Task: #{current_task.errors.full_messages.join(', ')}"
    redirect_to(:back) and return
  end
end

#editObject



73
74
75
76
77
# File 'app/controllers/rails_execution/tasks_controller.rb', line 73

def edit
  raise(::RailsExecution::AccessDeniedError, 'Edit task') unless can_edit_task?(current_task)

  @task = current_task
end

#executeObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'app/controllers/rails_execution/tasks_controller.rb', line 161

def execute
  unless can_execute_task?(current_task)
    flash[:alert] = "This task can't execute: #{how_to_executable(current_task)}"
    redirect_to(action: :show) and return
  end

  execute_service = ::RailsExecution::Services::Execution.new(current_task)
  if execute_service.call
    current_task.update(status: :completed) unless current_task.repeatable?
    current_task.activities.create(owner: current_owner, message: 'Execute: The task is completed')
    ::RailsExecution.configuration.notifier.new(current_task).after_execute_success(current_owner)
    flash[:notice] = 'This task is executed'
  else
    ::RailsExecution.configuration.notifier.new(current_task).after_execute_fail(current_owner)
    flash[:alert] = "Sorry!!! This task can't execute right now"
  end

  redirect_to(action: :show)
end

#execute_in_backgroundObject



181
182
183
184
185
186
187
188
# File 'app/controllers/rails_execution/tasks_controller.rb', line 181

def execute_in_background
  unless can_execute_task?(current_task)
    flash[:alert] = "This task can't execute: #{how_to_executable(current_task)}"
    redirect_to(action: :show) and return
  end
  RailsExecution::Services::BackgroundExecution.new(current_task, current_owner).setup
  redirect_to(action: :show)
end

#forkObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/rails_execution/tasks_controller.rb', line 34

def fork
  raise(::RailsExecution::AccessDeniedError, 'Fork task') unless can_create_task?

  @task = ::RailsExecution::Task.new({
    status: :created,
    owner_id: current_owner&.id,
    owner_type: ::RailsExecution.configuration.owner_model.to_s,
    title: current_task.title,
    scheduled_at: current_task.scheduled_at,
    repeat_mode: current_task.repeat_mode,
    description: current_task.description,
    script: current_task.script,
  })
  @task.labels = current_task.labels
  @task.syntax_status = ::RailsExecution::Services::SyntaxChecker.new(@task.script).call ? 'good' : 'bad'

  render action: :new
end

#indexObject



7
8
9
10
11
12
# File 'app/controllers/rails_execution/tasks_controller.rb', line 7

def index
  paging = ::RailsExecution::Services::Paging.new(page: params[:page], per_page: params[:per_page])
  processing_tasks = ::RailsExecution::Task.processing.preload(:owner, :labels)
  processing_tasks = ::RailsExecution::Tasks::FilterService.new(processing_tasks, params, current_owner&.id).call
  @tasks = paging.call(processing_tasks)
end

#newObject



14
15
16
17
18
# File 'app/controllers/rails_execution/tasks_controller.rb', line 14

def new
  raise(::RailsExecution::AccessDeniedError, 'Create task') unless can_create_task?

  @task = ::RailsExecution::Task.new
end

#rejectObject



139
140
141
142
143
144
145
146
147
148
# File 'app/controllers/rails_execution/tasks_controller.rb', line 139

def reject
  if ::RailsExecution::Services::Approvement.new(current_task, reviewer: current_owner).reject
    ::RailsExecution::Services::RemoveScheduledJob.new(current_task).call if can_remove_scheduled_job?(current_task)
    ::RailsExecution.configuration.notifier.new(current_task).after_reject(current_owner)
    flash[:notice] = 'Your decision is updated!'
  else
    flash[:alert] = "Your decision is can't update!"
  end
  redirect_to action: :show
end

#reopenObject



127
128
129
130
131
132
133
134
135
136
137
# File 'app/controllers/rails_execution/tasks_controller.rb', line 127

def reopen
  if current_task.update(status: :created)
    ::RailsExecution::Services::CreateScheduledJob.new(current_task).call if in_solo_mode? && can_schedule_task?(current_task)
    current_task.activities.create(owner: current_owner, message: 'Re-opened the Task')
    ::RailsExecution.configuration.notifier.new(current_task).after_reopen
    flash[:notice] = 'Your task is re-opened'
  else
    flash[:alert] = "Re-open is failed: #{current_task.errors.full_messages.join(', ')}"
  end
  redirect_to action: :show
end

#showObject



53
54
# File 'app/controllers/rails_execution/tasks_controller.rb', line 53

def show
end

#updateObject



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
# File 'app/controllers/rails_execution/tasks_controller.rb', line 79

def update
  raise(::RailsExecution::AccessDeniedError, 'Edit task') unless can_edit_task?(current_task)

  @task = current_task

  old_script = @task.script
  old_reviewer_ids = @task.task_reviews.pluck(:owner_id)
  checked_owner_ids = @task.task_reviews.checked.pluck(:owner_id)
  old_scheduled_at = @task.scheduled_at

  update_data = {
    title: params.dig(:task, :title),
    description: params.dig(:task, :description),
    scheduled_at: Time.zone.parse(params.dig(:task, :scheduled_at).to_s),
    repeat_mode: params.dig(:task, :repeat_mode),
  }

  update_data[:script] = params.dig(:task, :script) if @task.in_processing?
  @task.assign_reviewers(params.dig(:task, :task_review_ids).to_a)
  @task.assign_labels(params[:task_label_ids].to_a) if params.key?(:task_label_ids)
  @task.syntax_status = ::RailsExecution::Services::SyntaxChecker.new(update_data[:script]).call ? 'good' : 'bad'

  if @task.update(update_data)
    ::RailsExecution::Services::RemoveScheduledJob.new(@task).call if (old_script != @task.script || @task.scheduled_at != old_scheduled_at) && can_remove_scheduled_job?(@task)
    @task.add_files(params[:attachments]&.permit!.to_h, current_owner) if ::RailsExecution.configuration.file_upload
    @task.activities.create(owner: current_owner, message: 'Updated the Task')
    ::RailsExecution.configuration.notifier.new(@task).after_update_script(current_owner, checked_owner_ids) if old_script != @task.script
    ::RailsExecution.configuration.notifier.new(@task).after_assign_reviewers(current_owner, @task.task_reviews.reload.pluck(:owner_id) - old_reviewer_ids)
    redirect_to action: :show
  else
    render action: :edit
  end
end