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
ee4107ab
Commit
ee4107ab
authored
Mar 23, 2012
by
Prem Sichanugrist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add `validates_attachment` method to model
parent
b3a63edb
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
0 deletions
+55
-0
lib/paperclip/validators.rb
+30
-0
test/validators_test.rb
+25
-0
No files found.
lib/paperclip/validators.rb
View file @
ee4107ab
...
...
@@ -11,5 +11,35 @@ module Paperclip
extend
HelperMethods
include
HelperMethods
end
module
ClassMethods
# This method is a shortcut to validator classes that is in
# "Attachment...Validator" format. It is almost the same thing as the
# +validates+ method that shipped with Rails, but this is customized to
# be using with attachment validators. This is helpful when you're using
# multiple attachment validators on a single attachment.
#
# Example of using the validator:
#
# validates_attachment :avatar, :presence => true,
# :content_type => { :content_type => "image/jpg" },
# :size => { :in => 0..10.kilobytes }
#
def
validates_attachment
(
*
attributes
)
options
=
attributes
.
extract_options!
.
dup
Paperclip
::
Validators
.
constants
.
each
do
|
constant
|
if
constant
.
to_s
=~
/^Attachment(.+)Validator$/
validator_kind
=
$1
.
underscore
.
to_sym
if
options
.
has_key?
(
validator_kind
)
options
[
:"attachment_
#{
validator_kind
}
"
]
=
options
.
delete
(
validator_kind
)
end
end
end
validates
(
*
attributes
+
[
options
])
end
end
end
end
test/validators_test.rb
0 → 100644
View file @
ee4107ab
require
'./test/helper'
class
ValidatorsTest
<
Test
::
Unit
::
TestCase
def
setup
rebuild_model
end
context
"using the helper"
do
setup
do
Dummy
.
validates_attachment
:avatar
,
:presence
=>
true
,
:content_type
=>
{
:content_type
=>
"image/jpg"
},
:size
=>
{
:in
=>
0
..
10
.
kilobytes
}
end
should
"add the attachment_presence validator to the class"
do
assert
Dummy
.
validators_on
(
:avatar
).
any?
{
|
validator
|
validator
.
kind
==
:attachment_presence
}
end
should
"add the attachment_content_type validator to the class"
do
assert
Dummy
.
validators_on
(
:avatar
).
any?
{
|
validator
|
validator
.
kind
==
:attachment_content_type
}
end
should
"add the attachment_size validator to the class"
do
assert
Dummy
.
validators_on
(
:avatar
).
any?
{
|
validator
|
validator
.
kind
==
:attachment_size
}
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