Skip to content

Commit

Permalink
feature: action redirect allow_other_host and status arguments & acti…
Browse files Browse the repository at this point in the history
…ons turbo attribute (#1878)

* feature: action redirect allow_other_host and status arguments & actions turbo attribute

* respect turbo even if may_download_file
  • Loading branch information
Paul-Bob authored Aug 1, 2023
1 parent b31dcc5 commit 53f765f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/controllers/avo/actions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def respond(response)
path = instance_eval(&path)
end

redirect_to path
redirect_to path, **{allow_other_host: response[:allow_other_host], status: response[:status]}.compact
elsif response[:type] == :reload
redirect_back fallback_location: resources_path(resource: @resource)
end
Expand Down
9 changes: 6 additions & 3 deletions lib/avo/base_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class BaseAction
class_attribute :standalone, default: false
class_attribute :visible
class_attribute :may_download_file, default: false
class_attribute :turbo

attr_accessor :response
attr_accessor :model
Expand All @@ -35,9 +36,9 @@ class << self
def form_data_attributes
# We can't respond with a file download from Turbo se we disable it on the form
if may_download_file
{turbo: false, remote: false, action_target: :form}
{turbo: turbo || false, remote: false, action_target: :form}
else
{turbo_frame: :_top, action_target: :form}
{turbo: turbo, turbo_frame: :_top, action_target: :form}.compact
end
end

Expand Down Expand Up @@ -196,8 +197,10 @@ def silent
self
end

def redirect_to(path = nil, &block)
def redirect_to(path = nil, allow_other_host: nil, status: nil, &block)
response[:type] = :redirect
response[:allow_other_host] = allow_other_host
response[:status] = status
response[:path] = if block.present?
block
else
Expand Down
3 changes: 3 additions & 0 deletions spec/dummy/app/avo/actions/sub/dummy_action.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Sub::DummyAction < Avo::BaseAction
self.name = "Dummy action"
self.standalone = true
# self.turbo = false
self.visible = -> do
if resource.is_a? UserResource
view == :index
Expand Down Expand Up @@ -42,5 +43,7 @@ def handle(**args)
warn "Warning response ✌️"
inform "Info response ✌️"
error "Error response ✌️"

# redirect_to "https://www.google.com/"
end
end

0 comments on commit 53f765f

Please sign in to comment.