| Module | Paperclip::ClassMethods |
| In: |
lib/paperclip.rb
|
Returns the attachment definitions defined by each call to has_attached_file.
# File lib/paperclip.rb, line 310
310: def attachment_definitions
311: read_inheritable_attribute(:attachment_definitions)
312: end
has_attached_file gives the class it is called on an attribute that maps to a file. This is typically a file stored somewhere on the filesystem and has been uploaded by a user. The attribute returns a Paperclip::Attachment object which handles the management of that file. The intent is to make the attachment as much like a normal attribute. The thumbnails will be created when the new file is assigned, but they will not be saved until save is called on the record. Likewise, if the attribute is set to nil is called on it, the attachment will not be deleted until save is called. See the Paperclip::Attachment documentation for more specifics. There are a number of options you can set to change the behavior of a Paperclip attachment:
:url => "/:class/:attachment/:id/:style_:filename" :url => "http://some.other.host/stuff/:class/:id_:extension"
has_attached_file :avatar, :default_url => "/images/default_:style_avatar.png" User.new.avatar_url(:small) # => "/images/default_small_avatar.png"
has_attached_file :avatar, :styles => { :normal => "100x100#" },
:default_style => :normal
user.avatar.url # => "/avatars/23/normal_me.png"
has_attached_file :avatar, :styles => { :large => "300x300", :negative => "100x100" }
:convert_options => {
:all => "-strip",
:negative => "-negate"
}
NOTE: While not deprecated yet, it is not recommended to specify options this way. It is recommended that :convert_options option be included in the hash passed to each :styles for compatability with future versions.
# File lib/paperclip.rb, line 214
214: def has_attached_file name, options = {}
215: include InstanceMethods
216:
217: write_inheritable_attribute(:attachment_definitions, {}) if attachment_definitions.nil?
218: attachment_definitions[name] = {:validations => []}.merge(options)
219:
220: after_save :save_attached_files
221: before_destroy :destroy_attached_files
222:
223: define_callbacks :before_post_process, :after_post_process
224: define_callbacks "before_#{name}_post_process""before_#{name}_post_process", "after_#{name}_post_process""after_#{name}_post_process"
225:
226: define_method name do |*args|
227: a = attachment_for(name)
228: (args.length > 0) ? a.to_s(args.first) : a
229: end
230:
231: define_method "#{name}=" do |file|
232: attachment_for(name).assign(file)
233: end
234:
235: define_method "#{name}?" do
236: attachment_for(name).file?
237: end
238:
239: validates_each(name) do |record, attr, value|
240: attachment = record.attachment_for(name)
241: attachment.send(:flush_errors) unless attachment.valid?
242: end
243: end
Places ActiveRecord-style validations on the content type of the file assigned. The possible options are:
NOTE: If you do not specify an [attachment]_content_type field on your model, content_type validation will work _ONLY upon assignment_ and re-validation after the instance has been reloaded will always succeed.
# File lib/paperclip.rb, line 302
302: def validates_attachment_content_type name, options = {}
303: attachment_definitions[name][:validations] << [:content_type, {:content_type => options[:content_type],
304: :if => options[:if],
305: :unless => options[:unless]}]
306: end
Places ActiveRecord-style validations on the presence of a file. Options:
# File lib/paperclip.rb, line 279
279: def validates_attachment_presence name, options = {}
280: message = options[:message] || "must be set."
281: attachment_definitions[name][:validations] << [:presence, {:message => message,
282: :if => options[:if],
283: :unless => options[:unless]}]
284: end
Places ActiveRecord-style validations on the size of the file assigned. The possible options are:
# File lib/paperclip.rb, line 254
254: def validates_attachment_size name, options = {}
255: min = options[:greater_than] || (options[:in] && options[:in].first) || 0
256: max = options[:less_than] || (options[:in] && options[:in].last) || (1.0/0)
257: range = (min..max)
258: message = options[:message] || "file size must be between :min and :max bytes."
259:
260: attachment_definitions[name][:validations] << [:size, {:range => range,
261: :message => message,
262: :if => options[:if],
263: :unless => options[:unless]}]
264: end
Adds errors if thumbnail creation fails. The same as specifying :whiny_thumbnails => true.
# File lib/paperclip.rb, line 267
267: def validates_attachment_thumbnails name, options = {}
268: warn('[DEPRECATION] validates_attachment_thumbnail is deprecated. ' +
269: 'This validation is on by default and will be removed from future versions. ' +
270: 'If you wish to turn it off, supply :whiny => false in your definition.')
271: attachment_definitions[name][:whiny_thumbnails] = true
272: end