Commit ceb43f1a by Jon Yurek

Reintroduce attachment_definitions

parent f8cf74b9
......@@ -12,7 +12,7 @@ module Paperclip
def define
define_flush_errors
define_getter
define_getters
define_setter
define_query
register_new_attachment
......@@ -29,7 +29,12 @@ module Paperclip
end
end
def define_getter
def define_getters
define_instance_getter
define_class_getter
end
def define_instance_getter
name = @name
options = @options
......@@ -50,9 +55,12 @@ module Paperclip
end
end
def define_class_getter
@klass.extend(ClassMethods)
end
def define_setter
name = @name
@klass.send :define_method, "#{@name}=" do |file|
send(name).assign(file)
end
......@@ -60,7 +68,6 @@ module Paperclip
def define_query
name = @name
@klass.send :define_method, "#{@name}?" do
send(name).file?
end
......@@ -82,5 +89,11 @@ module Paperclip
:define_paperclip_callbacks,
:post_process, :"#{@name}_post_process")
end
module ClassMethods
def attachment_definitions
Paperclip::AttachmentRegistry.definitions_for(self.class)
end
end
end
end
......@@ -32,7 +32,7 @@ class AttachmentRegistryTest < Test::Unit::TestCase
should 'call the block with the class, attachment name, and options' do
foo = Class.new
expected_accumulations = [
[foo,:avatar, { yo: 'greeting' }],
[foo, :avatar, { yo: 'greeting' }],
[foo, :greeter, { ciao: 'greeting' }]
]
expected_accumulations.each do |args|
......
......@@ -15,6 +15,10 @@ class HasAttachedFileTest < Test::Unit::TestCase
assert_adding_attachment('avatar').defines_method('avatar?')
end
should 'define a method on the class to get all of its attachments' do
assert_adding_attachment('avatar').defines_class_method('attachment_definitions')
end
should 'flush errors as part of validations' do
assert_adding_attachment('avatar').defines_validation
end
......@@ -64,6 +68,17 @@ class HasAttachedFileTest < Test::Unit::TestCase
end
end
def defines_class_method(method_name)
a_class = stub_class
a_class.class.stubs(:define_method)
Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {})
assert_received(a_class, :extend) do |expect|
expect.with(Paperclip::HasAttachedFile::ClassMethods)
end
end
def defines_validation
a_class = stub_class
......@@ -103,6 +118,7 @@ class HasAttachedFileTest < Test::Unit::TestCase
before_destroy: nil,
after_destroy: nil,
define_paperclip_callbacks: nil,
extend: nil,
name: 'Billy')
end
end
......
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