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' ...@@ -49,7 +49,6 @@ require 'paperclip/glue'
require 'paperclip/errors' require 'paperclip/errors'
require 'paperclip/missing_attachment_styles' require 'paperclip/missing_attachment_styles'
require 'paperclip/validators' require 'paperclip/validators'
require 'paperclip/instance_methods'
require 'paperclip/logger' require 'paperclip/logger'
require 'paperclip/helpers' require 'paperclip/helpers'
require 'mime/types' require 'mime/types'
...@@ -173,38 +172,47 @@ module Paperclip ...@@ -173,38 +172,47 @@ module Paperclip
# end # end
# end # end
def has_attached_file(name, options = {}) def has_attached_file(name, options = {})
include InstanceMethods
if attachment_definitions.nil? if attachment_definitions.nil?
self.attachment_definitions = {} self.attachment_definitions = {}
else else
self.attachment_definitions = self.attachment_definitions.dup self.attachment_definitions = self.attachment_definitions.dup
end end
attachment_definitions[name] = Paperclip::AttachmentOptions.new(options) options = attachment_definitions[name] = Paperclip::AttachmentOptions.new(options)
Paperclip.classes_with_attachments << self.name Paperclip.classes_with_attachments << self.name
after_save :save_attached_files after_save { send(name).send(:save) }
before_destroy :prepare_for_destroy before_destroy { send(name).send(:queue_all_for_delete) }
after_destroy :destroy_attached_files after_destroy { send(name).send(:flush_deletes) }
define_paperclip_callbacks :post_process, :"#{name}_post_process" define_paperclip_callbacks :post_process, :"#{name}_post_process"
define_method name do |*args| define_method name do |*args|
a = attachment_for(name) ivar = "@attachment_#{name}"
(args.length > 0) ? a.to_s(args.first) : a 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 end
define_method "#{name}=" do |file| define_method "#{name}=" do |file|
attachment_for(name).assign(file) send(name).assign(file)
end end
define_method "#{name}?" do define_method "#{name}?" do
attachment_for(name).file? send(name).file?
end end
validates_each(name) do |record, attr, value| validates_each(name) do |record, attr, value|
attachment = record.attachment_for(name) attachment = record.send(name)
attachment.send(:flush_errors) attachment.send(:flush_errors)
end end
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 ...@@ -20,7 +20,7 @@ module Paperclip
def matches? subject def matches? subject
@subject = subject @subject = subject
@subject = @subject.class unless Class === @subject @subject = @subject.class unless Class === @subject
responds? && has_column? && included? responds? && has_column?
end end
def failure_message def failure_message
...@@ -47,10 +47,6 @@ module Paperclip ...@@ -47,10 +47,6 @@ module Paperclip
def has_column? def has_column?
@subject.column_names.include?("#{@attachment_name}_file_name") @subject.column_names.include?("#{@attachment_name}_file_name")
end end
def included?
@subject.ancestors.include?(Paperclip::InstanceMethods)
end
end end
end end
end end
......
...@@ -709,7 +709,7 @@ class S3Test < Test::Unit::TestCase ...@@ -709,7 +709,7 @@ class S3Test < Test::Unit::TestCase
setup do setup do
AWS::S3::S3Object.any_instance.stubs(:exists?).returns(true) AWS::S3::S3Object.any_instance.stubs(:exists?).returns(true)
AWS::S3::S3Object.any_instance.stubs(:delete) AWS::S3::S3Object.any_instance.stubs(:delete)
@dummy.destroy_attached_files @dummy.destroy
end end
should "succeed" do 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