Commit d61b7069 by Tute Costa

Merge pull request #1910 from lime/content-type-matcher-nil

Fix a nil error in failure_message of content type validation matcher
parents 2c59f96f cef29db0
...@@ -2,6 +2,7 @@ master: ...@@ -2,6 +2,7 @@ master:
* README adjustments for Ruby beginners (add links, elucidate model in Quick Start) * README adjustments for Ruby beginners (add links, elucidate model in Quick Start)
* Bugfix: Now it's possible to save images from URLs with special characters [#1932] * Bugfix: Now it's possible to save images from URLs with special characters [#1932]
* Fix a nil error in content type validation matcher [#1910]
5.0.0.beta2 (2015-04-01): 5.0.0.beta2 (2015-04-01):
......
...@@ -40,9 +40,9 @@ module Paperclip ...@@ -40,9 +40,9 @@ module Paperclip
def failure_message def failure_message
"#{expected_attachment}\n".tap do |message| "#{expected_attachment}\n".tap do |message|
message << accepted_types_and_failures message << accepted_types_and_failures.to_s
message << "\n\n" if @allowed_types.present? && @rejected_types.present? message << "\n\n" if @allowed_types.present? && @rejected_types.present?
message << rejected_types_and_failures message << rejected_types_and_failures.to_s
end end
end end
...@@ -55,7 +55,7 @@ module Paperclip ...@@ -55,7 +55,7 @@ module Paperclip
def accepted_types_and_failures def accepted_types_and_failures
if @allowed_types.present? if @allowed_types.present?
"Accept content types: #{@allowed_types.join(", ")}\n".tap do |message| "Accept content types: #{@allowed_types.join(", ")}\n".tap do |message|
if @missing_allowed_types.any? if @missing_allowed_types.present?
message << " #{@missing_allowed_types.join(", ")} were rejected." message << " #{@missing_allowed_types.join(", ")} were rejected."
else else
message << " All were accepted successfully." message << " All were accepted successfully."
...@@ -66,7 +66,7 @@ module Paperclip ...@@ -66,7 +66,7 @@ module Paperclip
def rejected_types_and_failures def rejected_types_and_failures
if @rejected_types.present? if @rejected_types.present?
"Reject content types: #{@rejected_types.join(", ")}\n".tap do |message| "Reject content types: #{@rejected_types.join(", ")}\n".tap do |message|
if @missing_rejected_types.any? if @missing_rejected_types.present?
message << " #{@missing_rejected_types.join(", ")} were accepted." message << " #{@missing_rejected_types.join(", ")} were accepted."
else else
message << " All were rejected successfully." message << " All were rejected successfully."
......
...@@ -17,22 +17,26 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do ...@@ -17,22 +17,26 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do
it "rejects a class with no validation" do it "rejects a class with no validation" do
expect(matcher).to_not accept(Dummy) expect(matcher).to_not accept(Dummy)
expect { matcher.failure_message }.to_not raise_error
end end
it 'rejects a class when the validation fails' do it 'rejects a class when the validation fails' do
Dummy.validates_attachment_content_type :avatar, content_type: %r{audio/.*} Dummy.validates_attachment_content_type :avatar, content_type: %r{audio/.*}
expect(matcher).to_not accept(Dummy) expect(matcher).to_not accept(Dummy)
expect { matcher.failure_message }.to_not raise_error
end end
it "accepts a class with a matching validation" do it "accepts a class with a matching validation" do
Dummy.validates_attachment_content_type :avatar, content_type: %r{image/.*} Dummy.validates_attachment_content_type :avatar, content_type: %r{image/.*}
expect(matcher).to accept(Dummy) expect(matcher).to accept(Dummy)
expect { matcher.failure_message }.to_not raise_error
end end
it "accepts a class with other validations but matching types" do it "accepts a class with other validations but matching types" do
Dummy.validates_presence_of :title Dummy.validates_presence_of :title
Dummy.validates_attachment_content_type :avatar, content_type: %r{image/.*} Dummy.validates_attachment_content_type :avatar, content_type: %r{image/.*}
expect(matcher).to accept(Dummy) expect(matcher).to accept(Dummy)
expect { matcher.failure_message }.to_not raise_error
end end
it "accepts a class that matches and a matcher that only specifies 'allowing'" do it "accepts a class that matches and a matcher that only specifies 'allowing'" do
...@@ -40,6 +44,7 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do ...@@ -40,6 +44,7 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do
matcher = plain_matcher.allowing(%w(image/png image/jpeg)) matcher = plain_matcher.allowing(%w(image/png image/jpeg))
expect(matcher).to accept(Dummy) expect(matcher).to accept(Dummy)
expect { matcher.failure_message }.to_not raise_error
end end
it "rejects a class that does not match and a matcher that only specifies 'allowing'" do it "rejects a class that does not match and a matcher that only specifies 'allowing'" do
...@@ -47,6 +52,7 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do ...@@ -47,6 +52,7 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do
matcher = plain_matcher.allowing(%w(image/png image/jpeg)) matcher = plain_matcher.allowing(%w(image/png image/jpeg))
expect(matcher).to_not accept(Dummy) expect(matcher).to_not accept(Dummy)
expect { matcher.failure_message }.to_not raise_error
end end
it "accepts a class that matches and a matcher that only specifies 'rejecting'" do it "accepts a class that matches and a matcher that only specifies 'rejecting'" do
...@@ -54,6 +60,7 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do ...@@ -54,6 +60,7 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do
matcher = plain_matcher.rejecting(%w(audio/mp3 application/octet-stream)) matcher = plain_matcher.rejecting(%w(audio/mp3 application/octet-stream))
expect(matcher).to accept(Dummy) expect(matcher).to accept(Dummy)
expect { matcher.failure_message }.to_not raise_error
end end
it "rejects a class that does not match and a matcher that only specifies 'rejecting'" do it "rejects a class that does not match and a matcher that only specifies 'rejecting'" do
...@@ -61,6 +68,7 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do ...@@ -61,6 +68,7 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do
matcher = plain_matcher.rejecting(%w(audio/mp3 application/octet-stream)) matcher = plain_matcher.rejecting(%w(audio/mp3 application/octet-stream))
expect(matcher).to_not accept(Dummy) expect(matcher).to_not accept(Dummy)
expect { matcher.failure_message }.to_not raise_error
end end
context "using an :if to control the validation" do context "using an :if to control the validation" do
...@@ -75,12 +83,14 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do ...@@ -75,12 +83,14 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do
dummy = Dummy.new dummy = Dummy.new
dummy.go = true dummy.go = true
expect(matcher).to accept(dummy) expect(matcher).to accept(dummy)
expect { matcher.failure_message }.to_not raise_error
end end
it "does not run the validation if the control is false" do it "does not run the validation if the control is false" do
dummy = Dummy.new dummy = Dummy.new
dummy.go = false dummy.go = false
expect(matcher).to_not accept(dummy) expect(matcher).to_not accept(dummy)
expect { matcher.failure_message }.to_not raise_error
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