2021-04-18 14:34:55 +03:00
# frozen_string_literal: true
2018-02-17 13:34:26 +03:00
class AdditionalsChangeStatusController < ApplicationController
before_action :find_issue
helper :additionals_issues
def update
2018-08-22 12:38:29 +03:00
issue_old_status_id = @issue . status . id
2018-02-19 11:11:25 +03:00
issue_old_user = @issue . assigned_to
2018-02-17 13:34:26 +03:00
new_status_id = params [ :new_status_id ] . to_i
2021-04-18 14:34:55 +03:00
allowed_status = @issue . sidbar_change_status_allowed_to User . current , new_status_id
2018-02-17 13:34:26 +03:00
if new_status_id < 1 || @issue . status_id == new_status_id || allowed_status . nil?
redirect_to ( issue_path ( @issue ) )
return
end
2021-01-10 20:36:47 +03:00
@issue . init_journal User . current
2018-02-17 13:34:26 +03:00
@issue . status_id = new_status_id
2018-02-19 12:09:36 +03:00
@issue . assigned_to = User . current if @issue . status_x_affected? ( new_status_id ) && issue_old_user != User . current
2018-02-19 11:11:25 +03:00
2022-03-12 16:58:45 +03:00
call_hook :controller_additionals_change_status_before_save ,
2024-08-27 10:30:58 +03:00
params : ,
2022-03-12 16:58:45 +03:00
issue : @issue ,
journal : @issue . current_journal
2018-08-22 12:38:29 +03:00
if ! @issue . save || issue_old_status_id == @issue . status_id
2022-09-16 16:13:25 +03:00
messages = @issue . errors . full_messages
2023-10-18 11:40:49 +03:00
flash [ :error ] = messages . present? ? messages . to_comma_list : l ( :error_issue_status_could_not_changed )
2018-02-19 11:11:25 +03:00
return redirect_to ( issue_path ( @issue ) )
end
2018-02-17 13:34:26 +03:00
2022-03-12 16:58:45 +03:00
call_hook :controller_additionals_change_status_after_save ,
2024-08-27 10:30:58 +03:00
params : ,
2022-03-12 16:58:45 +03:00
issue : @issue ,
journal : @issue . current_journal
2022-03-11 18:43:14 +03:00
2019-02-28 19:20:52 +03:00
last_journal = @issue . journals . visible . order ( :created_on ) . last
2018-02-17 13:34:26 +03:00
2022-01-17 09:26:24 +03:00
return redirect_to ( issue_path ( @issue ) ) unless last_journal
2018-02-17 13:34:26 +03:00
2019-02-28 19:20:52 +03:00
last_journal = @issue . journals . visible . order ( :created_on ) . last
2020-08-08 19:44:40 +03:00
redirect_to " #{ issue_path @issue } # change- #{ last_journal . id } "
2018-02-17 13:34:26 +03:00
end
private
def find_issue
2021-04-18 14:34:55 +03:00
@issue = Issue . find params [ :issue_id ]
2018-02-17 13:34:26 +03:00
raise Unauthorized unless @issue . visible? && @issue . editable?
2018-09-10 18:24:07 +03:00
2018-02-17 13:34:26 +03:00
@project = @issue . project
rescue ActiveRecord :: RecordNotFound
render_404
end
end