Skip to content

Commit

Permalink
Lazily evaluate default fallback locations in calls to redirect_to_re…
Browse files Browse the repository at this point in the history
…turn_location
  • Loading branch information
spohlenz committed May 6, 2024
1 parent a9eac1e commit 25d8a3d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
10 changes: 5 additions & 5 deletions app/controllers/concerns/trestle/resource/controller/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down Expand Up @@ -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 }

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 25d8a3d

Please sign in to comment.