Skip to content

Commit f59a589

Browse files
djcpJon Yurek
authored and
Jon Yurek
committed
Fixes validates_attachment_presence matcher, thoughtbot#1296
Copying attribute validation failures up to the root object breaks the validate_attachment_presence_matcher when other paperclip validations have failed. This fixes this bug.
1 parent 0d04ae1 commit f59a589

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/paperclip/matchers/validate_attachment_presence_matcher.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ def no_error_when_valid?
4646
@file = StringIO.new(".")
4747
@subject.send(@attachment_name).assign(@file)
4848
@subject.valid?
49-
@subject.errors[:"#{@attachment_name}"].blank?
49+
expected_message = [
50+
@attachment_name.to_s.titleize,
51+
I18n.t(:blank, scope: [:errors, :messages])
52+
].join(' ')
53+
@subject.errors.full_messages.exclude?(expected_message)
5054
end
5155
end
5256
end

test/matchers/validate_attachment_presence_matcher_test.rb

+22
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,28 @@ class ValidateAttachmentPresenceMatcherTest < Test::Unit::TestCase
2323
should_accept_dummy_class
2424
end
2525

26+
context "given an instance with other attachment validations" do
27+
setup do
28+
reset_table("dummies") do |d|
29+
d.string :avatar_file_name
30+
d.string :avatar_content_type
31+
end
32+
33+
@dummy_class.class_eval do
34+
validates_attachment_presence :avatar
35+
validates_attachment_content_type :avatar, :content_type => 'image/gif'
36+
end
37+
38+
@dummy = @dummy_class.new
39+
@matcher = self.class.validate_attachment_presence(:avatar)
40+
end
41+
42+
should "it should validate properly" do
43+
@dummy.avatar = File.new fixture_file('5k.png')
44+
assert_accepts @matcher, @dummy
45+
end
46+
end
47+
2648
context "using an :if to control the validation" do
2749
setup do
2850
@dummy_class.class_eval do

0 commit comments

Comments
 (0)