Commit 77d8de02 by Jon Yurek

Calling #valid? on the model corrctly runs validations on the attachment.…

Calling #valid? on the model corrctly runs validations on the attachment. Calling #valid? on the attachment correctly runs validations instead of reporting only on current status.
parent 0effaa64
...@@ -133,7 +133,7 @@ module Paperclip ...@@ -133,7 +133,7 @@ module Paperclip
end end
validates_each(name) do |record, attr, value| validates_each(name) do |record, attr, value|
value.send(:flush_errors) value.send(:flush_errors) unless value.valid?
end end
end end
......
...@@ -93,6 +93,7 @@ module Paperclip ...@@ -93,6 +93,7 @@ module Paperclip
# Returns true if there are any errors on this attachment. # Returns true if there are any errors on this attachment.
def valid? def valid?
validate
errors.length == 0 errors.length == 0
end end
...@@ -183,6 +184,7 @@ module Paperclip ...@@ -183,6 +184,7 @@ module Paperclip
end.flatten.compact.uniq end.flatten.compact.uniq
@errors += @validation_errors @errors += @validation_errors
end end
@validation_errors
end end
def normalize_style_definition def normalize_style_definition
......
...@@ -64,6 +64,38 @@ class PaperclipTest < Test::Unit::TestCase ...@@ -64,6 +64,38 @@ class PaperclipTest < Test::Unit::TestCase
assert Dummy.new.respond_to?(:avatar=) assert Dummy.new.respond_to?(:avatar=)
end end
context "that is valid" do
setup do
@dummy = Dummy.new
@dummy.avatar = @file
end
should "be valid" do
assert @dummy.valid?
end
context "then has a validation added that makes it invalid" do
setup do
assert @dummy.save
Dummy.class_eval do
validates_attachment_content_type :avatar, :content_type => ["text/plain"]
end
@dummy2 = Dummy.find(@dummy.id)
end
should "be invalid when reloaded" do
assert ! @dummy2.valid?, @dummy2.errors.inspect
end
should "be able to call #valid? twice without having duplicate errors" do
@dummy2.avatar.valid?
first_errors = @dummy2.avatar.errors
@dummy2.avatar.valid?
assert_equal first_errors, @dummy2.avatar.errors
end
end
end
[[:presence, nil, "5k.png", nil], [[:presence, nil, "5k.png", nil],
[:size, {:in => 1..10240}, "5k.png", "12k.png"], [:size, {:in => 1..10240}, "5k.png", "12k.png"],
[:size2, {:in => 1..10240}, nil, "12k.png"], [:size2, {:in => 1..10240}, nil, "12k.png"],
...@@ -102,21 +134,6 @@ class PaperclipTest < Test::Unit::TestCase ...@@ -102,21 +134,6 @@ class PaperclipTest < Test::Unit::TestCase
assert_equal 1, @dummy.avatar.errors.length assert_equal 1, @dummy.avatar.errors.length
end end
end end
# context "and an invalid file with :message" do
# setup do
# @file = args[3] && File.new(File.join(FIXTURES_DIR, args[3]))
# end
#
# should "have errors" do
# if args[1] && args[1][:message] && args[4]
# @dummy.avatar = @file
# assert ! @dummy.avatar.valid?
# assert_equal 1, @dummy.avatar.errors.length
# assert_equal args[4], @dummy.avatar.errors[0]
# end
# end
# end
end 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