Commit d8a721f6 by Mike Burns

Move validates_each into HasAttachedFile class

parent 2d28dbca
...@@ -187,11 +187,6 @@ module Paperclip ...@@ -187,11 +187,6 @@ module Paperclip
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"
validates_each(name) do |record, attr, value|
attachment = record.send(name)
attachment.send(:flush_errors)
end
end end
end end
end end
......
...@@ -11,6 +11,7 @@ module Paperclip ...@@ -11,6 +11,7 @@ module Paperclip
end end
def define def define
define_flush_errors
define_getter define_getter
define_setter define_setter
define_query define_query
...@@ -18,6 +19,13 @@ module Paperclip ...@@ -18,6 +19,13 @@ module Paperclip
private private
def define_flush_errors
@klass.send(:validates_each, @name) do |record, attr, value|
attachment = record.send(@name)
attachment.send(:flush_errors)
end
end
def define_getter def define_getter
name = @name name = @name
options = @options options = @options
......
...@@ -14,6 +14,10 @@ class HasAttachedFileTest < Test::Unit::TestCase ...@@ -14,6 +14,10 @@ class HasAttachedFileTest < Test::Unit::TestCase
should 'define a query on the class object' do should 'define a query on the class object' do
assert_adding_attachment('avatar').defines_method('avatar?') assert_adding_attachment('avatar').defines_method('avatar?')
end end
should 'flush errors as part of validations' do
assert_adding_attachment('avatar').defines_validation
end
end end
private private
...@@ -31,7 +35,7 @@ class HasAttachedFileTest < Test::Unit::TestCase ...@@ -31,7 +35,7 @@ class HasAttachedFileTest < Test::Unit::TestCase
end end
def defines_method(method_name) def defines_method(method_name)
a_class = stub('class', define_method: nil) a_class = stub_class
Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {}) Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {})
...@@ -39,5 +43,21 @@ class HasAttachedFileTest < Test::Unit::TestCase ...@@ -39,5 +43,21 @@ class HasAttachedFileTest < Test::Unit::TestCase
expect.with(method_name) expect.with(method_name)
end end
end end
def defines_validation
a_class = stub_class
Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {})
assert_received(a_class, :validates_each) do |expect|
expect.with(@attachment_name)
end
end
private
def stub_class
stub('class', validates_each: nil, define_method: nil)
end
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