0
@@ -133,11 +133,11 @@ module Thoughtbot #:nodoc:
0
# == Model Requirements
0
# For any given attachment _foo_, the model the attachment is in needs to have both a +foo_file_name+
0
- # and +foo_content_type+ column, as a type of +string+. The +foo_file_name+ column contains only the name
0
+ # and +foo_content_type+ column, as a type of +string+, and a +foo_file_size+ column as type +integer+.
0
+ # The +foo_file_name+ column contains only the name
0
# of the file and none of the path information. However, the +foo_file_name+ column accessor is overwritten
0
# by the one (defined above) which returns the full path to whichever style thumbnail is passed in.
0
- # In a pinch, you can either use +read_attribute+ or the plain +foo+ accessor, which returns the database's
0
- # +foo_file_name+ column.
0
+ # To access the name as stored in the database, you can use Attachment#original_filename.
0
# When an attachment is set by using he setter (+model.attachment=+), the thumbnails are created and held in
0
@@ -154,7 +154,10 @@ module Thoughtbot #:nodoc:
0
attr_accessor :attachment_definitions
0
include InstanceMethods
0
+ after_save :save_attachments
0
+ before_destroy :destroy_attachments
0
validates_each(*attachment_names) do |record, attr, value|
0
value.errors.each{|e| record.errors.add(attr, e) unless record.errors.on(attr) && record.errors.on(attr).include?(e) }
0
@@ -191,18 +194,6 @@ module Thoughtbot #:nodoc:
0
define_method "destroy_#{name}" do |*args|
0
attachment_for(name).queue_destroy(args.first)
0
- define_method "#{name}_after_save" do
0
- attachment_for(name).save
0
- private :"#{name}_after_save"
0
- after_save :"#{name}_after_save"
0
- define_method "#{name}_before_destroy" do
0
- attachment_for(name).destroy
0
- private :"#{name}_before_destroy"
0
- before_destroy :"#{name}_before_destroy"
0
@@ -221,6 +212,18 @@ module Thoughtbot #:nodoc:
0
alias_method_chain :after_initialize, :paperclip
0
+ @attachments.each do |name, attachment|
0
+ def destroy_attachments
0
+ @attachments.each do |name, attachment|
0
def attachment_for name
0
@@ -230,17 +233,27 @@ module Thoughtbot #:nodoc:
0
@attachment_definitions.keys
0
+ # Paperclip always validates whether or not file creation was successful, but does not validate
0
+ # the presence or size of the file unless told. You can specify validations either in the
0
+ # has_attached_file call or with a separate validates_attached_file call, with a syntax similar
0
+ # to has_attached_file. If no options are given, the existence of the file is validated.
0
+ # validates_attached_file :avatar, :existence => true, :size => 0..(500.kilobytes)
0
def validates_attached_file *attachment_names
0
+ options = attachment_names.pop if attachment_names.last.is_a? Hash
0
+ options ||= { :existence => true }
0
attachment_names.each do |name|
0
- @attachment_definitions[name].validate :existence
0
+ options.each do |key, value|
0
+ @attachment_definitions[name].validate key, value
0
def whine_about_columns_for name #:nodoc:
0
- [ "#{name}_file_name", "#{name}_content_type", "#{name}_
size" ].each do |column|
0
+ [ "#{name}_file_name", "#{name}_content_type", "#{name}_
file_size" ].each do |column|
0
unless column_names.include?(column)
0
raise PaperclipError, "Class #{self.name} does not have all of the necessary columns to have an attachment named #{name}. " +
0
- "(#{name}_file_name, #{name}_content_type, and #{name}_
size)"
0
+ "(#{name}_file_name, #{name}_content_type, and #{name}_
file_size)"
0
@@ -265,12 +278,20 @@ module Thoughtbot #:nodoc:
0
# * delete_attachment: Delete the files and thumbnails from the storage medium. Should return true or false
0
# depending on success.
0
- # When writing files, the @files variable will hold a hash of style names and their data. If @files is nil,
0
- # then no new data has been assigned to the attachment and you should not perform any work.
0
- # You will also have access to @definition, which is the AttachmentDefintion object for the attachment. The
0
- # methods in your module will be mixed into an Attachment instance, so you have full access to the
0
+ # When writing a storage system, your code will be mixed into the Attachment that the file represents. You
0
+ # will therefore have access to all of the methods available to Attachments. Some methods of note are:
0
+ # * definition: Returns the AttachmentDefintion object created by has_attached_file. Useful for getting
0
+ # style definitions and flags that you want to set. You should open the AttachmentDefinition class to
0
+ # add getters for any options you want to be able to set.
0
+ # * instance: Returns the ActiveRecord object that the Attachment is attached to. Can be used to obtain ids.
0
+ # * original_filename: Returns the original_filename, which is the same as the attachment_file_name column
0
+ # in the database. Should be nil if there is no attachment.
0
+ # * original_file_size: Returns the size of the original file, as passed to Attachment#assign.
0
+ # * interpolate: Given a style and a pattern, this will interpolate variables like :rails_root, :name,
0
+ # and :id. See documentation for has_attached_file for more info on interpolation.
0
+ # * for_attached_files: Iterates over the collection of files for this attachment, passing the style name
0
+ # and the data to the block. Will not call the block if the data is nil.
0
+ # * dirty?: Returns true if a new file has been assigned with Attachment#assign, false otherwise.
0
# Storage systems provide their own validations, since the manner of checking the status of them is usually
Comments
No one has commented yet.