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
3d745db9
Commit
3d745db9
authored
May 02, 2008
by
austin.bain
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added option to customize the validation error messages
parent
044acfcd
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
8 deletions
+34
-8
lib/paperclip.rb
+11
-3
test/test_paperclip.rb
+23
-5
No files found.
lib/paperclip.rb
View file @
3d745db9
...
...
@@ -147,6 +147,7 @@ module Paperclip
# * +in+: a Range of bytes (i.e. +1..1.megabyte+),
# * +less_than+: equivalent to :in => 0..options[:less_than]
# * +greater_than+: equivalent to :in => options[:greater_than]..Infinity
# * +message+: error message to display, use #{min} and #{max} as replacements
def
validates_attachment_size
name
,
options
=
{}
attachment_definitions
[
name
][
:validations
]
<<
lambda
do
|
attachment
,
instance
|
unless
options
[
:greater_than
].
nil?
...
...
@@ -156,16 +157,23 @@ module Paperclip
options
[
:in
]
=
(
0
..
options
[
:less_than
])
end
unless
options
[
:in
].
include?
instance
[
:"
#{
name
}
_file_size"
].
to_i
"file size is not between
#{
options
[
:in
].
first
}
and
#{
options
[
:in
].
last
}
bytes."
min
=
options
[
:in
].
first
max
=
options
[
:in
].
last
if
options
[
:message
]
eval
(
'"'
+
options
[
:message
]
+
'"'
)
else
"file size is not between
#{
min
}
and
#{
max
}
bytes."
end
end
end
end
# Places ActiveRecord-style validations on the presence of a file.
def
validates_attachment_presence
name
def
validates_attachment_presence
name
,
options
=
{}
attachment_definitions
[
name
][
:validations
]
<<
lambda
do
|
attachment
,
instance
|
if
attachment
.
original_filename
.
blank?
"must be set."
options
[
:message
]
||
"must be set."
end
end
end
...
...
test/test_paperclip.rb
View file @
3d745db9
...
...
@@ -64,11 +64,14 @@ class PaperclipTest < Test::Unit::TestCase
assert
Dummy
.
new
.
respond_to?
(
:avatar
=
)
end
[[
:presence
,
nil
,
"5k.png"
,
nil
],
[
:size
,
{
:in
=>
1
..
10240
},
"5k.png"
,
"12k.png"
],
[
:content_type1
,{
:content_type
=>
"image/png"
},
"5k.png"
,
"text.txt"
],
[
:content_type2
,
{
:content_type
=>
:image
},
"5k.png"
,
"text.txt"
],
[
:content_type3
,
{
:content_type
=>
"text/plain"
},
"text.txt"
,
"5k.png"
]].
each
do
|
args
|
[[
:presence
,
nil
,
"5k.png"
,
nil
],
[
:size
,
{
:in
=>
1
..
10240
},
"5k.png"
,
"12k.png"
],
[
:content_type1
,{
:content_type
=>
"image/png"
},
"5k.png"
,
"text.txt"
],
[
:content_type2
,
{
:content_type
=>
:image
},
"5k.png"
,
"text.txt"
],
[
:content_type3
,
{
:content_type
=>
"text/plain"
},
"text.txt"
,
"5k.png"
],
[
:presence2
,
{
:message
=>
"error"
},
"5k.png"
,
nil
,
"error"
],
[
:size2
,
{
:in
=>
1
..
10240
,
:message
=>
'size is not between #{min} and #{max} bytes.'
},
"5k.png"
,
"12k.png"
,
"size is not between 1 and 10240 bytes."
],
[
:content_type4
,{
:content_type
=>
"image/png"
,
:message
=>
"error"
},
"5k.png"
,
"text.txt"
,
"error"
]].
each
do
|
args
|
context
"with
#{
args
[
0
]
}
validations"
do
setup
do
Dummy
.
class_eval
do
...
...
@@ -100,6 +103,21 @@ class PaperclipTest < Test::Unit::TestCase
assert_equal
1
,
@dummy
.
avatar
.
errors
.
length
end
end
context
"and an invalid file with :message"
do
setup
do
@file
=
args
[
3
]
&&
File
.
new
(
File
.
join
(
FIXTURES_DIR
,
args
[
3
]))
end
should
"have errors"
do
if
args
[
1
]
&&
args
[
1
][
:message
]
&&
args
[
4
]
@dummy
.
avatar
=
@file
assert
!
@dummy
.
avatar
.
valid?
assert_equal
1
,
@dummy
.
avatar
.
errors
.
length
assert_equal
args
[
4
],
@dummy
.
avatar
.
errors
[
0
]
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