Skip to content

Commit

Permalink
Add inline file upload to attachment part (SpinaCMS#1116)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bramjetten authored Oct 7, 2022
1 parent f6f9200 commit 7c61799
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 10 deletions.
7 changes: 6 additions & 1 deletion app/controllers/spina/admin/attachments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ def create
end
end

def inline_upload
@attachment = Attachment.create(attachment_params)
render turbo_stream: turbo_stream.append(params[:select_id], partial: "parts/attachments/attachment", object: @attachment)
end

def update
@attachment = Attachment.find(params[:id])
old_signed_id = @attachment.file&.blob&.signed_id
Expand Down Expand Up @@ -62,7 +67,7 @@ def set_breadcrumbs
end

def attachment_params
params.require(:attachment).permit(:file, :page_id, :_destroy)
params.require(:attachment).permit(:file, :_destroy)
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions app/views/spina/admin/parts/attachments/_attachment.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<option value="<%= attachment.id %>" signed-blob-id="<%= attachment.file&.blob&.signed_id %>" filename="<%= attachment.file&.filename %>" selected>
<%= attachment.file&.filename %>
</option>
23 changes: 22 additions & 1 deletion app/views/spina/admin/parts/attachments/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,26 @@
<%= f.hidden_field :signed_blob_id, data: {attachment_picker_target: 'signedBlobId'} %>
<%= f.hidden_field :filename, data: {attachment_picker_target: 'filename'} %>

<%= f.select :attachment_id, Spina::Attachment.sorted.map{|attachment| [attachment.file&.filename, attachment.id, data: {signed_blob_id: attachment.file&.blob&.signed_id, filename: attachment.file&.filename}]}, {include_blank: t("spina.attachments.choose_attachment")}, {class: "form-select mt-1", data: {action: "attachment-picker#pick"}} %>
<div class="flex items-center space-x-3 mt-1">
<% select_id = dom_id(f.object, f.object.object_id) %>

<%= f.select :attachment_id, Spina::Attachment.sorted.map{|attachment| [attachment.file&.filename, attachment.id, data: {signed_blob_id: attachment.file&.blob&.signed_id, filename: attachment.file&.filename}]}, {include_blank: t("spina.attachments.choose_attachment")}, {id: select_id, class: "form-select", data: {action: "attachment-picker#pick"}} %>

<div class="text-gray-400 font-medium text-sm">
<%=t 'spina.ui.or' %>
</div>

<%= form_with model: [:admin, Spina::Attachment.new], url: spina.inline_upload_admin_attachments_path, data: {controller: "form loading-button", loading_message: t('spina.media_library.uploading'), action: "turbo:submit-end->loading-button#doneLoading"} do |f| %>
<%= hidden_field_tag :select_id, select_id %>
<% file_field_id = "file_#{f.object.object_id}" %>

<%= f.file_field :file, id: file_field_id, class: 'hidden', data: {action: "loading-button#loading form#requestSubmit"} %>

<button type="button" class="btn btn-default" data-controller="delegate-click" data-action="delegate-click#click" data-loading-button-target="button" data-delegate-click-target="#<%= file_field_id %>">
<%= heroicon("upload", style: :solid, class: "w-4 h-4 -ml-1 mr-1") %>
<%=t 'spina.attachments.upload_one' %>
</button>
<% end %>

</div>
</div>
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ en:
insert: Insert document
insert_multiple: Insert documents
upload: Upload files
upload_one: Upload file
cancel: Cancel
choose_from_library: Choose from library
close: Close
Expand Down Expand Up @@ -348,6 +349,7 @@ en:
move_to: Move to
new_entry: New entry
optional: Optional
or: or
rename: Rename
replace: Replace
save_changes: Save changes
Expand Down
4 changes: 3 additions & 1 deletion config/locales/nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ nl:
delete_confirmation_html: Weet je het zeker? Het kan zijn dat dit document in gebruik is op een pagina.
insert: Document toevoegen
insert_multiple: Documenten toevoegen
upload:
upload: Upload documenten
upload_one: Upload document
cancel: Annuleren
choose_from_library: Kies uit bibliotheek
close: Sluiten
Expand Down Expand Up @@ -319,6 +320,7 @@ nl:
move_to: Verplaats naar
new_entry: Nieuw item
optional: Optioneel
or: of
rename: Hernoemen
replace: Vervangen
save_changes: Wijzigingen opslaan
Expand Down
7 changes: 1 addition & 6 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,7 @@
end

resources :attachments do
collection do
get 'select/:page_part_id' => 'attachments#select', as: :select
post 'insert/:page_part_id' => 'attachments#insert', as: :insert
get 'select_collection/:page_part_id' => 'attachments#select_collection', as: :select_collection
post 'insert_collection/:page_part_id' => 'attachments#insert_collection', as: :insert_collection
end
post :inline_upload, on: :collection
end
resources :rename_files

Expand Down
6 changes: 5 additions & 1 deletion test/dummy/config/initializers/themes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
title: 'Text',
hint: 'Your main content',
part_type: 'Spina::Parts::Text'
}, {
name: 'attachment',
title: "Attachment",
part_type: "Spina::Parts::Attachment"
}]

theme.view_templates = [{
Expand All @@ -19,7 +23,7 @@
title: 'Default',
description: 'A simple page',
usage: 'Use for your content',
parts: ['text']
parts: ['text', 'attachment']
}]

theme.custom_pages = [{
Expand Down

0 comments on commit 7c61799

Please sign in to comment.