Skip to content

Commit

Permalink
Moving AASM capture from wizard controller to application controller (#…
Browse files Browse the repository at this point in the history
…1738)

* Moving AASM capture from wizard controller to application controller

* Moving aasm handeling to application controller
so wizard and work controller can take advantage of duplicate logic
Also cleans up work controller for rubocop violations
  • Loading branch information
carolyncole authored Apr 4, 2024
1 parent 2ba9129 commit 1e49314
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 43 deletions.
12 changes: 12 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,16 @@ def prepare_decorators_for_work_form(work)
@work_decorator = WorkDecorator.new(work, current_user)
@form_resource_decorator = FormResourceDecorator.new(work, current_user)
end

def rescue_aasm_error
yield
rescue AASM::InvalidTransition => error
message = message_from_assm_error(aasm_error: error, work: @work)

Honeybadger.notify("Invalid #{@work.current_transition}: #{error.message} errors: #{message}")
transition_error_message = "We apologize, the following errors were encountered: #{message}. Please contact the PDC Describe administrators for any assistance."
@errors = [transition_error_message]

redirect_aasm_error(transition_error_message)
end
end
57 changes: 24 additions & 33 deletions app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,51 +289,42 @@ def readme_file_param
patch_params[:readme_file]
end

# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/BlockNesting
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/PerceivedComplexity
def rescue_aasm_error
yield
rescue AASM::InvalidTransition => error
message = message_from_assm_error(aasm_error: error, work: @work)
Honeybadger.notify("Invalid #{@work.current_transition}: #{error.message} errors: #{message}")
transition_error_message = "We apologize, the following errors were encountered: #{message}. Please contact the PDC Describe administrators for any assistance."
@errors = [transition_error_message]
super
rescue StandardError => generic_error
if action_name == "create"
handle_error_for_create(generic_error)
else
redirect_to root_url, notice: "We apologize, an error was encountered: #{generic_error.message}. Please contact the PDC Describe administrators."
end
end

def handle_error_for_create(generic_error)
if @work.persisted?
redirect_to edit_work_url(id: @work.id), notice: transition_error_message, params:
Honeybadger.notify("Failed to create the new Dataset #{@work.id}: #{generic_error.message}")
@form_resource_decorator = FormResourceDecorator.new(@work, current_user)
redirect_to edit_work_url(id: @work.id), notice: "Failed to create the new Dataset #{@work.id}: #{generic_error.message}", params:
else
Honeybadger.notify("Failed to create a new Dataset #{@work.id}: #{generic_error.message}")
new_params = {}
new_params[:wizard] = wizard_mode? if wizard_mode?
new_params[:migrate] = migrating? if migrating?
@form_resource_decorator = FormResourceDecorator.new(@work, current_user)
redirect_to new_work_url(params: new_params), notice: transition_error_message, params: new_params
redirect_to new_work_url(params: new_params), notice: "Failed to create a new Dataset: #{generic_error.message}", params: new_params
end
rescue StandardError => generic_error
if action_name == "create"
if @work.persisted?
Honeybadger.notify("Failed to create the new Dataset #{@work.id}: #{generic_error.message}")
@form_resource_decorator = FormResourceDecorator.new(@work, current_user)
redirect_to edit_work_url(id: @work.id), notice: "Failed to create the new Dataset #{@work.id}: #{generic_error.message}", params:
else
Honeybadger.notify("Failed to create a new Dataset #{@work.id}: #{generic_error.message}")
new_params = {}
new_params[:wizard] = wizard_mode? if wizard_mode?
new_params[:migrate] = migrating? if migrating?
@form_resource_decorator = FormResourceDecorator.new(@work, current_user)
redirect_to new_work_url(params: new_params), notice: "Failed to create a new Dataset: #{generic_error.message}", params: new_params
end
end

def redirect_aasm_error(transition_error_message)
if @work.persisted?
redirect_to edit_work_url(id: @work.id), notice: transition_error_message, params:
else
redirect_to root_url, notice: "We apologize, an error was encountered: #{generic_error.message}. Please contact the PDC Describe administrators."
new_params = {}
new_params[:wizard] = wizard_mode? if wizard_mode?
new_params[:migrate] = migrating? if migrating?
@form_resource_decorator = FormResourceDecorator.new(@work, current_user)
redirect_to new_work_url(params: new_params), notice: transition_error_message, params: new_params
end
end
# rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/BlockNesting
# rubocop:enable Metrics/AbcSize

def error_action
@form_resource_decorator = FormResourceDecorator.new(@work, current_user)
Expand Down
15 changes: 5 additions & 10 deletions app/controllers/works_wizard_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,22 +195,17 @@ def readme_file_param
end

def rescue_aasm_error
yield
rescue AASM::InvalidTransition => error
message = message_from_assm_error(aasm_error: error, work: @work)

Honeybadger.notify("Invalid #{@work.current_transition}: #{error.message} errors: #{message}")
transition_error_message = "We apologize, the following errors were encountered: #{message}. Please contact the PDC Describe administrators for any assistance."
@errors = [transition_error_message]
prepare_decorators_for_work_form(@work)
super
rescue StandardError => generic_error
redirect_to root_url, notice: "We apologize, an error was encountered: #{generic_error.message}. Please contact the PDC Describe administrators."
end

def redirect_aasm_error(transition_error_message)
if @work.persisted?
redirect_to edit_work_wizard_path(id: @work.id), notice: transition_error_message, params:
else
redirect_to work_create_new_submission_path, notice: transition_error_message, params:
end
rescue StandardError => generic_error
redirect_to root_url, notice: "We apologize, an error was encountered: #{generic_error.message}. Please contact the PDC Describe administrators."
end
end
# rubocop:enable Metrics/ClassLength

0 comments on commit 1e49314

Please sign in to comment.