Commit 02bd35fe by Mike Burns

Pull the file setter into a separate object

parent dad6086e
...@@ -51,6 +51,7 @@ require 'paperclip/missing_attachment_styles' ...@@ -51,6 +51,7 @@ require 'paperclip/missing_attachment_styles'
require 'paperclip/validators' require 'paperclip/validators'
require 'paperclip/logger' require 'paperclip/logger'
require 'paperclip/helpers' require 'paperclip/helpers'
require 'paperclip/has_attached_file'
require 'paperclip/tasks/attachments' require 'paperclip/tasks/attachments'
require 'mime/types' require 'mime/types'
require 'logger' require 'logger'
...@@ -173,17 +174,24 @@ module Paperclip ...@@ -173,17 +174,24 @@ module Paperclip
# end # end
# end # end
def has_attached_file(name, options = {}) def has_attached_file(name, options = {})
HasAttachedFile.new(name, options).define_on(self)
options = Paperclip::AttachmentOptions.new(options) options = Paperclip::AttachmentOptions.new(options)
Paperclip.classes_with_attachments << self.name Paperclip.classes_with_attachments << self.name
Paperclip.check_for_path_clash(name, options[:path], self.name) Paperclip.check_for_path_clash(name, options[:path], self.name)
Paperclip::Tasks::Attachments.add(self, name, options)
after_save { send(name).send(:save) } after_save { send(name).send(:save) }
before_destroy { send(name).send(:queue_all_for_delete) } before_destroy { send(name).send(:queue_all_for_delete) }
after_destroy { send(name).send(:flush_deletes) } after_destroy { send(name).send(:flush_deletes) }
define_paperclip_callbacks :post_process, :"#{name}_post_process" define_paperclip_callbacks :post_process, :"#{name}_post_process"
Paperclip::Tasks::Attachments.add(self, name, options) validates_each(name) do |record, attr, value|
attachment = record.send(name)
attachment.send(:flush_errors)
end
define_method name do |*args| define_method name do |*args|
ivar = "@attachment_#{name}" ivar = "@attachment_#{name}"
...@@ -201,18 +209,9 @@ module Paperclip ...@@ -201,18 +209,9 @@ module Paperclip
end end
end end
define_method "#{name}=" do |file|
send(name).assign(file)
end
define_method "#{name}?" do define_method "#{name}?" do
send(name).file? send(name).file?
end end
validates_each(name) do |record, attr, value|
attachment = record.send(name)
attachment.send(:flush_errors)
end
end end
end end
end end
......
module Paperclip
class HasAttachedFile
def initialize(name, options)
@name = name
end
def define_on(klass)
name = @name
klass.send :define_method, "#{@name}=" do |file|
send(name).assign(file)
end
end
end
end
require './test/helper'
require 'paperclip/has_attached_file'
class HasAttachedFileTest < Test::Unit::TestCase
context '#define_on' do
should 'define a setter on the class object' do
a_class = stub('class', define_method: nil)
has_attached_file = Paperclip::HasAttachedFile.new(:avatar, {})
has_attached_file.define_on(a_class)
assert_received(a_class, :define_method) do |expect|
expect.with('avatar=')
end
end
end
end
require './test/helper' require './test/helper'
require 'tasks/attachments' require 'paperclip/tasks/attachments'
class AttachmentsTest < Test::Unit::TestCase class AttachmentsTest < Test::Unit::TestCase
context '.names_for' do context '.names_for' 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