Skip to content

Commit

Permalink
fix: actions download
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Bob committed Jan 22, 2024
1 parent 5ec7f50 commit 20d12d8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 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 @@ -69,7 +69,7 @@ def respond
when :download
# Trigger download, removes modal and flash the messages
render turbo_stream: [
turbo_stream.download(content: @response[:path], filename: @response[:filename]),
turbo_stream.download(content: Base64.encode64(@response[:path]), filename: @response[:filename]),
turbo_stream.close_action_modal,
turbo_stream.flash_alerts
]
Expand Down
10 changes: 9 additions & 1 deletion app/javascript/js/custom-stream-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@ StreamActions.open_filter = function () {
}
// END TODO: move these to the avo_filters gem

// https://stackoverflow.com/a/77850750/9067704
StreamActions.download = function () {
var byteCharacters = atob(this.getAttribute('content'););
var byteNumbers = new Array(byteCharacters.length);
for (var i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);

saveAs(
new Blob(
[this.getAttribute('content')],
[byteArray],
),
this.getAttribute('filename'),
)
Expand Down
5 changes: 5 additions & 0 deletions spec/dummy/app/avo/actions/download_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Avo::Actions::DownloadFile < Avo::BaseAction
# TODO: fix fields for actions
def fields
field :read_from_file, as: :boolean, name: "Read from file", default: false
field :read_from_pdf_file, as: :boolean, name: "Read from PDF file", default: false
end

def handle(**args)
Expand All @@ -15,6 +16,10 @@ def handle(**args)
file = File.open(Avo::Engine.root.join("spec", "dummy", "dummy-file.txt"))

download file.read, "dummy-file.txt"
elsif fields["read_from_pdf_file"]
file = File.open(Avo::Engine.root.join("spec", "dummy", "dummy-file.pdf"))

download file.read, "dummy-file.pdf"
else
download "On the fly dummy content.", "dummy-content.txt"
end
Expand Down
Binary file added spec/dummy/dummy-file.pdf
Binary file not shown.
19 changes: 19 additions & 0 deletions spec/system/avo/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,25 @@
expect(download.split("/").last).to eq file_name
end
end

context "with File.open().read on pdf" do
let(:file_name) { "dummy-file.pdf" }

it "downloads the file and closes the modal" do
visit "/admin/resources/users"

click_on "Actions"
click_on "Download file"
check "fields[read_from_pdf_file]"
click_on "Run"

wait_for_download

expect(downloaded?).to be true
expect(download_content).to eq File.read(Rails.root.join(file_name))
expect(download.split("/").last).to eq file_name
end
end
end

describe "default values" do
Expand Down

0 comments on commit 20d12d8

Please sign in to comment.