about summary refs log tree commit diff
path: root/app/models/concerns/attachmentable.rb
blob: 995643ada517c5dcd321a9fc9666a9e9390c21e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# frozen_string_literal: true

module Attachmentable
  extend ActiveSupport::Concern

  included do
    before_post_process :set_file_extensions
  end

  private

  def set_file_extensions
    self.class.attachment_definitions.each_key do |attachment_name|
      attachment = send(attachment_name)
      next if attachment.blank?
      extension = Paperclip::Interpolations.content_type_extension(attachment, :original)
      basename  = Paperclip::Interpolations.basename(attachment, :original)
      attachment.instance_write :file_name, [basename, extension].delete_if(&:empty?).join('.')
    end
  end
end