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.

Methods

Included Modules

IOStream

Attributes

convert_options  [R] 
default_style  [R] 
instance  [R] 
interpolator  [R] 
name  [R] 
options  [R] 
post_processing  [RW] 
queued_for_write  [R] 
source_file_options  [R] 
whiny  [R] 
whiny  [R] 

Public Class methods

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.

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.

Options include:

url - a relative URL of the attachment. This is interpolated using interpolator path - where on the filesystem to store the attachment. This is interpolated using interpolator styles - a hash of options for processing the attachment. See has_attached_file for the details only_process - style args to be run through the post-processor. This defaults to the empty list default_url - a URL for the missing image default_style - the style to use when don‘t specify an argument to e.g. url, path storage - the storage mechanism. Defaults to :filesystem use_timestamp - whether to append an anti-caching timestamp to image URLs. Defaults to true whiny, whiny_thumbnails - whether to raise when thumbnailing fails use_default_time_zone - related to use_timestamp. Defaults to true hash_digest - a string representing a class that will be used to hash URLs for obfuscation hash_data - the relative URL for the hash data. This is interpolated using interpolator hash_secret - a secret passed to the hash_digest convert_options - flags passed to the convert command for processing source_file_options - flags passed to the convert command that controls how the file is read processors - classes that transform the attachment. Defaults to [:thumbnail] preserve_files - whether to keep files on the filesystem when deleting to clearing the attachment. Defaults to false interpolator - the object used to interpolate filenames and URLs. Defaults to Paperclip::Interpolations url_generator - the object used to generate URLs, using the interpolator. Defaults to Paperclip::UrlGenerator

Public Instance methods

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

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.

Returns the content_type of the file as originally assigned, and lives in the <attachment>_content_type attribute of the model.

Destroys the attachment. Has the same effect as previously assigning nil to the attachment *and saving*. This is permanent. If you wish to wipe out the existing attachment but not save, use clear.

Returns true if there are changes that need to be saved.

Returns an array containing the errors on this attachment.

Returns true if a file has been assigned.

Returns the hash of the file as originally assigned, and lives in the <attachment>_fingerprint attribute of the model.

Returns a unique hash suitable for obfuscating the URL of an otherwise publicly viewable attachment.

Reads the attachment-specific attribute on the instance. See instance_write for more details.

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).

Returns the name of the file as originally assigned, and lives in the <attachment>_file_name attribute of the model.

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.

present?()

Alias for file?

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.

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.

Returns the size of the file as originally assigned, and lives in the <attachment>_file_size attribute of the model.

The time zone to use for timestamp interpolation. Using the default time zone ensures that results are consistent across all threads.

Returns the last modified time of the file as originally assigned, and lives in the <attachment>_updated_at attribute of the model.

Returns the uploaded file if present.

Returns the public URL of the attachment with a given style. This does not necessarily need to point to a file that your Web server can access and can instead point to an action in your app, for example for fine grained security; this has a serious performance tradeoff.

Options:

timestamp - Add a timestamp to the end of the URL. Default: true. escape - Perform URI escaping to the URL. Default: true.

Global controls (set on has_attached_file):

interpolator - The object that fills in a URL pattern‘s variables. default_url - The image to show when the attachment has no image. url - The URL for a saved image. url_generator - The object that generates a URL. Default: Paperclip::UrlGenerator.

As mentioned just above, the object that generates this URL can be passed in, for finer control. This object must respond to two methods:

+new(Paperclip::Attachment, options_hash)+ +for(style_name, options_hash)+

[Validate]