Commit ceb43f1a by Jon Yurek

Reintroduce attachment_definitions

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