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
0bca09a1
Commit
0bca09a1
authored
Feb 04, 2011
by
Franco Catena
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added lambda messages to validators
parent
fc792c89
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
0 deletions
+32
-0
lib/paperclip.rb
+2
-0
test/paperclip_test.rb
+30
-0
No files found.
lib/paperclip.rb
View file @
0bca09a1
...
@@ -276,6 +276,7 @@ module Paperclip
...
@@ -276,6 +276,7 @@ module Paperclip
max
=
options
[
:less_than
]
||
(
options
[
:in
]
&&
options
[
:in
].
last
)
||
(
1.0
/
0
)
max
=
options
[
:less_than
]
||
(
options
[
:in
]
&&
options
[
:in
].
last
)
||
(
1.0
/
0
)
range
=
(
min
..
max
)
range
=
(
min
..
max
)
message
=
options
[
:message
]
||
"file size must be between :min and :max bytes."
message
=
options
[
:message
]
||
"file size must be between :min and :max bytes."
message
=
message
.
call
if
message
.
respond_to?
(
:call
)
message
=
message
.
gsub
(
/:min/
,
min
.
to_s
).
gsub
(
/:max/
,
max
.
to_s
)
message
=
message
.
gsub
(
/:min/
,
min
.
to_s
).
gsub
(
/:max/
,
max
.
to_s
)
validates_inclusion_of
:"
#{
name
}
_file_size"
,
validates_inclusion_of
:"
#{
name
}
_file_size"
,
...
@@ -330,6 +331,7 @@ module Paperclip
...
@@ -330,6 +331,7 @@ module Paperclip
if
!
allowed_types
.
any?
{
|
t
|
t
===
value
}
&&
!
(
value
.
nil?
||
value
.
blank?
)
if
!
allowed_types
.
any?
{
|
t
|
t
===
value
}
&&
!
(
value
.
nil?
||
value
.
blank?
)
if
record
.
errors
.
method
(
:add
).
arity
==
-
2
if
record
.
errors
.
method
(
:add
).
arity
==
-
2
message
=
options
[
:message
]
||
"is not one of
#{
allowed_types
.
join
(
", "
)
}
"
message
=
options
[
:message
]
||
"is not one of
#{
allowed_types
.
join
(
", "
)
}
"
message
=
message
.
call
if
message
.
respond_to?
(
:call
)
record
.
errors
.
add
(
:"
#{
name
}
_content_type"
,
message
)
record
.
errors
.
add
(
:"
#{
name
}
_content_type"
,
message
)
else
else
record
.
errors
.
add
(
:"
#{
name
}
_content_type"
,
:inclusion
,
:default
=>
options
[
:message
],
:value
=>
value
)
record
.
errors
.
add
(
:"
#{
name
}
_content_type"
,
:inclusion
,
:default
=>
options
[
:message
],
:value
=>
value
)
...
...
test/paperclip_test.rb
View file @
0bca09a1
...
@@ -252,6 +252,21 @@ class PaperclipTest < Test::Unit::TestCase
...
@@ -252,6 +252,21 @@ class PaperclipTest < Test::Unit::TestCase
should_validate
validation
,
options
,
valid_file
,
invalid_file
should_validate
validation
,
options
,
valid_file
,
invalid_file
end
end
context
"with content_type validation and lambda message"
do
context
"and assigned an invalid file"
do
setup
do
Dummy
.
send
(
:"validates_attachment_content_type"
,
:avatar
,
:content_type
=>
%r{image/.*}
,
:message
=>
lambda
{
'lambda content type message'
})
@dummy
=
Dummy
.
new
@dummy
.
avatar
&&=
File
.
open
(
File
.
join
(
FIXTURES_DIR
,
"text.txt"
),
"rb"
)
@dummy
.
valid?
end
should
"have a content type error message"
do
assert
[
@dummy
.
errors
[
:avatar_content_type
]].
flatten
.
any?
{
|
error
|
error
=~
%r/lambda content type message/
}
end
end
end
context
"with size validation and less_than 10240 option"
do
context
"with size validation and less_than 10240 option"
do
context
"and assigned an invalid file"
do
context
"and assigned an invalid file"
do
setup
do
setup
do
...
@@ -267,5 +282,20 @@ class PaperclipTest < Test::Unit::TestCase
...
@@ -267,5 +282,20 @@ class PaperclipTest < Test::Unit::TestCase
end
end
end
end
context
"with size validation and less_than 10240 option with lambda message"
do
context
"and assigned an invalid file"
do
setup
do
Dummy
.
send
(
:"validates_attachment_size"
,
:avatar
,
:less_than
=>
10240
,
:message
=>
lambda
{
'lambda between 0 and 10240 bytes'
})
@dummy
=
Dummy
.
new
@dummy
.
avatar
&&=
File
.
open
(
File
.
join
(
FIXTURES_DIR
,
"12k.png"
),
"rb"
)
@dummy
.
valid?
end
should
"have a file size min/max error message"
do
assert
[
@dummy
.
errors
[
:avatar_file_size
]].
flatten
.
any?
{
|
error
|
error
=~
%r/lambda between 0 and 10240 bytes/
}
end
end
end
end
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