attachmentable.rb 610 B

123456789101112131415161718192021
  1. # frozen_string_literal: true
  2. module Attachmentable
  3. extend ActiveSupport::Concern
  4. included do
  5. before_post_process :set_file_extensions
  6. end
  7. private
  8. def set_file_extensions
  9. self.class.attachment_definitions.each_key do |attachment_name|
  10. attachment = send(attachment_name)
  11. next if attachment.blank?
  12. extension = Paperclip::Interpolations.content_type_extension(attachment, :original)
  13. basename = Paperclip::Interpolations.basename(attachment, :original)
  14. attachment.instance_write :file_name, [basename, extension].delete_if(&:blank?).join('.')
  15. end
  16. end
  17. end