0
+# Paperclip allows file attachments that are stored in the filesystem. All graphical
0
+# transformations are done using the Graphics/ImageMagick command line utilities and
0
+# are stored in Tempfiles until the record is saved. Paperclip does not require a
0
+# separate model for storing the attachment's information, instead adding a few simple
0
+# columns to your table.
0
+# Copyright:: Copyright (c) 2008 thoughtbot, inc.
0
+# License:: MIT License (http://www.opensource.org/licenses/mit-license.php)
0
+# Paperclip defines an attachment as any file, though it makes special considerations
0
+# for image files. You can declare that a model has an attached file with the
0
+# +has_attached_file+ method:
0
+# class User < ActiveRecord::Base
0
+# has_attached_file :avatar, :styles => { :thumb => "100x100" }
0
+# user.avatar = params[:user][:avatar]
0
+# # => "/users/avatars/4/original_me.jpg"
0
+# user.avatar.url(:thumb)
0
+# # => "/users/avatars/4/thumb_me.jpg"
0
+# See the +has_attached_file+ documentation for more details.
0
+require 'paperclip/upfile'
0
+require 'paperclip/iostream'
0
+require 'paperclip/geometry'
0
+require 'paperclip/thumbnail'
0
+require 'paperclip/storage'
0
+require 'paperclip/attachment'
0
+# The base module that gets included in ActiveRecord::Base.
0
+ # Provides configurability to Paperclip. There are a number of options available, such as:
0
+ # * whiny_thumbnails: Will raise an error if Paperclip cannot process thumbnails of
0
+ # an uploaded image. Defaults to true.
0
+ # * image_magick_path: Defines the path at which to find the +convert+ and +identify+
0
+ # programs if they are not visible to Rails the system's search path. Defaults to
0
+ # nil, which uses the first executable found in the search path.
0
+ :whiny_thumbnails => true,
0
+ :image_magick_path => nil
0
+ def path_for_command command #:nodoc:
0
+ path = [options[:image_magick_path], command].compact
0
+ def included base #:nodoc:
0
+ base.extend ClassMethods
0
+ class PaperclipError < StandardError #:nodoc:
0
+ # +has_attached_file+ gives the class it is called on an attribute that maps to a file. This
0
+ # is typically a file stored somewhere on the filesystem and has been uploaded by a user.
0
+ # The attribute returns a Paperclip::Attachment object which handles the management of
0
+ # that file. The intent is to make the attachment as much like a normal attribute. The
0
+ # thumbnails will be created when the new file is assigned, but they will *not* be saved
0
+ # until +save+ is called on the record. Likewise, if the attribute is set to +nil+ is
0
+ # called on it, the attachment will *not* be deleted until +save+ is called. See the
0
+ # Paperclip::Attachment documentation for more specifics. There are a number of options
0
+ # you can set to change the behavior of a Paperclip attachment:
0
+ # * +url+: The full URL of where the attachment is publically accessible. This can just
0
+ # as easily point to a directory served directly through Apache as it can to an action
0
+ # that can control permissions. You can specify the full domain and path, but usually
0
+ # just an absolute path is sufficient. The leading slash must be included manually for
0
+ # absolute paths. The default value is "/:class/:attachment/:id/:style_:filename". See
0
+ # Paperclip::Attachment#interpolate for more information on variable interpolaton.
0
+ # :url => "/:attachment/:id/:style_:basename:extension"
0
+ # :url => "http://some.other.host/stuff/:class/:id_:extension"
0
+ # * +default_url+: The URL that will be returned if there is no attachment assigned.
0
+ # This field is interpolated just as the url is. The default value is
0
+ # "/:class/:attachment/missing_:style.png"
0
+ # has_attached_file :avatar, :default_url => "/images/default_:style_avatar.png"
0
+ # User.new.avatar_url(:small) # => "/images/default_small_avatar.png"
0
+ # * +styles+: A hash of thumbnail styles and their geometries. You can find more about
0
+ # geometry strings at the ImageMagick website
0
+ # (http://www.imagemagick.org/script/command-line-options.php#resize). Paperclip
0
+ # also adds the "#" option (e.g. "50x50#"), which will resize the image to fit maximally
0
+ # inside the dimensions and then crop the rest off (weighted at the center). The
0
+ # default value is to generate no thumbnails.
0
+ # * +default_style+: The thumbnail style that will be used by default URLs.
0
+ # Defaults to +original+.
0
+ # has_attached_file :avatar, :styles => { :normal => "100x100#" },
0
+ # :default_style => :normal
0
+ # user.avatar.url # => "/avatars/23/normal_me.png"
0
+ # * +path+: The location of the repository of attachments on disk. This can be coordinated
0
+ # with the value of the +url+ option to allow files to be saved into a place where Apache
0
+ # can serve them without hitting your app. Defaults to
0
+ # ":rails_root/public/:class/:attachment/:id/:style_:filename".
0
+ # By default this places the files in the app's public directory which can be served
0
+ # directly. If you are using capistrano for deployment, a good idea would be to
0
+ # make a symlink to the capistrano-created system directory from inside your app's
0
+ # See Paperclip::Attachment#interpolate for more information on variable interpolaton.
0
+ # :path => "/var/app/attachments/:class/:id/:style/:filename"
0
+ # * +whiny_thumbnails+: Will raise an error if Paperclip cannot process thumbnails of an
0
+ # uploaded image. This will ovrride the global setting for this attachment.
0
+ def has_attached_file name, options = {}
0
+ include InstanceMethods
0
+ write_inheritable_attribute(:attachment_definitions, {}) if attachment_definitions.nil?
0
+ attachment_definitions[name] = {:validations => []}.merge(options)
0
+ after_save :save_attached_files
0
+ before_destroy :destroy_attached_files
0
+ define_method name do |*args|
0
+ a = attachment_for(name)
0
+ (args.length > 0) ? a.to_s(args.first) : a
0
+ define_method "#{name}=" do |file|
0
+ attachment_for(name).assign(file)
0
+ define_method "#{name}?" do
0
+ ! attachment_for(name).file.nil?
0
+ validates_each(name) do |record, attr, value|
0
+ value.send(:flush_errors)
0
+ # Places ActiveRecord-style validations on the size of the file assigned. The
0
+ # possible options are:
0
+ # * +in+: a Range of bytes (i.e. +1..1.megabyte+),
0
+ # * +less_than+: equivalent to :in => 0..options[:less_than]
0
+ # * +greater_than+: equivalent to :in => options[:greater_than]..Infinity
0
+ def validates_attachment_size name, options = {}
0
+ attachment_definitions[name][:validations] << lambda do |attachment, instance|
0
+ unless options[:greater_than].nil?
0
+ options[:in] = (options[:greater_than]..(1/0)) # 1/0 => Infinity
0
+ unless options[:less_than].nil?
0
+ options[:in] = (0..options[:less_than])
0
+ unless options[:in].include? instance[:"#{name}_file_size"].to_i
0
+ "file size is not between #{options[:in].first} and #{options[:in].last} bytes."
0
+ # Places ActiveRecord-style validations on the presence of a file.
0
+ def validates_attachment_presence name
0
+ attachment_definitions[name][:validations] << lambda do |attachment, instance|
0
+ if attachment.file.nil? || !File.exist?(attachment.file.path)
0
+ # Returns the attachment definitions defined by each call to has_attached_file.
0
+ def attachment_definitions
0
+ read_inheritable_attribute(:attachment_definitions)
0
+ module InstanceMethods #:nodoc:
0
+ def attachment_for name
0
+ @attachments[name] ||= Attachment.new(name, self, self.class.attachment_definitions[name])
0
+ self.class.attachment_definitions.each do |name, definition|
0
+ yield(name, attachment_for(name))
0
+ def save_attached_files
0
+ each_attachment do |name, attachment|
0
+ attachment.send(:save)
0
+ def destroy_attached_files
0
+ each_attachment do |name, attachment|
0
+ attachment.send(:queue_existing_for_delete)
0
+ attachment.send(:flush_deletes)