Class: MiniPaperclip::Attachment
- Inherits:
-
Object
- Object
- MiniPaperclip::Attachment
- Defined in:
- lib/mini_paperclip/attachment.rb
Constant Summary collapse
- UnsupportedError =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#attachment_name ⇒ Object
readonly
Returns the value of attribute attachment_name.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#meta_content_type ⇒ Object
readonly
Returns the value of attribute meta_content_type.
-
#record ⇒ Object
readonly
Returns the value of attribute record.
-
#storage ⇒ Object
readonly
Returns the value of attribute storage.
-
#waiting_write_file ⇒ Object
readonly
Returns the value of attribute waiting_write_file.
Instance Method Summary collapse
- #animated? ⇒ Boolean
- #assign(file) ⇒ Object
- #blank? ⇒ Boolean
- #content_type ⇒ Object
- #dirty? ⇒ Boolean
- #do_delete_files ⇒ Object
- #exists?(style = :original) ⇒ Boolean (also: #exist?)
- #file? ⇒ Boolean (also: #present?)
-
#initialize(record, attachment_name, overwrite_config = {}) ⇒ Attachment
constructor
A new instance of Attachment.
- #original_filename ⇒ Object
- #process_and_store ⇒ Object
- #push_delete_files ⇒ Object
- #size ⇒ Object
- #updated_at ⇒ Object
- #url(style = :original) ⇒ Object
Constructor Details
#initialize(record, attachment_name, overwrite_config = {}) ⇒ Attachment
Returns a new instance of Attachment.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/mini_paperclip/attachment.rb', line 10 def initialize(record, , overwrite_config = {}) @record = record @attachment_name = @config = MiniPaperclip.config.merge(overwrite_config) @waiting_write_file = nil @meta_content_type = nil @dirty = false @storage = Storage.const_get(@config.storage.to_s.camelcase) .new(self, @config) end |
Instance Attribute Details
#attachment_name ⇒ Object (readonly)
Returns the value of attribute attachment_name.
7 8 9 |
# File 'lib/mini_paperclip/attachment.rb', line 7 def @attachment_name end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
7 8 9 |
# File 'lib/mini_paperclip/attachment.rb', line 7 def config @config end |
#meta_content_type ⇒ Object (readonly)
Returns the value of attribute meta_content_type.
7 8 9 |
# File 'lib/mini_paperclip/attachment.rb', line 7 def @meta_content_type end |
#record ⇒ Object (readonly)
Returns the value of attribute record.
7 8 9 |
# File 'lib/mini_paperclip/attachment.rb', line 7 def record @record end |
#storage ⇒ Object (readonly)
Returns the value of attribute storage.
7 8 9 |
# File 'lib/mini_paperclip/attachment.rb', line 7 def storage @storage end |
#waiting_write_file ⇒ Object (readonly)
Returns the value of attribute waiting_write_file.
7 8 9 |
# File 'lib/mini_paperclip/attachment.rb', line 7 def waiting_write_file @waiting_write_file end |
Instance Method Details
#animated? ⇒ Boolean
151 152 153 |
# File 'lib/mini_paperclip/attachment.rb', line 151 def animated? content_type == 'image/gif' end |
#assign(file) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/mini_paperclip/attachment.rb', line 59 def assign(file) @dirty = true @waiting_copy_attachment = nil @waiting_write_file = nil @meta_content_type = nil if present? push_delete_files end if file.nil? assign_nil elsif file.instance_of?(Attachment) if file.present? (file) else assign_nil end elsif file.respond_to?(:original_filename) assign_uploaded_file(file) elsif file.respond_to?(:path) assign_file(file) elsif file.instance_of?(String) if file.empty? # do nothing elsif file.start_with?('http') assign_http(file) elsif file.start_with?('data:') assign_data_uri(file) else raise UnsupportedError, "attachment for \"#{file[0..100]}\" is not supported" end else raise UnsupportedError, "attachment for #{file.class} is not supported" end end |
#blank? ⇒ Boolean
42 43 44 |
# File 'lib/mini_paperclip/attachment.rb', line 42 def blank? !file? end |
#content_type ⇒ Object
25 26 27 |
# File 'lib/mini_paperclip/attachment.rb', line 25 def content_type @record.read_attribute("#{@attachment_name}_content_type") end |
#dirty? ⇒ Boolean
55 56 57 |
# File 'lib/mini_paperclip/attachment.rb', line 55 def dirty? @dirty end |
#do_delete_files ⇒ Object
147 148 149 |
# File 'lib/mini_paperclip/attachment.rb', line 147 def do_delete_files @storage.do_delete_files end |
#exists?(style = :original) ⇒ Boolean Also known as: exist?
46 47 48 |
# File 'lib/mini_paperclip/attachment.rb', line 46 def exists?(style = :original) file? && @storage.exists?(style) end |
#file? ⇒ Boolean Also known as: present?
37 38 39 |
# File 'lib/mini_paperclip/attachment.rb', line 37 def file? original_filename.present? end |
#original_filename ⇒ Object
21 22 23 |
# File 'lib/mini_paperclip/attachment.rb', line 21 def original_filename @record.read_attribute("#{@attachment_name}_file_name") end |
#process_and_store ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/mini_paperclip/attachment.rb', line 96 def process_and_store return unless file? return unless @waiting_write_file begin debug("start attachment styles process") @storage.write(:original, @waiting_write_file) @config.styles&.each do |style, size_arg| Tempfile.create([style.to_s, File.extname(@waiting_write_file.path)]) do |temp| temp.binmode MiniMagick::Tool::Convert.new do |convert| convert << @waiting_write_file.path convert.coalesce if animated? convert.auto_orient if size_arg.end_with?('#') # crop option convert.resize("#{size_arg[0..-2]}^") convert.gravity("center") convert.extent(size_arg[0..-2]) else convert.resize(size_arg) end convert.layers("optimize") if animated? convert << temp.path end @storage.write(style, temp) end end # should delete after write for copy if !@config.keep_old_files do_delete_files end ensure if @waiting_write_file.respond_to?(:close!) @waiting_write_file.close! elsif @waiting_write_file.respond_to?(:close) @waiting_write_file.close end end @waiting_write_file = nil end |
#push_delete_files ⇒ Object
140 141 142 143 144 145 |
# File 'lib/mini_paperclip/attachment.rb', line 140 def push_delete_files @storage.push_delete_file(:original) @config.styles&.each_key do |style| @storage.push_delete_file(style) end end |
#size ⇒ Object
29 30 31 |
# File 'lib/mini_paperclip/attachment.rb', line 29 def size @record.read_attribute("#{@attachment_name}_file_size") end |
#updated_at ⇒ Object
33 34 35 |
# File 'lib/mini_paperclip/attachment.rb', line 33 def updated_at @record.read_attribute("#{@attachment_name}_updated_at") end |
#url(style = :original) ⇒ Object
51 52 53 |
# File 'lib/mini_paperclip/attachment.rb', line 51 def url(style = :original) @storage.url_for_read(style) end |