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.
25 lines
710 B
Ruby
25 lines
710 B
Ruby
class Ckeditor::Picture < Ckeditor::Asset
|
|
include HasAttachment
|
|
|
|
has_attachment :data,
|
|
url: "/ckeditor_assets/pictures/:id/:style_:basename.:extension",
|
|
path: ":rails_root/public/ckeditor_assets/pictures/:id/:style_:basename.:extension",
|
|
styles: { content: "800>", thumb: "118x100#" }
|
|
|
|
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
|