Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Ensure required validations accepts subclasses #1515

Merged
merged 1 commit into from May 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/paperclip/attachment.rb
Expand Up @@ -391,8 +391,16 @@ def active_validator_classes
@instance.class.validators.map(&:class)
end

def required_validator_classes
Paperclip::REQUIRED_VALIDATORS + Paperclip::REQUIRED_VALIDATORS.flat_map(&:descendants)
end

def missing_required_validator?
(active_validator_classes & required_validator_classes).empty?
end

def ensure_required_validations!
if (active_validator_classes & Paperclip::REQUIRED_VALIDATORS).empty?
if missing_required_validator?
raise Paperclip::Errors::MissingRequiredValidatorError
end
end
Expand Down
22 changes: 22 additions & 0 deletions spec/paperclip/validators_spec.rb
Expand Up @@ -77,6 +77,28 @@ def title_present?
end
end

it 'does not raise an error when a content_type validation exists using validates_with' do
Dummy.validates_with Paperclip::Validators::AttachmentContentTypeValidator, attributes: :attachment, content_type: 'images/jpeg'

assert_nothing_raised do
Dummy.new(avatar: File.new(fixture_file("12k.png")))
end
end

it 'does not raise an error when an inherited validator is used' do
class MyValidator < Paperclip::Validators::AttachmentContentTypeValidator
def initialize(options)
options[:content_type] = "images/jpeg" unless options.has_key?(:content_type)
super
end
end
Dummy.validates_with MyValidator, attributes: :attachment

assert_nothing_raised do
Dummy.new(avatar: File.new(fixture_file("12k.png")))
end
end

it 'does not raise an error when a file_name validation exists' do
Dummy.validates_attachment :avatar, file_name: { matches: /png$/ }

Expand Down