Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
dropzone: '#assign_file_list',
file_attribute: 'file',
in_dialog: true,
accept: Array(search_filter_params[:only]).map { |extension|
Marcel::MimeType.for(extension:)
}.join(",").presence,
redirect_url: admin_attachments_path(
form_field_id: @form_field_id
) %>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<% file_upload_id = "file_upload_#{dom_id(object)}" %>
<% file_types = Alchemy.config.uploader.allowed_filetypes[object.class.model_name.collection] || ['*'] %>
<% accept ||= file_types.to_a == ["*"] ? nil : file_types.map {|type| ".#{type}"}.join(", ") %>

<alchemy-uploader id="<%= file_upload_id %>">
<%= form_for [:admin, object], html: {multipart: true, class: 'upload-button'} do |f| %>
<%= f.file_field file_attribute,
class: 'fileupload--field',
class: 'fileupload--field', accept: accept,
name: "#{f.object_name}[#{file_attribute}]",
id: "replace_#{dom_id(object)}" %>
<%= label_tag "replace_#{dom_id(object)}", class: "icon_button" do %>
Expand All @@ -12,7 +14,7 @@
<% end %>
</alchemy-uploader>

<script type='text/javascript'>
<script type="text/javascript">
document.getElementById("<%= file_upload_id %>").addEventListener("Alchemy.upload.successful", (event) => {
Turbo.visit('<%= redirect_url.html_safe %>');
})
Expand Down
2 changes: 1 addition & 1 deletion app/views/alchemy/admin/uploader/_button.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% file_types = Alchemy.config.uploader.allowed_filetypes[object.class.model_name.collection] || ['*'] %>
<% accept = file_types.to_a == ["*"] ? nil : file_types.map {|type| ".#{type}"}.join(", ") %>
<% accept ||= file_types.to_a == ["*"] ? nil : file_types.map {|type| ".#{type}"}.join(", ") %>

<alchemy-uploader dropzone="<%= local_assigns[:dropzone] || "#main_content" %>">
<%= form_for [:admin, object], html: { multipart: true, class: 'upload-button' } do |f| %>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

require "rails_helper"

describe "alchemy/admin/attachments/_replace_button.html.erb" do
let(:object) { Alchemy::Attachment.new(id: 666) }
let(:file_attribute) { :file }
let(:redirect_url) { "/admin/attachments" }

before do
allow(view).to receive(:admin_attachments_path).and_return("/admin/attachments")
view.extend Alchemy::BaseHelper
end

it "renders a alchemy-uploader component" do
render partial: "alchemy/admin/attachments/replace_button",
locals: {object: object, file_attribute: file_attribute, redirect_url: redirect_url}
expect(rendered).to have_selector("alchemy-uploader#file_upload_attachment_666")
end

context "with allowed_filetypes configured as wildcard" do
before do
allow(Alchemy.config.uploader.allowed_filetypes).to receive(:alchemy_attachments) do
["*"]
end
end

it "does not render the accept attribute" do
render partial: "alchemy/admin/attachments/replace_button",
locals: {object: object, file_attribute: file_attribute, redirect_url: redirect_url}

expect(rendered).not_to have_selector('input[type="file"][accept]')
end
end

context "with allowed_filetypes configured as specific file types" do
before do
allow(Alchemy.config.uploader.allowed_filetypes).to receive(:alchemy_attachments) do
["pdf", "doc", "docx"]
end
end

it "renders the accept attribute with the correct file extensions" do
render partial: "alchemy/admin/attachments/replace_button",
locals: {object: object, file_attribute: file_attribute, redirect_url: redirect_url}

expect(rendered).to have_selector('input[type="file"][accept=".pdf, .doc, .docx"]')
end
end
end