Skip to content

Commit

Permalink
Fix File attachments in proposals
Browse files Browse the repository at this point in the history
* Fix File attachments in proposals

* Fixing Failing specs
  • Loading branch information
alecslupu committed May 8, 2023
1 parent da1b65d commit 1c6e8dc
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 4 deletions.
22 changes: 20 additions & 2 deletions decidim-core/app/commands/decidim/attachment_methods.rb
Expand Up @@ -12,8 +12,8 @@ def build_attachment(attached_to = nil)
@attachment = Attachment.new(
title: { I18n.locale => @form.attachment.title },
attached_to: attached_to,
file: @form.attachment.file, # Define attached_to before this
content_type: @form.attachment.file.content_type
file: signed_id_for(@form.attachment.file),
content_type: content_type_for(@form.attachment.file)
)
end

Expand Down Expand Up @@ -53,5 +53,23 @@ def process_attachments?
def delete_attachment?
@form.attachment&.delete_file.present?
end

protected

def signed_id_for(attachment)
return attachment[:file] if attachment.is_a?(Hash)

attachment
end

def content_type_for(attachment)
return attachment.content_type if attachment.instance_of?(ActionDispatch::Http::UploadedFile)

blob(signed_id_for(attachment)).content_type
end

def blob(signed_id)
ActiveStorage::Blob.find_signed(signed_id)
end
end
end
Expand Up @@ -197,10 +197,17 @@ module Admin

context "when attachments are allowed" do
let(:component) { create(:proposal_component, :with_attachments_allowed) }
let(:blob) do
ActiveStorage::Blob.create_and_upload!(
io: File.open(Decidim::Dev.test_file("city.jpeg", "image/jpeg"), "rb"),
filename: "city.jpeg",
content_type: "image/jpeg" # Or figure it out from `name` if you have non-JPEGs
)
end
let(:attachment_params) do
{
title: "My attachment",
file: Decidim::Dev.test_file("city.jpeg", "image/jpeg")
file: blob.signed_id
}
end

Expand Down
Expand Up @@ -112,10 +112,19 @@

context "when attachments are allowed" do
let(:component) { create(:proposal_component, :with_attachments_allowed) }

let(:blob) do
ActiveStorage::Blob.create_and_upload!(
io: File.open(Decidim::Dev.test_file("city.jpeg", "image/jpeg"), "rb"),
filename: "city.jpeg",
content_type: "image/jpeg" # Or figure it out from `name` if you have non-JPEGs
)
end

let(:attachment_params) do
{
title: "My attachment",
file: Decidim::Dev.test_file("city.jpeg", "image/jpeg")
file: blob.signed_id
}
end

Expand Down
45 changes: 45 additions & 0 deletions decidim-proposals/spec/system/admin/admin_creates_proposal_spec.rb
@@ -0,0 +1,45 @@
# frozen_string_literal: true

require "spec_helper"

describe "Admin creates proposals", type: :system do
let(:manifest_name) { "proposals" }
let(:creation_enabled?) { true }
let!(:component) do
create(:proposal_component,
:with_creation_enabled,
:with_attachments_allowed,
manifest:,
participatory_space: participatory_process)
end
let(:new_title) { "This is my proposal new title" }
let(:new_body) { "This is my proposal new body" }

include_context "when managing a component as an admin"

before do
component.update!(
settings: { official_proposals_enabled: true, attachments_allowed: true, creation_enabled: true },
step_settings: {
component.participatory_space.active_step.id => {
creation_enabled: creation_enabled?
}
}
)
end

it "can attach a file" do
visit_component_admin
click_link("New proposal")

fill_in_i18n :proposal_title, "#proposal-title-tabs", en: new_title
fill_in_i18n_editor :proposal_body, "#proposal-body-tabs", en: new_body
fill_in :proposal_attachment_title, with: "FOO BAR"
dynamically_attach_file(:proposal_attachment_file, Decidim::Dev.asset("city.jpeg"))
click_button("Create")
find("a.action-icon--edit-proposal").click

expect(page).to have_content("city.jpeg")
expect(page).to have_content("FOO BAR")
end
end
18 changes: 18 additions & 0 deletions decidim-proposals/spec/system/admin/admin_edits_proposal_spec.rb
Expand Up @@ -90,6 +90,24 @@
find("a.action-icon--edit-proposal").click
expect(page).to have_no_content("Current file")
end

it "can attach a file" do
visit_component_admin
find("a.action-icon--edit-proposal").click
fill_in :proposal_attachment_title, with: "FOO BAR"

find("input#proposal_attachment_delete_file").set(true)
click_button("Replace")
click_button("× Remove")
click_button("Save")
dynamically_attach_file(:proposal_attachment_file, Decidim::Dev.asset("city.jpeg"))

click_button("Update")
find("a.action-icon--edit-proposal").click

expect(page).to have_content("city.jpeg")
expect(page).to have_content("FOO BAR")
end
end
end

Expand Down

0 comments on commit 1c6e8dc

Please sign in to comment.