diff --git a/Gemfile b/Gemfile index 28833049f..f801c109d 100644 --- a/Gemfile +++ b/Gemfile @@ -19,6 +19,7 @@ gem "dalli", "~> 2.7.11" gem "delayed_job_active_record", "~> 4.1.6" gem "devise", "~> 4.8.0" gem "devise-security", "~> 0.16.0" +gem "file_validators", "~> 3.0.0" gem "font-awesome-sass", "~> 5.15.1" # Remember to update vendor/assets/images/fontawesome when updating this gem gem "foundation-rails", "~> 6.6.2.0" gem "foundation_rails_helper", "~> 4.0.0" diff --git a/Gemfile.lock b/Gemfile.lock index 1ea2c5adb..59d23bcca 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -250,6 +250,9 @@ GEM faraday-patron (1.0.0) faraday-rack (1.0.0) ffi (1.15.4) + file_validators (3.0.0) + activemodel (>= 3.2) + mime-types (>= 1.0) font-awesome-sass (5.15.1) sassc (>= 1.11) foundation-rails (6.6.2.0) @@ -723,6 +726,7 @@ DEPENDENCIES erb_lint (~> 0.0.37) factory_bot_rails (~> 6.2.0) faker (~> 2.18.0) + file_validators (~> 3.0.0) font-awesome-sass (~> 5.15.1) foundation-rails (~> 6.6.2.0) foundation_rails_helper (~> 4.0.0) diff --git a/app/models/ckeditor/picture.rb b/app/models/ckeditor/picture.rb index bcce7ee99..8b0846143 100644 --- a/app/models/ckeditor/picture.rb +++ b/app/models/ckeditor/picture.rb @@ -6,11 +6,19 @@ class Ckeditor::Picture < Ckeditor::Asset path: ":rails_root/public/ckeditor_assets/pictures/:id/:style_:basename.:extension", styles: { content: "800>", thumb: "118x100#" } - validates_attachment_presence :data - validates_attachment_size :data, less_than: 2.megabytes - validates_attachment_content_type :data, content_type: /\Aimage/ + do_not_validate_attachment_file_type :data + validate :attachment_presence + validates :data, file_content_type: { allow: /^image\/.*/ }, file_size: { less_than: 2.megabytes } def url_content url(:content) end + + private + + def attachment_presence + unless data.present? + errors.add(:data, I18n.t("errors.messages.blank")) + end + end end diff --git a/app/models/site_customization/image.rb b/app/models/site_customization/image.rb index 49479837a..8648cd046 100644 --- a/app/models/site_customization/image.rb +++ b/app/models/site_customization/image.rb @@ -14,7 +14,9 @@ class SiteCustomization::Image < ApplicationRecord has_attachment :image validates :name, presence: true, uniqueness: true, inclusion: { in: VALID_IMAGES.keys } - validates_attachment_content_type :image, content_type: ["image/png", "image/jpeg"] + do_not_validate_attachment_file_type :image + validates :image, file_content_type: { allow: ["image/png", "image/jpeg"] } + validate :check_image def self.all_images diff --git a/config/i18n-tasks.yml b/config/i18n-tasks.yml index b5932360a..8fe6467f7 100644 --- a/config/i18n-tasks.yml +++ b/config/i18n-tasks.yml @@ -169,6 +169,7 @@ ignore_unused: - "admin.dashboard.administrator_tasks.index.filter*" - "admin.dashboard.actions.index.default.*" - "admin.users.index.filter*" + - "errors.messages.allowed_file_content_types" - "moderation.comments.index.filter*" - "moderation.comments.index.orders.*" - "moderation.debates.index.filter*" diff --git a/config/locales/en/general.yml b/config/locales/en/general.yml index 01477295b..049f090f7 100644 --- a/config/locales/en/general.yml +++ b/config/locales/en/general.yml @@ -150,6 +150,7 @@ en: submit_button: Save changes errors: messages: + allowed_file_content_types: "content type must be one of %{types}" user_not_found: User not found invalid_date_range: "Invalid date range" form: diff --git a/config/locales/es/general.yml b/config/locales/es/general.yml index c76541d7e..bb1ecca96 100644 --- a/config/locales/es/general.yml +++ b/config/locales/es/general.yml @@ -150,6 +150,7 @@ es: submit_button: Guardar cambios errors: messages: + allowed_file_content_types: "el tipo de contenido debe ser uno de los siguientes: %{types}" user_not_found: Usuario no encontrado invalid_date_range: "El rango de fechas no es vĂ¡lido" form: