Use file_validators instead of Papeclip validations

Since we're going to remove Paperclip and Active Storage doesn't provide
any validations, we have to either write our own validation rules or use
a different gem.

We're using the file_validators gem instead of the
`active_storage_validations` gem because the latter doesn't support
proc/lambda objects in size and content type definitions. We need to use
them because in our case these values depend on settings stored in the
database.
This commit is contained in:
Javi Martín
2021-07-27 23:50:43 +02:00
parent 7daf75d01a
commit 600f5c35e9
7 changed files with 22 additions and 4 deletions

View File

@@ -19,6 +19,7 @@ gem "dalli", "~> 2.7.11"
gem "delayed_job_active_record", "~> 4.1.6" gem "delayed_job_active_record", "~> 4.1.6"
gem "devise", "~> 4.8.0" gem "devise", "~> 4.8.0"
gem "devise-security", "~> 0.16.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 "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", "~> 6.6.2.0"
gem "foundation_rails_helper", "~> 4.0.0" gem "foundation_rails_helper", "~> 4.0.0"

View File

@@ -250,6 +250,9 @@ GEM
faraday-patron (1.0.0) faraday-patron (1.0.0)
faraday-rack (1.0.0) faraday-rack (1.0.0)
ffi (1.15.4) ffi (1.15.4)
file_validators (3.0.0)
activemodel (>= 3.2)
mime-types (>= 1.0)
font-awesome-sass (5.15.1) font-awesome-sass (5.15.1)
sassc (>= 1.11) sassc (>= 1.11)
foundation-rails (6.6.2.0) foundation-rails (6.6.2.0)
@@ -723,6 +726,7 @@ DEPENDENCIES
erb_lint (~> 0.0.37) erb_lint (~> 0.0.37)
factory_bot_rails (~> 6.2.0) factory_bot_rails (~> 6.2.0)
faker (~> 2.18.0) faker (~> 2.18.0)
file_validators (~> 3.0.0)
font-awesome-sass (~> 5.15.1) font-awesome-sass (~> 5.15.1)
foundation-rails (~> 6.6.2.0) foundation-rails (~> 6.6.2.0)
foundation_rails_helper (~> 4.0.0) foundation_rails_helper (~> 4.0.0)

View File

@@ -6,11 +6,19 @@ class Ckeditor::Picture < Ckeditor::Asset
path: ":rails_root/public/ckeditor_assets/pictures/:id/:style_:basename.:extension", path: ":rails_root/public/ckeditor_assets/pictures/:id/:style_:basename.:extension",
styles: { content: "800>", thumb: "118x100#" } styles: { content: "800>", thumb: "118x100#" }
validates_attachment_presence :data do_not_validate_attachment_file_type :data
validates_attachment_size :data, less_than: 2.megabytes validate :attachment_presence
validates_attachment_content_type :data, content_type: /\Aimage/ validates :data, file_content_type: { allow: /^image\/.*/ }, file_size: { less_than: 2.megabytes }
def url_content def url_content
url(:content) url(:content)
end end
private
def attachment_presence
unless data.present?
errors.add(:data, I18n.t("errors.messages.blank"))
end
end
end end

View File

@@ -14,7 +14,9 @@ class SiteCustomization::Image < ApplicationRecord
has_attachment :image has_attachment :image
validates :name, presence: true, uniqueness: true, inclusion: { in: VALID_IMAGES.keys } 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 validate :check_image
def self.all_images def self.all_images

View File

@@ -169,6 +169,7 @@ ignore_unused:
- "admin.dashboard.administrator_tasks.index.filter*" - "admin.dashboard.administrator_tasks.index.filter*"
- "admin.dashboard.actions.index.default.*" - "admin.dashboard.actions.index.default.*"
- "admin.users.index.filter*" - "admin.users.index.filter*"
- "errors.messages.allowed_file_content_types"
- "moderation.comments.index.filter*" - "moderation.comments.index.filter*"
- "moderation.comments.index.orders.*" - "moderation.comments.index.orders.*"
- "moderation.debates.index.filter*" - "moderation.debates.index.filter*"

View File

@@ -150,6 +150,7 @@ en:
submit_button: Save changes submit_button: Save changes
errors: errors:
messages: messages:
allowed_file_content_types: "content type must be one of %{types}"
user_not_found: User not found user_not_found: User not found
invalid_date_range: "Invalid date range" invalid_date_range: "Invalid date range"
form: form:

View File

@@ -150,6 +150,7 @@ es:
submit_button: Guardar cambios submit_button: Guardar cambios
errors: errors:
messages: messages:
allowed_file_content_types: "el tipo de contenido debe ser uno de los siguientes: %{types}"
user_not_found: Usuario no encontrado user_not_found: Usuario no encontrado
invalid_date_range: "El rango de fechas no es válido" invalid_date_range: "El rango de fechas no es válido"
form: form: