Permalink
Browse files

Move validates_each into HasAttachedFile class

  • Loading branch information...
1 parent bd8c08c commit 40337d1aea69ba947060a100dbe2bd72ed3bfcb3 @mike-burns mike-burns committed Jan 25, 2013
Showing with 29 additions and 6 deletions.
  1. +0 −5 lib/paperclip.rb
  2. +8 −0 lib/paperclip/has_attached_file.rb
  3. +21 −1 test/has_attached_file_test.rb
View
@@ -186,11 +186,6 @@ def has_attached_file(name, options = {})
after_destroy { send(name).send(:flush_deletes) }
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
@@ -11,13 +11,21 @@ def initialize(klass, name, options)
end
def define
+ define_flush_errors
define_getter
define_setter
define_query
end
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
name = @name
options = @options
@@ -14,6 +14,10 @@ class HasAttachedFileTest < Test::Unit::TestCase
should 'define a query on the class object' do
assert_adding_attachment('avatar').defines_method('avatar?')
end
+
+ should 'flush errors as part of validations' do
+ assert_adding_attachment('avatar').defines_validation
+ end
end
private
@@ -31,13 +35,29 @@ def initialize(attachment_name)
end
def defines_method(method_name)
- a_class = stub('class', define_method: nil)
+ a_class = stub_class
Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {})
assert_received(a_class, :define_method) do |expect|
expect.with(method_name)
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

0 comments on commit 40337d1

Please sign in to comment.