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
5befa1a9
Commit
5befa1a9
authored
Apr 10, 2014
by
Andrey Novikov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow to pass arrays of validations to the validators
parent
41da3a3c
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
50 additions
and
3 deletions
+50
-3
lib/paperclip/validators.rb
+4
-3
spec/paperclip/validators_spec.rb
+46
-0
spec/support/fixtures/spaced file.jpg
+0
-0
No files found.
lib/paperclip/validators.rb
View file @
5befa1a9
...
...
@@ -41,10 +41,11 @@ module Paperclip
if
options
.
has_key?
(
validator_kind
)
validator_options
=
options
.
delete
(
validator_kind
)
validator_options
=
{}
if
validator_options
==
true
local_options
=
attributes
+
[
validator_options
]
conditional_options
=
options
.
slice
(
:if
,
:unless
)
local_options
.
last
.
merge!
(
conditional_options
)
send
(
Paperclip
::
Validators
.
const_get
(
constant
.
to_s
).
helper_method_name
,
*
local_options
)
Array
.
wrap
(
validator_options
).
each
do
|
local_options
|
method_name
=
Paperclip
::
Validators
.
const_get
(
constant
.
to_s
).
helper_method_name
send
(
method_name
,
attributes
,
local_options
.
merge
(
conditional_options
))
end
end
end
end
...
...
spec/paperclip/validators_spec.rb
View file @
5befa1a9
...
...
@@ -26,6 +26,52 @@ describe Paperclip::Validators do
end
end
context
'using the helper with array of validations'
do
before
do
rebuild_class
Dummy
.
validates_attachment
:avatar
,
file_type_ignorance:
true
,
file_name:
[
{
matches:
/\A.*\.jpe?g\Z/i
,
message: :invalid_extension
},
{
matches:
/\A.{,8}\..+\Z/i
,
message:
[
:too_long
,
count:
8
]
},
]
end
it
'adds the attachment_file_name validator to the class'
do
assert
Dummy
.
validators_on
(
:avatar
).
any?
{
|
validator
|
validator
.
kind
==
:attachment_file_name
}
end
it
'adds the attachment_file_name validator with two validations'
do
assert_equal
2
,
Dummy
.
validators_on
(
:avatar
).
select
{
|
validator
|
validator
.
kind
==
:attachment_file_name
}.
size
end
it
'prevents you from attaching a file that violates all of these validations'
do
Dummy
.
class_eval
{
validate
(
:name
)
{
raise
'DO NOT RUN THIS'
}
}
dummy
=
Dummy
.
new
(
avatar:
File
.
new
(
fixture_file
(
'spaced file.png'
)))
expect
(
dummy
.
errors
.
keys
).
to
match_array
[
:avatar
,
:avatar_file_name
]
assert_raises
(
RuntimeError
){
dummy
.
valid?
}
end
it
'prevents you from attaching a file that violates only first of these validations'
do
Dummy
.
class_eval
{
validate
(
:name
)
{
raise
'DO NOT RUN THIS'
}
}
dummy
=
Dummy
.
new
(
avatar:
File
.
new
(
fixture_file
(
'5k.png'
)))
expect
(
dummy
.
errors
.
keys
).
to
match_array
[
:avatar
,
:avatar_file_name
]
assert_raises
(
RuntimeError
){
dummy
.
valid?
}
end
it
'prevents you from attaching a file that violates only second of these validations'
do
Dummy
.
class_eval
{
validate
(
:name
)
{
raise
'DO NOT RUN THIS'
}
}
dummy
=
Dummy
.
new
(
avatar:
File
.
new
(
fixture_file
(
'spaced file.jpg'
)))
expect
(
dummy
.
errors
.
keys
).
to
match_array
[
:avatar
,
:avatar_file_name
]
assert_raises
(
RuntimeError
){
dummy
.
valid?
}
end
it
'allows you to attach a file that does not violates these validations'
do
dummy
=
Dummy
.
new
(
avatar:
File
.
new
(
fixture_file
(
'rotated.jpg'
)))
expect
(
dummy
.
errors
.
keys
).
to
match_array
[]
assert
dummy
.
valid?
end
end
context
"using the helper with a conditional"
do
before
do
rebuild_class
...
...
spec/support/fixtures/spaced file.jpg
0 → 100644
View file @
5befa1a9
81.1 KB
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