lib/dm-paperclip.rb in dm-paperclip-2.1.2.1 vs lib/dm-paperclip.rb in dm-paperclip-2.1.4

- old
+ new

@@ -35,11 +35,11 @@ # Only include validations if dm-validations is loaded require File.join(File.dirname(__FILE__), 'dm-paperclip', 'validations') unless defined?(DataMapper::Validate).nil? module Paperclip - VERSION = "2.1.2.1" + VERSION = "2.1.4" class << self # Provides configurability to Paperclip. There are a number of options available, such as: # * whiny_thumbnails: Will raise an error if Paperclip cannot process thumbnails of # an uploaded image. Defaults to true. # * image_magick_path: Defines the path at which to find the +convert+ and +identify+ @@ -64,16 +64,25 @@ class NotIdentifiedByImageMagickError < PaperclipError #:nodoc: end module Resource def self.included(base) + base.class_eval <<-RUBY, __FILE__, __LINE__ + 1 + class_variable_set(:@@attachment_definitions,nil) unless class_variable_defined?(:@@attachment_definitions) + def self.attachment_definitions + @@attachment_definitions + end + + def self.attachment_definitions=(obj) + @@attachment_definitions = obj + end + RUBY base.extend Paperclip::ClassMethods end end module ClassMethods - @@attachment_definitions = {} # +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 @@ -104,34 +113,44 @@ # * +default_style+: The thumbnail style that will be used by default URLs. # Defaults to +original+. # has_attached_file :avatar, :styles => { :normal => "100x100#" }, # :default_style => :normal # user.avatar.url # => "/avatars/23/normal_me.png" - # * +path+: The location of the repository of attachments on disk. This can be coordinated - # with the value of the +url+ option to allow files to be saved into a place where Apache - # can serve them without hitting your app. Defaults to - # ":merb_root/public/:class/:attachment/:id/:style_:filename". - # By default this places the files in the app's public directory which can be served - # directly. If you are using capistrano for deployment, a good idea would be to - # make a symlink to the capistrano-created system directory from inside your app's - # public directory. - # See Paperclip::Attachment#interpolate for more information on variable interpolaton. - # :path => "/var/app/attachments/:class/:id/:style/:filename" # * +whiny_thumbnails+: Will raise an error if Paperclip cannot process thumbnails of an # uploaded image. This will ovrride the global setting for this attachment. # Defaults to true. + # * +convert_options+: When creating thumbnails, use this free-form options + # field to pass in various convert command options. Typical options are "-strip" to + # remove all Exif data from the image (save space for thumbnails and avatars) or + # "-depth 8" to specify the bit depth of the resulting conversion. See ImageMagick + # convert documentation for more options: (http://www.imagemagick.org/script/convert.php) + # Note that this option takes a hash of options, each of which correspond to the style + # of thumbnail being generated. You can also specify :all as a key, which will apply + # to all of the thumbnails being generated. If you specify options for the :original, + # it would be best if you did not specify destructive options, as the intent of keeping + # the original around is to regenerate all the thumbnails then requirements change. + # has_attached_file :avatar, :styles => { :large => "300x300", :negative => "100x100" } + # :convert_options => { + # :all => "-strip", + # :negative => "-negate" + # } + # * +storage+: Chooses the storage backend where the files will be stored. The current + # choices are :filesystem and :s3. The default is :filesystem. Make sure you read the + # documentation for Paperclip::Storage::Filesystem and Paperclip::Storage::S3 + # for backend-specific options. def has_attached_file name, options = {} include InstanceMethods - @@attachment_definitions = {} if @@attachment_definitions.nil? - @@attachment_definitions[name] = {:validations => []}.merge(options) + self.attachment_definitions = {} if self.attachment_definitions.nil? + self.attachment_definitions[name] = {:validations => []}.merge(options) property_options = options.delete_if { |k,v| ![ :public, :protected, :private, :accessor, :reader, :writer ].include?(key) } property "#{name}_file_name".to_sym, String, property_options property "#{name}_content_type".to_sym, String, property_options property "#{name}_file_size".to_sym, Integer, property_options + property "#{name}_updated_at".to_sym, DateTime, property_options after :save, :save_attached_files before :destroy, :destroy_attached_files define_method name do |*args| @@ -165,11 +184,11 @@ add_validator_to_context(opts, fields, Paperclip::Validate::SizeValidator) end # Adds errors if thumbnail creation fails. The same as specifying :whiny_thumbnails => true. def validates_attachment_thumbnails name, options = {} - @@attachment_definitions[name][:whiny_thumbnails] = true + self.attachment_definitions[name][:whiny_thumbnails] = true end # Places ActiveRecord-style validations on the presence of a file. def validates_attachment_presence(*fields) opts = opts_from_validator_args(fields) @@ -185,15 +204,10 @@ add_validator_to_context(opts, fields, Paperclip::Validate::ContentTypeValidator) end end - # Returns the attachment definitions defined by each call to has_attached_file. - def attachment_definitions - @@attachment_definitions - end - end module InstanceMethods #:nodoc: def attachment_for name @attachments ||= {} @@ -205,15 +219,17 @@ yield(name, attachment_for(name)) end end def save_attached_files + #logger.info("[paperclip] Saving attachments.") each_attachment do |name, attachment| attachment.send(:save) end end def destroy_attached_files + #logger.info("[paperclip] Deleting attachments.") each_attachment do |name, attachment| attachment.send(:queue_existing_for_delete) attachment.send(:flush_deletes) end end