about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--app/models/account.rb16
-rw-r--r--app/models/concerns/attachmentable.rb21
-rw-r--r--app/models/preview_card.rb2
3 files changed, 24 insertions, 15 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index 19f8ca365..798042b3b 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -21,7 +21,7 @@ class Account < ApplicationRecord
   validates_attachment_content_type :header, content_type: IMAGE_MIME_TYPES
   validates_attachment_size :header, less_than: 2.megabytes
 
-  before_post_process :set_file_extensions
+  include Attachmentable
 
   # Local user profile validations
   validates :display_name, length: { maximum: 30 }, if: 'local?'
@@ -363,18 +363,4 @@ class Account < ApplicationRecord
 
     self.domain = TagManager.instance.normalize_domain(domain)
   end
-
-  def set_file_extensions
-    unless avatar.blank?
-      extension = Paperclip::Interpolations.content_type_extension(avatar, :original)
-      basename  = Paperclip::Interpolations.basename(avatar, :original)
-      avatar.instance_write :file_name, [basename, extension].delete_if(&:empty?).join('.')
-    end
-
-    unless header.blank?
-      extension = Paperclip::Interpolations.content_type_extension(header, :original)
-      basename  = Paperclip::Interpolations.basename(header, :original)
-      header.instance_write :file_name, [basename, extension].delete_if(&:empty?).join('.')
-    end
-  end
 end
diff --git a/app/models/concerns/attachmentable.rb b/app/models/concerns/attachmentable.rb
new file mode 100644
index 000000000..995643ada
--- /dev/null
+++ b/app/models/concerns/attachmentable.rb
@@ -0,0 +1,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
diff --git a/app/models/preview_card.rb b/app/models/preview_card.rb
index 0aa771101..238588157 100644
--- a/app/models/preview_card.rb
+++ b/app/models/preview_card.rb
@@ -11,6 +11,8 @@ class PreviewCard < ApplicationRecord
 
   has_attached_file :image, styles: { original: '120x120#' }, convert_options: { all: '-quality 80 -strip' }
 
+  include Attachmentable
+
   validates :url, presence: true
   validates_attachment_content_type :image, content_type: IMAGE_MIME_TYPES
   validates_attachment_size :image, less_than: 1.megabytes