about summary refs log tree commit diff
path: root/app/models/concerns
diff options
context:
space:
mode:
authorYamagishi Kazutoshi <ykzts@desire.sh>2017-05-01 23:20:57 +0900
committerEugen Rochko <eugen@zeonfederated.com>2017-05-01 16:20:57 +0200
commit1899cf5f04400f8055e45ceda941a9580b93fa1e (patch)
tree1e38818e9c7f8aee79c0111265b13aef34f29e1d /app/models/concerns
parent5259319cf5d01a23a0e9517b9dc91c0a1f7e2ae9 (diff)
Detect extension for preview card (#2679)
* Detect extension for preview card

* next
Diffstat (limited to 'app/models/concerns')
-rw-r--r--app/models/concerns/attachmentable.rb21
1 files changed, 21 insertions, 0 deletions
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