Skip to content

Commit 15cbc41

Browse files
authored
Merge pull request #3527 from AlchemyCMS/backport/8.0-stable/pr-3526
[8.0-stable] Fix overlay uploader
2 parents f1f630a + 2ecec92 commit 15cbc41

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

app/views/alchemy/admin/attachments/_archive_overlay.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
dropzone: '#assign_file_list',
88
file_attribute: 'file',
99
in_dialog: true,
10+
accept: Array(search_filter_params[:only]).map { |extension|
11+
Marcel::MimeType.for(extension:)
12+
}.join(",").presence,
1013
redirect_url: admin_attachments_path(
1114
form_field_id: @form_field_id
1215
) %>

app/views/alchemy/admin/attachments/_replace_button.html.erb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<% file_upload_id = "file_upload_#{dom_id(object)}" %>
2+
<% file_types = Alchemy.config.uploader.allowed_filetypes[object.class.model_name.collection] || ['*'] %>
3+
<% accept ||= file_types.to_a == ["*"] ? nil : file_types.map {|type| ".#{type}"}.join(", ") %>
24

35
<alchemy-uploader id="<%= file_upload_id %>">
46
<%= form_for [:admin, object], html: {multipart: true, class: 'upload-button'} do |f| %>
57
<%= f.file_field file_attribute,
6-
class: 'fileupload--field',
8+
class: 'fileupload--field', accept: accept,
79
name: "#{f.object_name}[#{file_attribute}]",
810
id: "replace_#{dom_id(object)}" %>
911
<%= label_tag "replace_#{dom_id(object)}", class: "icon_button" do %>
@@ -12,7 +14,7 @@
1214
<% end %>
1315
</alchemy-uploader>
1416

15-
<script type='text/javascript'>
17+
<script type="text/javascript">
1618
document.getElementById("<%= file_upload_id %>").addEventListener("Alchemy.upload.successful", (event) => {
1719
Turbo.visit('<%= redirect_url.html_safe %>');
1820
})

app/views/alchemy/admin/uploader/_button.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<% file_types = Alchemy.config.uploader.allowed_filetypes[object.class.model_name.collection] || ['*'] %>
2-
<% accept = file_types.to_a == ["*"] ? nil : file_types.map {|type| ".#{type}"}.join(", ") %>
2+
<% accept ||= file_types.to_a == ["*"] ? nil : file_types.map {|type| ".#{type}"}.join(", ") %>
33

44
<alchemy-uploader dropzone="<%= local_assigns[:dropzone] || "#main_content" %>">
55
<%= form_for [:admin, object], html: { multipart: true, class: 'upload-button' } do |f| %>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# frozen_string_literal: true
2+
3+
require "rails_helper"
4+
5+
describe "alchemy/admin/attachments/_replace_button.html.erb" do
6+
let(:object) { Alchemy::Attachment.new(id: 666) }
7+
let(:file_attribute) { :file }
8+
let(:redirect_url) { "/admin/attachments" }
9+
10+
before do
11+
allow(view).to receive(:admin_attachments_path).and_return("/admin/attachments")
12+
view.extend Alchemy::BaseHelper
13+
end
14+
15+
it "renders a alchemy-uploader component" do
16+
render partial: "alchemy/admin/attachments/replace_button",
17+
locals: {object: object, file_attribute: file_attribute, redirect_url: redirect_url}
18+
expect(rendered).to have_selector("alchemy-uploader#file_upload_attachment_666")
19+
end
20+
21+
context "with allowed_filetypes configured as wildcard" do
22+
before do
23+
allow(Alchemy.config.uploader.allowed_filetypes).to receive(:alchemy_attachments) do
24+
["*"]
25+
end
26+
end
27+
28+
it "does not render the accept attribute" do
29+
render partial: "alchemy/admin/attachments/replace_button",
30+
locals: {object: object, file_attribute: file_attribute, redirect_url: redirect_url}
31+
32+
expect(rendered).not_to have_selector('input[type="file"][accept]')
33+
end
34+
end
35+
36+
context "with allowed_filetypes configured as specific file types" do
37+
before do
38+
allow(Alchemy.config.uploader.allowed_filetypes).to receive(:alchemy_attachments) do
39+
["pdf", "doc", "docx"]
40+
end
41+
end
42+
43+
it "renders the accept attribute with the correct file extensions" do
44+
render partial: "alchemy/admin/attachments/replace_button",
45+
locals: {object: object, file_attribute: file_attribute, redirect_url: redirect_url}
46+
47+
expect(rendered).to have_selector('input[type="file"][accept=".pdf, .doc, .docx"]')
48+
end
49+
end
50+
end

0 commit comments

Comments
 (0)