This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
112 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'active_model/validations/presence' | ||
|
||
module Paperclip | ||
module Validators | ||
class AttachmentPresenceValidator < ActiveModel::Validations::PresenceValidator | ||
def validate(record) | ||
[attributes].flatten.map do |attribute| | ||
if record.send(:read_attribute_for_validation, "#{attribute}_file_name").blank? | ||
record.errors.add(attribute, :blank, options) | ||
end | ||
end | ||
end | ||
end | ||
|
||
module HelperMethods | ||
# Places ActiveRecord-style validations on the presence of a file. | ||
# Options: | ||
# * +if+: A lambda or name of an instance method. Validation will only | ||
# be run if this lambda or method returns true. | ||
# * +unless+: Same as +if+ but validates if lambda or method returns false. | ||
def validates_attachment_presence(*attr_names) | ||
validates_with AttachmentPresenceValidator, _merge_attributes(attr_names) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
require './test/helper' | ||
|
||
class AttachmentPresenceValidatorTest < Test::Unit::TestCase | ||
def setup | ||
rebuild_model | ||
@dummy = Dummy.new | ||
end | ||
|
||
def build_validator(options={}) | ||
@validator = Paperclip::Validators::AttachmentPresenceValidator.new(options.merge( | ||
:attributes => :avatar | ||
)) | ||
end | ||
|
||
context "nil attachment" do | ||
setup do | ||
@dummy.avatar = nil | ||
end | ||
|
||
context "with default options" do | ||
setup do | ||
build_validator | ||
@validator.validate(@dummy) | ||
end | ||
|
||
should "add error on the attachment" do | ||
assert @dummy.errors[:avatar].present? | ||
end | ||
|
||
should "not add an error on the file_name attribute" do | ||
assert @dummy.errors[:avatar_file_name].blank? | ||
end | ||
end | ||
|
||
context "with :if option" do | ||
context "returning true" do | ||
setup do | ||
build_validator :if => true | ||
@validator.validate(@dummy) | ||
end | ||
|
||
should "perform a validation" do | ||
assert @dummy.errors[:avatar].present? | ||
end | ||
end | ||
|
||
context "returning false" do | ||
setup do | ||
build_validator :if => false | ||
@validator.validate(@dummy) | ||
end | ||
|
||
should "perform a validation" do | ||
assert @dummy.errors[:avatar].present? | ||
end | ||
end | ||
end | ||
end | ||
|
||
context "with attachment" do | ||
setup do | ||
build_validator | ||
@dummy.avatar = StringIO.new('.') | ||
@validator.validate(@dummy) | ||
end | ||
|
||
should "not add error on the attachment" do | ||
assert @dummy.errors[:avatar].blank? | ||
end | ||
|
||
should "not add an error on the file_name attribute" do | ||
assert @dummy.errors[:avatar_file_name].blank? | ||
end | ||
end | ||
|
||
context "using the helper" do | ||
setup do | ||
Dummy.validates_attachment_presence :avatar | ||
end | ||
|
||
should "add the validator to the class" do | ||
assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_presence } | ||
end | ||
end | ||
end |