Commit 40bdd3d5 by Mike Burns and George Brocklehurst Committed by Mike Burns

Remove the InstanceMethods and reduce class-level state

parent 1da9e4d6
......@@ -49,7 +49,6 @@ require 'paperclip/glue'
require 'paperclip/errors'
require 'paperclip/missing_attachment_styles'
require 'paperclip/validators'
require 'paperclip/instance_methods'
require 'paperclip/logger'
require 'paperclip/helpers'
require 'mime/types'
......@@ -173,38 +172,47 @@ module Paperclip
# end
# end
def has_attached_file(name, options = {})
include InstanceMethods
if attachment_definitions.nil?
self.attachment_definitions = {}
else
self.attachment_definitions = self.attachment_definitions.dup
end
attachment_definitions[name] = Paperclip::AttachmentOptions.new(options)
options = attachment_definitions[name] = Paperclip::AttachmentOptions.new(options)
Paperclip.classes_with_attachments << self.name
after_save :save_attached_files
before_destroy :prepare_for_destroy
after_destroy :destroy_attached_files
after_save { send(name).send(:save) }
before_destroy { send(name).send(:queue_all_for_delete) }
after_destroy { send(name).send(:flush_deletes) }
define_paperclip_callbacks :post_process, :"#{name}_post_process"
define_method name do |*args|
a = attachment_for(name)
(args.length > 0) ? a.to_s(args.first) : a
ivar = "@attachment_#{name}"
attachment = instance_variable_get(ivar)
if attachment.nil?
attachment = Attachment.new(name, self, options)
instance_variable_set(ivar, attachment)
end
if args.length > 0
attachment.to_s(args.first)
else
attachment
end
end
define_method "#{name}=" do |file|
attachment_for(name).assign(file)
send(name).assign(file)
end
define_method "#{name}?" do
attachment_for(name).file?
send(name).file?
end
validates_each(name) do |record, attr, value|
attachment = record.attachment_for(name)
attachment = record.send(name)
attachment.send(:flush_errors)
end
end
......
module Paperclip
module InstanceMethods #:nodoc:
def attachment_for name
@_paperclip_attachments ||= {}
@_paperclip_attachments[name] ||= Attachment.new(name, self, attachment_definitions[name])
end
def each_attachment
self.attachment_definitions.each do |name, definition|
yield(name, attachment_for(name))
end
end
def save_attached_files
Paperclip.log("Saving attachments.")
each_attachment do |name, attachment|
attachment.send(:save)
end
end
def destroy_attached_files
Paperclip.log("Deleting attachments.")
each_attachment do |name, attachment|
attachment.send(:flush_deletes)
end
end
def prepare_for_destroy
Paperclip.log("Scheduling attachments for deletion.")
each_attachment do |name, attachment|
attachment.send(:queue_all_for_delete)
end
end
end
end
......@@ -20,7 +20,7 @@ module Paperclip
def matches? subject
@subject = subject
@subject = @subject.class unless Class === @subject
responds? && has_column? && included?
responds? && has_column?
end
def failure_message
......@@ -47,10 +47,6 @@ module Paperclip
def has_column?
@subject.column_names.include?("#{@attachment_name}_file_name")
end
def included?
@subject.ancestors.include?(Paperclip::InstanceMethods)
end
end
end
end
......
......@@ -709,7 +709,7 @@ class S3Test < Test::Unit::TestCase
setup do
AWS::S3::S3Object.any_instance.stubs(:exists?).returns(true)
AWS::S3::S3Object.any_instance.stubs(:delete)
@dummy.destroy_attached_files
@dummy.destroy
end
should "succeed" do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment