Commit eadd4527 by Alan Ho Committed by Mike Burns

Reduce class-oriented programming to help metaclasses

parent eb928346
......@@ -2,11 +2,11 @@ module Paperclip
module InstanceMethods #:nodoc:
def attachment_for name
@_paperclip_attachments ||= {}
@_paperclip_attachments[name] ||= Attachment.new(name, self, self.class.attachment_definitions[name])
@_paperclip_attachments[name] ||= Attachment.new(name, self, attachment_definitions[name])
end
def each_attachment
self.class.attachment_definitions.each do |name, definition|
self.attachment_definitions.each do |name, definition|
yield(name, attachment_for(name))
end
end
......
......@@ -103,6 +103,13 @@ def rebuild_class options = {}
end
end
def rebuild_meta_class_of obj, options = {}
(class << obj; self; end).tap do |metaklass|
metaklass.has_attached_file :avatar, options
Paperclip.reset_duplicate_clash_check!
end
end
class FakeModel
attr_accessor :avatar_file_name,
:avatar_file_size,
......
require './test/helper'
class MetaClassTest < Test::Unit::TestCase
context "A meta-class of dummy" do
setup do
rebuild_model
@file = File.new(fixture_file("5k.png"), 'rb')
end
teardown { @file.close }
should "be able to use Paperclip like a normal class" do
reset_class("Dummy")
@dummy = Dummy.new
assert_nothing_raised do
rebuild_meta_class_of(@dummy)
end
end
should "work like any other instance" do
reset_class("Dummy")
@dummy = Dummy.new
rebuild_meta_class_of(@dummy)
assert_nothing_raised do
@dummy.avatar = @file
end
assert @dummy.save
end
end
end
\ No newline at end of file
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