| 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] | |
| styles | [R] |
# File lib/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/paperclip/attachment.rb, line 197
197: def self.interpolations
198: warn('[DEPRECATION] Paperclip::Attachment.interpolations is deprecated ' +
199: 'and will be removed from future versions. ' +
200: 'Use Paperclip.interpolates instead')
201: Paperclip::Interpolations
202: 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 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/paperclip/attachment.rb, line 63
63: def assign uploaded_file
64: ensure_required_accessors!
65:
66: if uploaded_file.is_a?(Paperclip::Attachment)
67: uploaded_file = uploaded_file.to_file(:original)
68: close_uploaded_file = uploaded_file.respond_to?(:close)
69: end
70:
71: return nil unless valid_assignment?(uploaded_file)
72:
73: uploaded_file.binmode if uploaded_file.respond_to? :binmode
74: self.clear
75:
76: return nil if uploaded_file.nil?
77:
78: @queued_for_write[:original] = uploaded_file.to_tempfile
79: instance_write(:file_name, uploaded_file.original_filename.strip.gsub(/[^\w\d\.\-]+/, '_'))
80: instance_write(:content_type, uploaded_file.content_type.to_s.strip)
81: instance_write(:file_size, uploaded_file.size.to_i)
82: instance_write(:updated_at, Time.now)
83:
84: @dirty = true
85:
86: post_process if valid?
87:
88: # Reset the file size if the original file was reprocessed.
89: instance_write(:file_size, @queued_for_write[:original].size.to_i)
90: ensure
91: uploaded_file.close if close_uploaded_file
92: validate
93: 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/paperclip/attachment.rb, line 153
153: def clear
154: queue_existing_for_delete
155: @errors = {}
156: @validation_errors = nil
157: 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 181
181: def content_type
182: instance_read(:content_type)
183: end
Returns true if there are changes that need to be saved.
# File lib/paperclip/attachment.rb, line 132
132: def dirty?
133: @dirty
134: end
Returns true if a file has been assigned.
# File lib/paperclip/attachment.rb, line 227
227: def file?
228: !original_filename.blank?
229: end
Reads the attachment-specific attribute on the instance. See instance_write for more details.
# File lib/paperclip/attachment.rb, line 243
243: def instance_read(attr)
244: getter = "#{name}_#{attr}""#{name}_#{attr}"
245: responds = instance.respond_to?(getter)
246: cached = self.instance_variable_get("@_#{getter}")
247: return cached if cached
248: instance.send(getter) if responds || attr.to_s == "file_name"
249: 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 234
234: def instance_write(attr, value)
235: setter = "#{name}_#{attr}=""#{name}_#{attr}="
236: responds = instance.respond_to?(setter)
237: self.instance_variable_set("@_#{setter.to_s.chop}", value)
238: instance.send(setter, value) if responds || attr.to_s == "file_name"
239: 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 169
169: def original_filename
170: instance_read(:file_name)
171: 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 111
111: def path style = default_style
112: original_filename.nil? ? nil : interpolate(@path, style)
113: 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 208
208: def reprocess!
209: new_original = Tempfile.new("paperclip-reprocess")
210: new_original.binmode
211: if old_original = to_file(:original)
212: new_original.write( old_original.read )
213: new_original.rewind
214:
215: @queued_for_write = { :original => new_original }
216: post_process
217:
218: old_original.close if old_original.respond_to?(:close)
219:
220: save
221: else
222: true
223: end
224: 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/paperclip/attachment.rb, line 138
138: def save
139: if valid?
140: flush_deletes
141: flush_writes
142: @dirty = false
143: true
144: else
145: flush_errors
146: false
147: end
148: 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 187
187: def updated_at
188: time = instance_read(:updated_at)
189: time && time.to_i
190: 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/paperclip/attachment.rb, line 102
102: def url style = default_style, include_updated_timestamp = true
103: url = original_filename.nil? ? interpolate(@default_url, style) : interpolate(@url, style)
104: include_updated_timestamp && updated_at ? [url, updated_at].compact.join(url.include?("?") ? "&" : "?") : url
105: end