From 5d56ea65756dbc0306dd22f54e9f1f55b466dd1f Mon Sep 17 00:00:00 2001 From: Nicolas Florentin Date: Fri, 8 Dec 2023 16:55:21 +0100 Subject: [PATCH] fix organization logo validation (#720) --- app/models/organization.rb | 4 +++- app/views/organizations/_form.html.erb | 2 +- spec/models/organization_spec.rb | 5 +++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/models/organization.rb b/app/models/organization.rb index bc62cc11..cec86710 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -26,7 +26,9 @@ class Organization < ApplicationRecord has_many :petitions, dependent: :delete_all validates :name, presence: true, uniqueness: true - validates :logo, content_type: /\Aimage\/.*\z/ + + LOGO_CONTENT_TYPES = %w(image/jpeg image/png image/gif) + validates :logo, content_type: LOGO_CONTENT_TYPES before_validation :ensure_url after_create :create_account diff --git a/app/views/organizations/_form.html.erb b/app/views/organizations/_form.html.erb index c0a3de0f..4405fa83 100644 --- a/app/views/organizations/_form.html.erb +++ b/app/views/organizations/_form.html.erb @@ -9,6 +9,6 @@ <%= f.input :address %> <%= f.input :neighborhood %> <%= f.input :city %> - <%= f.input :logo, input_html: { accept: "image/*" } %> + <%= f.input :logo, input_html: { accept: Organization::LOGO_CONTENT_TYPES.join(',') } %> <%= f.button :submit %> <% end %> diff --git a/spec/models/organization_spec.rb b/spec/models/organization_spec.rb index 121d5dc1..557c48ea 100644 --- a/spec/models/organization_spec.rb +++ b/spec/models/organization_spec.rb @@ -8,6 +8,11 @@ expect(organization).to be_invalid + temp_file = Tempfile.new('test.svg') + organization.logo.attach(io: File.open(temp_file.path), filename: 'test.svg') + + expect(organization).to be_invalid + temp_file = Tempfile.new('test.png') organization.logo.attach(io: File.open(temp_file.path), filename: 'test.png')