Class | Paperclip::Attachment |
In: |
lib/dm-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] | |
styles | [R] |
# File lib/dm-paperclip/attachment.rb, line 7 7: def self.default_options 8: @default_options ||= { 9: :url => "/system/:attachment/:id/:style/:filename", 10: :path => ":rails_root/public:url", 11: :styles => {}, 12: :default_url => "/:attachment/:style/missing.png", 13: :default_style => :original, 14: :validations => [], 15: :storage => :filesystem, 16: :whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails] 17: } 18: 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/dm-paperclip/attachment.rb, line 208 208: def self.interpolations 209: warn('[DEPRECATION] Paperclip::Attachment.interpolations is deprecated ' + 210: 'and will be removed from future versions. ' + 211: 'Use Paperclip.interpolates instead') 212: Paperclip::Interpolations 213: 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/dm-paperclip/attachment.rb, line 25 25: def initialize name, instance, options = {} 26: @name = name 27: @instance = instance 28: 29: options = self.class.default_options.merge(options) 30: 31: @url = options[:url] 32: @url = @url.call(self) if @url.is_a?(Proc) 33: @path = options[:path] 34: @path = @path.call(self) if @path.is_a?(Proc) 35: @styles = options[:styles] 36: @styles = @styles.call(self) if @styles.is_a?(Proc) 37: @default_url = options[:default_url] 38: @validations = options[:validations] 39: @default_style = options[:default_style] 40: @storage = options[:storage] 41: @whiny = options[:whiny_thumbnails] || options[:whiny] 42: @convert_options = options[:convert_options] || {} 43: @processors = options[:processors] || [:thumbnail] 44: @options = options 45: @queued_for_delete = [] 46: @queued_for_write = {} 47: @errors = {} 48: @validation_errors = nil 49: @dirty = false 50: 51: normalize_style_definition 52: initialize_storage 53: end
What gets called when you call instance.attachment = File. It clears errors, assigns attributes, processes the file, and runs validations. 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
If the file that is assigned is not valid, the processing (i.e. thumbnailing, etc) will NOT be run.
# File lib/dm-paperclip/attachment.rb, line 63 63: def assign uploaded_file 64: 65: ensure_required_accessors! 66: 67: if uploaded_file.is_a?(Paperclip::Attachment) 68: uploaded_file = uploaded_file.to_file(:original) 69: close_uploaded_file = uploaded_file.respond_to?(:close) 70: end 71: 72: return nil unless valid_assignment?(uploaded_file) 73: 74: uploaded_file.binmode if uploaded_file.respond_to? :binmode 75: self.clear 76: 77: return nil if uploaded_file.nil? 78: 79: if uploaded_file.respond_to?(:[]) 80: uploaded_file = uploaded_file.to_mash 81: 82: @queued_for_write[:original] = uploaded_file['tempfile'] 83: instance_write(:file_name, uploaded_file['filename'].strip.gsub(/[^\w\d\.\-]+/, '_')) 84: instance_write(:content_type, uploaded_file['content_type'] ? uploaded_file['content_type'].strip : uploaded_file['tempfile'].content_type.to_s.strip) 85: instance_write(:file_size, uploaded_file['size'] ? uploaded_file['size'].to_i : uploaded_file['tempfile'].size.to_i) 86: instance_write(:updated_at, Time.now) 87: else 88: @queued_for_write[:original] = uploaded_file.to_tempfile 89: instance_write(:file_name, uploaded_file.original_filename.strip.gsub(/[^\w\d\.\-]+/, '_')) 90: instance_write(:content_type, uploaded_file.content_type.to_s.strip) 91: instance_write(:file_size, uploaded_file.size.to_i) 92: instance_write(:updated_at, Time.now) 93: end 94: 95: @dirty = true 96: 97: post_process if valid? 98: 99: # Reset the file size if the original file was reprocessed. 100: instance_write(:file_size, @queued_for_write[:original].size.to_i) 101: 102: ensure 103: uploaded_file.close if close_uploaded_file 104: validate 105: end
Clears out the attachment. Has the same effect as previously assigning nil to the attachment. Does NOT save. If you wish to clear AND save, use destroy.
# File lib/dm-paperclip/attachment.rb, line 165 165: def clear 166: queue_existing_for_delete 167: @errors = {} 168: @validation_errors = nil 169: end
Returns the content_type of the file as originally assigned, and lives in the <attachment>_content_type attribute of the model.
# File lib/dm-paperclip/attachment.rb, line 193 193: def content_type 194: instance_read(:content_type) 195: end
Returns true if there are changes that need to be saved.
# File lib/dm-paperclip/attachment.rb, line 144 144: def dirty? 145: @dirty 146: end
Returns true if a file has been assigned.
# File lib/dm-paperclip/attachment.rb, line 238 238: def file? 239: !original_filename.blank? 240: end
Reads the attachment-specific attribute on the instance. See instance_write for more details.
# File lib/dm-paperclip/attachment.rb, line 254 254: def instance_read(attr) 255: getter = "#{name}_#{attr}""#{name}_#{attr}" 256: responds = instance.respond_to?(getter) 257: cached = self.instance_variable_get("@_#{getter}") 258: return cached if cached 259: instance.send(getter) if responds || attr.to_s == "file_name" 260: 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/dm-paperclip/attachment.rb, line 245 245: def instance_write(attr, value) 246: setter = "#{name}_#{attr}=""#{name}_#{attr}=" 247: responds = instance.respond_to?(setter) 248: self.instance_variable_set("@_#{setter.to_s.chop}", value) 249: instance.send(setter, value) if responds || attr.to_s == "file_name" 250: end
Returns the name of the file as originally assigned, and lives in the <attachment>_file_name attribute of the model.
# File lib/dm-paperclip/attachment.rb, line 181 181: def original_filename 182: instance_read(:file_name) 183: 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/dm-paperclip/attachment.rb, line 123 123: def path style = default_style 124: original_filename.nil? ? nil : interpolate(@path, style) 125: 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/dm-paperclip/attachment.rb, line 219 219: def reprocess! 220: new_original = Tempfile.new("paperclip-reprocess") 221: new_original.binmode 222: if old_original = to_file(:original) 223: new_original.write( old_original.read ) 224: new_original.rewind 225: 226: @queued_for_write = { :original => new_original } 227: post_process 228: 229: old_original.close if old_original.respond_to?(:close) 230: 231: save 232: else 233: true 234: end 235: end
Saves the file, if there are no errors. If there are, it flushes them to the instance‘s errors and returns false, cancelling the save.
# File lib/dm-paperclip/attachment.rb, line 150 150: def save 151: if valid? 152: flush_deletes 153: flush_writes 154: @dirty = false 155: true 156: else 157: flush_errors 158: false 159: end 160: 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/dm-paperclip/attachment.rb, line 199 199: def updated_at 200: instance_read(:updated_at) 201: 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 include_updated_timestamp to false if you want to stop the attachment update time appended to the url
# File lib/dm-paperclip/attachment.rb, line 114 114: def url style = default_style, include_updated_timestamp = true 115: the_url = original_filename.nil? ? interpolate(@default_url, style) : interpolate(@url, style) 116: include_updated_timestamp && updated_at ? [the_url, updated_at.to_time.to_i].compact.join(the_url.include?("?") ? "&" : "?") : the_url 117: end