Class | Paperclip::Attachment |
In: |
lib/paperclip/attachment.rb
|
Parent: | Object |
The Attachment class manages the files for a given attachment. It saves when the model saves, deletes when the model is destroyed, and processes the file upon assignment.
convert_options | [R] | |
default_style | [R] | |
instance | [R] | |
name | [R] | |
options | [R] | |
queued_for_write | [R] | |
whiny | [R] |
# File lib/paperclip/attachment.rb, line 9 9: def self.default_options 10: @default_options ||= { 11: :url => "/system/:attachment/:id/:style/:filename", 12: :path => ":rails_root/public:url", 13: :styles => {}, 14: :processors => [:thumbnail], 15: :convert_options => {}, 16: :default_url => "/:attachment/:style/missing.png", 17: :default_style => :original, 18: :storage => :filesystem, 19: :use_timestamp => true, 20: :whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails] 21: } 22: end
Paths and URLs can have a number of variables interpolated into them to vary the storage location based on name, id, style, class, etc. This method is a deprecated access into supplying and retrieving these interpolations. Future access should use either Paperclip.interpolates or extend the Paperclip::Interpolations module directly.
# File lib/paperclip/attachment.rb, line 211 211: def self.interpolations 212: warn('[DEPRECATION] Paperclip::Attachment.interpolations is deprecated ' + 213: 'and will be removed from future versions. ' + 214: 'Use Paperclip.interpolates instead') 215: Paperclip::Interpolations 216: end
Creates an Attachment object. name is the name of the attachment, instance is the ActiveRecord object instance it‘s attached to, and options is the same as the hash passed to has_attached_file.
# File lib/paperclip/attachment.rb, line 29 29: def initialize name, instance, options = {} 30: @name = name 31: @instance = instance 32: 33: options = self.class.default_options.merge(options) 34: 35: @url = options[:url] 36: @url = @url.call(self) if @url.is_a?(Proc) 37: @path = options[:path] 38: @path = @path.call(self) if @path.is_a?(Proc) 39: @styles = options[:styles] 40: @normalized_styles = nil 41: @default_url = options[:default_url] 42: @default_style = options[:default_style] 43: @storage = options[:storage] 44: @use_timestamp = options[:use_timestamp] 45: @whiny = options[:whiny_thumbnails] || options[:whiny] 46: @convert_options = options[:convert_options] 47: @processors = options[:processors] 48: @options = options 49: @queued_for_delete = [] 50: @queued_for_write = {} 51: @errors = {} 52: @dirty = false 53: 54: initialize_storage 55: end
What gets called when you call instance.attachment = File. It clears errors, assigns attributes, and processes the file. It also queues up the previous file for deletion, to be flushed away on save of its host. In addition to form uploads, you can also assign another Paperclip attachment:
new_user.avatar = old_user.avatar
# File lib/paperclip/attachment.rb, line 77 77: def assign uploaded_file 78: ensure_required_accessors! 79: 80: if uploaded_file.is_a?(Paperclip::Attachment) 81: uploaded_file = uploaded_file.to_file(:original) 82: close_uploaded_file = uploaded_file.respond_to?(:close) 83: end 84: 85: return nil unless valid_assignment?(uploaded_file) 86: 87: uploaded_file.binmode if uploaded_file.respond_to? :binmode 88: self.clear 89: 90: return nil if uploaded_file.nil? 91: 92: @queued_for_write[:original] = to_tempfile(uploaded_file) 93: instance_write(:file_name, uploaded_file.original_filename.strip) 94: instance_write(:content_type, uploaded_file.content_type.to_s.strip) 95: instance_write(:file_size, uploaded_file.size.to_i) 96: instance_write(:fingerprint, generate_fingerprint(uploaded_file)) 97: instance_write(:updated_at, Time.now) 98: 99: @dirty = true 100: 101: post_process 102: 103: # Reset the file size if the original file was reprocessed. 104: instance_write(:file_size, @queued_for_write[:original].size.to_i) 105: instance_write(:fingerprint, generate_fingerprint(@queued_for_write[:original])) 106: ensure 107: uploaded_file.close if close_uploaded_file 108: end
Returns the content_type of the file as originally assigned, and lives in the <attachment>_content_type attribute of the model.
# File lib/paperclip/attachment.rb, line 189 189: def content_type 190: instance_read(:content_type) 191: end
Returns true if there are changes that need to be saved.
# File lib/paperclip/attachment.rb, line 140 140: def dirty? 141: @dirty 142: end
Returns true if a file has been assigned.
# File lib/paperclip/attachment.rb, line 241 241: def file? 242: !original_filename.blank? 243: end
Returns the hash of the file as originally assigned, and lives in the <attachment>_fingerprint attribute of the model.
# File lib/paperclip/attachment.rb, line 183 183: def fingerprint 184: instance_read(:fingerprint) || (@queued_for_write[:original] && generate_fingerprint(@queued_for_write[:original])) 185: end
# File lib/paperclip/attachment.rb, line 200 200: def generate_fingerprint(source) 201: data = source.read 202: source.rewind if source.respond_to?(:rewind) 203: Digest::MD5.hexdigest(data) 204: end
Reads the attachment-specific attribute on the instance. See instance_write for more details.
# File lib/paperclip/attachment.rb, line 257 257: def instance_read(attr) 258: getter = "#{name}_#{attr}""#{name}_#{attr}" 259: responds = instance.respond_to?(getter) 260: cached = self.instance_variable_get("@_#{getter}") 261: return cached if cached 262: instance.send(getter) if responds || attr.to_s == "file_name" 263: end
Writes the attachment-specific attribute on the instance. For example, instance_write(:file_name, "me.jpg") will write "me.jpg" to the instance‘s "avatar_file_name" field (assuming the attachment is called avatar).
# File lib/paperclip/attachment.rb, line 248 248: def instance_write(attr, value) 249: setter = "#{name}_#{attr}=""#{name}_#{attr}=" 250: responds = instance.respond_to?(setter) 251: self.instance_variable_set("@_#{setter.to_s.chop}", value) 252: instance.send(setter, value) if responds || attr.to_s == "file_name" 253: end
Returns the name of the file as originally assigned, and lives in the <attachment>_file_name attribute of the model.
# File lib/paperclip/attachment.rb, line 171 171: def original_filename 172: instance_read(:file_name) 173: end
Returns the path of the attachment as defined by the :path option. If the file is stored in the filesystem the path refers to the path of the file on disk. If the file is stored in S3, the path is the "key" part of the URL, and the :bucket option refers to the S3 bucket.
# File lib/paperclip/attachment.rb, line 125 125: def path style_name = default_style 126: original_filename.nil? ? nil : interpolate(@path, style_name) 127: end
# File lib/paperclip/attachment.rb, line 67 67: def processors 68: @processors.respond_to?(:call) ? @processors.call(instance) : @processors 69: end
This method really shouldn‘t be called that often. It‘s expected use is in the paperclip:refresh rake task and that‘s it. It will regenerate all thumbnails forcefully, by reobtaining the original file and going through the post-process again.
# File lib/paperclip/attachment.rb, line 222 222: def reprocess! 223: new_original = Tempfile.new("paperclip-reprocess") 224: new_original.binmode 225: if old_original = to_file(:original) 226: new_original.write( old_original.respond_to?(:get) ? old_original.get : old_original.read ) 227: new_original.rewind 228: 229: @queued_for_write = { :original => new_original } 230: post_process 231: 232: old_original.close if old_original.respond_to?(:close) 233: 234: save 235: else 236: true 237: end 238: end
# File lib/paperclip/attachment.rb, line 57 57: def styles 58: unless @normalized_styles 59: @normalized_styles = {} 60: (@styles.respond_to?(:call) ? @styles.call(self) : @styles).each do |name, args| 61: @normalized_styles[name] = Paperclip::Style.new(name, args.dup, self) 62: end 63: end 64: @normalized_styles 65: end
Returns the last modified time of the file as originally assigned, and lives in the <attachment>_updated_at attribute of the model.
# File lib/paperclip/attachment.rb, line 195 195: def updated_at 196: time = instance_read(:updated_at) 197: time && time.to_f.to_i 198: end
Returns the public URL of the attachment, with a given style. Note that this does not necessarily need to point to a file that your web server can access and can point to an action in your app, if you need fine grained security. This is not recommended if you don‘t need the security, however, for performance reasons. Set use_timestamp to false if you want to stop the attachment update time appended to the url
# File lib/paperclip/attachment.rb, line 116 116: def url(style_name = default_style, use_timestamp = @use_timestamp) 117: url = original_filename.nil? ? interpolate(@default_url, style_name) : interpolate(@url, style_name) 118: use_timestamp && updated_at ? [url, updated_at].compact.join(url.include?("?") ? "&" : "?") : url 119: end