diff --git a/app/controllers/concerns/trestle/resource/controller/actions.rb b/app/controllers/concerns/trestle/resource/controller/actions.rb index bd2974fb..7d4452e3 100644 --- a/app/controllers/concerns/trestle/resource/controller/actions.rb +++ b/app/controllers/concerns/trestle/resource/controller/actions.rb @@ -25,7 +25,7 @@ def create respond_to do |format| format.html do flash[:message] = flash_message("create.success", title: "Success!", message: "The %{lowercase_model_name} was successfully created.") - redirect_to_return_location(:create, instance, default: admin.instance_path(instance)) + redirect_to_return_location(:create, instance) { admin.instance_path(instance) } end format.json { render json: instance, status: :created, location: admin.instance_path(instance) } @@ -85,7 +85,7 @@ def update respond_to do |format| format.html do flash[:message] = flash_message("update.success", title: "Success!", message: "The %{lowercase_model_name} was successfully updated.") - redirect_to_return_location(:update, instance, default: admin.instance_path(instance)) + redirect_to_return_location(:update, instance) { admin.instance_path(instance) } end format.json { render json: instance, status: :ok } @@ -111,14 +111,14 @@ def destroy format.html do if success flash[:message] = flash_message("destroy.success", title: "Success!", message: "The %{lowercase_model_name} was successfully deleted.") - redirect_to_return_location(:destroy, instance, default: admin.path(:index)) + redirect_to_return_location(:destroy, instance) { admin.path(:index) } else flash[:error] = flash_message("destroy.failure", title: "Warning!", message: "Could not delete %{lowercase_model_name}.") if load_instance - redirect_to_return_location(:update, instance, default: admin.instance_path(instance)) + redirect_to_return_location(:update, instance) { admin.instance_path(instance) } else - redirect_to_return_location(:destroy, instance, default: admin.path(:index)) + redirect_to_return_location(:destroy, instance) { admin.path(:index) } end end end diff --git a/app/controllers/concerns/trestle/resource/controller/redirection.rb b/app/controllers/concerns/trestle/resource/controller/redirection.rb index a7bf4a0c..ba16ad73 100644 --- a/app/controllers/concerns/trestle/resource/controller/redirection.rb +++ b/app/controllers/concerns/trestle/resource/controller/redirection.rb @@ -3,18 +3,20 @@ class Resource module Controller module Redirection protected - def redirect_to_return_location(action, instance, default:) + def redirect_to_return_location(action, instance, default: nil, &block) + fallback_location = block_given? ? block : default + if admin.return_locations[action] && !dialog_request? location = instance_exec(instance, &admin.return_locations[action]) case location when :back - redirect_back fallback_location: default, turbolinks: false + redirect_back fallback_location: fallback_location, turbolinks: false else redirect_to location, turbolinks: false end else - redirect_to default, turbolinks: false + redirect_to fallback_location, turbolinks: false end end end