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

Attributes

default_style  [R] 
instance  [R] 
name  [R] 
styles  [R] 

Public Class methods

[Source]

    # File lib/paperclip/attachment.rb, line 6
 6:     def self.default_options
 7:       @default_options ||= {
 8:         :url           => "/:attachment/:id/:style/:basename.:extension",
 9:         :path          => ":rails_root/public/:attachment/:id/:style/:basename.:extension",
10:         :styles        => {},
11:         :default_url   => "/:attachment/:style/missing.png",
12:         :default_style => :original,
13:         :validations   => [],
14:         :storage       => :filesystem
15:       }
16:     end

A hash of procs that are run during the interpolation of a path or url. A variable of the format :name will be replaced with the return value of the proc named ":name". Each lambda takes the attachment and the current style as arguments. This hash can be added to with your own proc if necessary.

[Source]

     # File lib/paperclip/attachment.rb, line 134
134:     def self.interpolations
135:       @interpolations ||= {
136:         :rails_root   => lambda{|attachment,style| RAILS_ROOT },
137:         :class        => lambda do |attachment,style|
138:                            attachment.instance.class.name.underscore.pluralize
139:                          end,
140:         :basename     => lambda do |attachment,style|
141:                            attachment.original_filename.gsub(File.extname(attachment.original_filename), "")
142:                          end,
143:         :extension    => lambda do |attachment,style| 
144:                            ((style = attachment.styles[style]) && style.last) ||
145:                            File.extname(attachment.original_filename).gsub(/^\.+/, "")
146:                          end,
147:         :id           => lambda{|attachment,style| attachment.instance.id },
148:         :id_partition => lambda do |attachment, style|
149:                            ("%09d" % attachment.instance.id).scan(/\d{3}/).join("/")
150:                          end,
151:         :attachment   => lambda{|attachment,style| attachment.name.to_s.downcase.pluralize },
152:         :style        => lambda{|attachment,style| style || attachment.default_style },
153:       }
154:     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.

[Source]

    # File lib/paperclip/attachment.rb, line 23
23:     def initialize name, instance, options = {}
24:       @name              = name
25:       @instance          = instance
26: 
27:       options = self.class.default_options.merge(options)
28: 
29:       @url               = options[:url]
30:       @path              = options[:path]
31:       @styles            = options[:styles]
32:       @default_url       = options[:default_url]
33:       @validations       = options[:validations]
34:       @default_style     = options[:default_style]
35:       @storage           = options[:storage]
36:       @whiny_thumbnails  = options[:whiny_thumbnails]
37:       @options           = options
38:       @queued_for_delete = []
39:       @queued_for_write  = {}
40:       @errors            = []
41:       @validation_errors = nil
42:       @dirty             = false
43: 
44:       normalize_style_definition
45:       initialize_storage
46:     end

Public Instance methods

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.

[Source]

    # File lib/paperclip/attachment.rb, line 51
51:     def assign uploaded_file
52:       return nil unless valid_assignment?(uploaded_file)
53: 
54:       queue_existing_for_delete
55:       @errors            = []
56:       @validation_errors = nil
57: 
58:       return nil if uploaded_file.nil?
59: 
60:       @queued_for_write[:original]        = uploaded_file.to_tempfile
61:       @instance["#{@name}_file_name""#{@name}_file_name"]    = uploaded_file.original_filename
62:       @instance["#{@name}_content_type""#{@name}_content_type"] = uploaded_file.content_type
63:       @instance["#{@name}_file_size""#{@name}_file_size"]    = uploaded_file.size
64: 
65:       @dirty = true
66: 
67:       post_process
68:     ensure
69:       validate
70:     end

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

[Source]

     # File lib/paperclip/attachment.rb, line 105
105:     def dirty?
106:       @dirty
107:     end

Returns an array containing the errors on this attachment.

[Source]

     # File lib/paperclip/attachment.rb, line 100
100:     def errors
101:       @errors.compact.uniq
102:     end

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

[Source]

     # File lib/paperclip/attachment.rb, line 125
125:     def original_filename
126:       instance["#{name}_file_name""#{name}_file_name"]
127:     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.

[Source]

     # File lib/paperclip/attachment.rb, line 160
160:     def reprocess!
161:       new_original = Tempfile.new("paperclip-reprocess")
162:       old_original = to_file(:original)
163:       new_original.write( old_original.read )
164:       new_original.rewind
165: 
166:       @queued_for_write = { :original => new_original }
167:       post_process
168: 
169:       old_original.close if old_original.respond_to?(:close)
170:     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.

[Source]

     # File lib/paperclip/attachment.rb, line 111
111:     def save
112:       if valid?
113:         flush_deletes
114:         flush_writes
115:         @dirty = false
116:         true
117:       else
118:         flush_errors
119:         false
120:       end
121:     end

Alias to url

[Source]

    # File lib/paperclip/attachment.rb, line 90
90:     def to_s style = nil
91:       url(style)
92:     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.

[Source]

    # File lib/paperclip/attachment.rb, line 77
77:     def url style = default_style
78:       original_filename.nil? ? interpolate(@default_url, style) : interpolate(@url, style)
79:     end

Returns true if there are any errors on this attachment.

[Source]

    # File lib/paperclip/attachment.rb, line 95
95:     def valid?
96:       errors.length == 0
97:     end

[Validate]