Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
paperclip
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ikcrm_common
paperclip
Commits
ee42b198
Commit
ee42b198
authored
Mar 16, 2012
by
Prem Sichanugrist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add AttachmentPresenceValidator
parent
e83f88f3
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
112 additions
and
17 deletions
+112
-17
lib/paperclip.rb
+0
-17
lib/paperclip/validators.rb
+1
-0
lib/paperclip/validators/attachment_presence_validator.rb
+26
-0
test/validators/attachment_presence_validator_test.rb
+85
-0
No files found.
lib/paperclip.rb
View file @
ee42b198
...
@@ -195,23 +195,6 @@ module Paperclip
...
@@ -195,23 +195,6 @@ module Paperclip
end
end
end
end
# 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
name
,
options
=
{}
message
=
options
[
:message
]
||
:empty
validates_each
:"
#{
name
}
_file_name"
do
|
record
,
attr
,
value
|
if_clause_passed
=
options
[
:if
].
nil?
||
(
options
[
:if
].
respond_to?
(
:call
)
?
options
[:
if
].
call
(
record
)
!=
false
:
record
.
send
(
options
[
:if
]))
unless_clause_passed
=
options
[
:unless
].
nil?
||
(
options
[
:unless
].
respond_to?
(
:call
)
?
!!
options
[:
unless
].
call
(
record
)
==
false
:
!
record
.
send
(
options
[
:unless
]))
if
if_clause_passed
&&
unless_clause_passed
&&
value
.
blank?
record
.
errors
.
add
(
name
,
message
)
record
.
errors
.
add
(
"
#{
name
}
_file_name"
,
message
)
end
end
end
# Places ActiveRecord-style validations on the content type of the file
# Places ActiveRecord-style validations on the content type of the file
# assigned. The possible options are:
# assigned. The possible options are:
# * +content_type+: Allowed content types. Can be a single content type
# * +content_type+: Allowed content types. Can be a single content type
...
...
lib/paperclip/validators.rb
View file @
ee42b198
require
'active_support/concern'
require
'active_support/concern'
require
'paperclip/validators/attachment_presence_validator'
require
'paperclip/validators/attachment_size_validator'
require
'paperclip/validators/attachment_size_validator'
module
Paperclip
module
Paperclip
...
...
lib/paperclip/validators/attachment_presence_validator.rb
0 → 100644
View file @
ee42b198
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
test/validators/attachment_presence_validator_test.rb
0 → 100644
View file @
ee42b198
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment