Skip to content

Commit

Permalink
Fix image attachments through the form
Browse files Browse the repository at this point in the history
  • Loading branch information
ahukkanen committed Mar 11, 2024
1 parent 2ce11c2 commit d508fb6
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,33 @@ module ResultFormExtensions

validates :main_image, passthru: {
to: Decidim::Accountability::Result,
with: { component: ->(form) { form.current_component } }
with: {
# When the image validations are done through the validation
# endpoint, the component is unknown and would cause the
# validations to fail because the component would not exist.
component: lambda do |form|
Decidim::Component.new(
participatory_space: Decidim::ParticipatoryProcess.new(
organization: form.current_organization
)
)
end
}
}
validates :list_image, passthru: {
to: Decidim::Accountability::Result,
with: { component: ->(form) { form.current_component } }
with: {
# When the image validations are done through the validation
# endpoint, the component is unknown and would cause the
# validations to fail because the component would not exist.
component: lambda do |form|
Decidim::Component.new(
participatory_space: Decidim::ParticipatoryProcess.new(
organization: form.current_organization
)
)
end
}
}

validates_locations_for Decidim::Accountability::Result
Expand Down
49 changes: 49 additions & 0 deletions spec/requests/admin_validates_result_images_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

require "spec_helper"

describe "Admin validates result images" do # rubocop:disable RSpec/DescribeClass
include Decidim::ComponentPathHelper

let(:user) { create(:user, :confirmed, :admin) }

let(:headers) { { "HOST" => user.organization.host } }

before do
login_as user, scope: :user
end

describe "POST create" do
let(:request_path) { Decidim::Core::Engine.routes.url_helpers.upload_validations_path }

let(:blob) do
ActiveStorage::Blob.create_and_upload!(
io: File.open(Decidim::Dev.asset("city.jpeg")),
filename: "city.jpeg",
content_type: "image/jpeg"
)
end

%w(main_image list_image).each do |property|
context "with #{property}" do
let(:params) do
{
resource_class: "Decidim::Accountability::Result",
property: property,
blob: blob.signed_id,
form_class: "Decidim::Accountability::Admin::ResultForm"
}
end

it "validates the image" do
post(request_path, params: params, headers: headers)

expect(response).to have_http_status(:ok)

messages = JSON.parse(response.body)
expect(messages).to be_empty
end
end
end
end
end

0 comments on commit d508fb6

Please sign in to comment.