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
1926744e
Commit
1926744e
authored
Nov 30, 2009
by
Jon Yurek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Changed validations from Attachment based to be ActiveRecord based.
parent
c64223c6
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
43 additions
and
43 deletions
+43
-43
lib/paperclip.rb
+13
-10
lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
+2
-3
lib/paperclip/matchers/validate_attachment_presence_matcher.rb
+6
-6
lib/paperclip/matchers/validate_attachment_size_matcher.rb
+5
-3
test/matchers/validate_attachment_content_type_matcher_test.rb
+1
-0
test/matchers/validate_attachment_presence_matcher_test.rb
+3
-1
test/matchers/validate_attachment_size_matcher_test.rb
+1
-0
test/paperclip_test.rb
+12
-20
No files found.
lib/paperclip.rb
View file @
1926744e
...
...
@@ -257,13 +257,13 @@ module Paperclip
max
=
options
[
:less_than
]
||
(
options
[
:in
]
&&
options
[
:in
].
last
)
||
(
1.0
/
0
)
range
=
(
min
..
max
)
message
=
options
[
:message
]
||
"file size must be between :min and :max bytes."
message
=
message
.
gsub
(
/:min/
,
min
.
to_s
).
gsub
(
/:max/
,
max
.
to_s
)
attachment_definitions
[
name
][
:validations
]
<<
[
:size
,
{
:min
=>
min
,
:max
=>
max
,
:range
=>
range
,
validates_inclusion_of
:"
#{
name
}
_file_size"
,
:in
=>
range
,
:message
=>
message
,
:if
=>
options
[
:if
],
:unless
=>
options
[
:unless
]}
]
:unless
=>
options
[
:unless
]
end
# Adds errors if thumbnail creation fails. The same as specifying :whiny_thumbnails => true.
...
...
@@ -281,9 +281,10 @@ module Paperclip
# * +unless+: Same as +if+ but validates if lambda or method returns false.
def
validates_attachment_presence
name
,
options
=
{}
message
=
options
[
:message
]
||
"must be set."
attachment_definitions
[
name
][
:validations
]
<<
[
:presence
,
{
:message
=>
message
,
validates_presence_of
:"
#{
name
}
_file_name"
,
:message
=>
message
,
:if
=>
options
[
:if
],
:unless
=>
options
[
:unless
]}
]
:unless
=>
options
[
:unless
]
end
# Places ActiveRecord-style validations on the content type of the file
...
...
@@ -303,10 +304,12 @@ module Paperclip
# model, content_type validation will work _ONLY upon assignment_ and
# re-validation after the instance has been reloaded will always succeed.
def
validates_attachment_content_type
name
,
options
=
{}
attachment_definitions
[
name
][
:validations
]
<<
[
:content_type
,
{
:content_type
=>
options
[
:content_type
],
:message
=>
options
[
:message
],
:if
=>
options
[
:if
],
:unless
=>
options
[
:unless
]}]
types
=
[
options
.
delete
(
:content_type
)].
flatten
validates_each
(
:"
#{
name
}
_content_type"
,
options
)
do
|
record
,
attr
,
value
|
unless
types
.
any?
{
|
t
|
t
===
value
}
record
.
errors
.
add
(
:"
#{
name
}
_content_type"
,
:inclusion
,
:default
=>
options
[
:message
],
:value
=>
value
)
end
end
end
# Returns the attachment definitions defined by each call to
...
...
lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
View file @
1926744e
...
...
@@ -46,9 +46,8 @@ module Paperclip
types
.
all?
do
|
type
|
file
=
StringIO
.
new
(
"."
)
file
.
content_type
=
type
attachment
=
@subject
.
new
.
attachment_for
(
@attachment_name
)
attachment
.
assign
(
file
)
attachment
.
errors
[
:content_type
].
nil?
(
subject
=
@subject
.
new
).
attachment_for
(
@attachment_name
).
assign
(
file
)
subject
.
valid?
&&
subject
.
errors
.
on
(
:"
#{
@attachment_name
}
_content_type"
).
blank?
end
end
...
...
lib/paperclip/matchers/validate_attachment_presence_matcher.rb
View file @
1926744e
...
...
@@ -30,16 +30,16 @@ module Paperclip
protected
def
error_when_not_valid?
@attachment
=
@subject
.
new
.
send
(
@attachment_name
)
@attachment
.
assign
(
nil
)
not
@attachment
.
errors
[
:presence
].
nil
?
(
subject
=
@subject
.
new
).
send
(
@attachment_name
).
assign
(
nil
)
subject
.
valid?
not
subject
.
errors
.
on
(
:"
#{
@attachment_name
}
_file_name"
).
blank
?
end
def
no_error_when_valid?
@file
=
StringIO
.
new
(
"."
)
@attachment
=
@subject
.
new
.
send
(
@attachment_nam
e
)
@attachment
.
assign
(
@file
)
@attachment
.
errors
[
:presence
].
nil
?
(
subject
=
@subject
.
new
).
send
(
@attachment_name
).
assign
(
@fil
e
)
subject
.
valid?
subject
.
errors
.
on
(
:"
#{
@attachment_name
}
_file_name"
).
blank
?
end
end
end
...
...
lib/paperclip/matchers/validate_attachment_size_matcher.rb
View file @
1926744e
...
...
@@ -54,9 +54,11 @@ module Paperclip
def
passes_validation_with_size
(
new_size
)
file
=
StringIO
.
new
(
"."
)
override_method
(
file
,
:size
){
new_size
}
attachment
=
@subject
.
new
.
attachment_for
(
@attachment_name
)
attachment
.
assign
(
file
)
attachment
.
errors
[
:size
].
nil?
override_method
(
file
,
:to_tempfile
){
file
}
(
subject
=
@subject
.
new
).
send
(
@attachment_name
).
assign
(
file
)
subject
.
valid?
subject
.
errors
.
on
(
:"
#{
@attachment_name
}
_file_size"
).
blank?
end
def
lower_than_low?
...
...
test/matchers/validate_attachment_content_type_matcher_test.rb
View file @
1926744e
...
...
@@ -5,6 +5,7 @@ class ValidateAttachmentContentTypeMatcherTest < Test::Unit::TestCase
setup
do
reset_table
(
"dummies"
)
do
|
d
|
d
.
string
:avatar_file_name
d
.
string
:avatar_content_type
end
@dummy_class
=
reset_class
"Dummy"
@dummy_class
.
has_attached_file
:avatar
...
...
test/matchers/validate_attachment_presence_matcher_test.rb
View file @
1926744e
...
...
@@ -3,7 +3,9 @@ require 'test/helper'
class
ValidateAttachmentPresenceMatcherTest
<
Test
::
Unit
::
TestCase
context
"validate_attachment_presence"
do
setup
do
reset_table
(
"dummies"
){
|
d
|
d
.
string
:avatar_file_name
}
reset_table
(
"dummies"
)
do
|
d
|
d
.
string
:avatar_file_name
end
@dummy_class
=
reset_class
"Dummy"
@dummy_class
.
has_attached_file
:avatar
@matcher
=
self
.
class
.
validate_attachment_presence
(
:avatar
)
...
...
test/matchers/validate_attachment_size_matcher_test.rb
View file @
1926744e
...
...
@@ -5,6 +5,7 @@ class ValidateAttachmentSizeMatcherTest < Test::Unit::TestCase
setup
do
reset_table
(
"dummies"
)
do
|
d
|
d
.
string
:avatar_file_name
d
.
integer
:avatar_file_size
end
@dummy_class
=
reset_class
"Dummy"
@dummy_class
.
has_attached_file
:avatar
...
...
test/paperclip_test.rb
View file @
1926744e
...
...
@@ -212,18 +212,17 @@ class PaperclipTest < Test::Unit::TestCase
setup
do
Dummy
.
send
(
:"validates_attachment_presence"
,
:avatar
,
:if
=>
lambda
{
|
i
|
i
.
foo
})
@dummy
=
Dummy
.
new
@dummy
.
stubs
(
:avatar_file_name
).
returns
(
nil
)
end
should
"attempt validation if the guard returns true"
do
@dummy
.
expects
(
:foo
).
returns
(
true
)
@dummy
.
avatar
.
expects
(
:validate_presence
).
returns
(
nil
)
@dummy
.
valid?
assert
!
@dummy
.
valid?
end
should
"not attempt validation if the guard returns false"
do
@dummy
.
expects
(
:foo
).
returns
(
false
)
@dummy
.
avatar
.
expects
(
:validate_presence
).
never
@dummy
.
valid?
assert
@dummy
.
valid?
end
end
...
...
@@ -231,18 +230,17 @@ class PaperclipTest < Test::Unit::TestCase
setup
do
Dummy
.
send
(
:"validates_attachment_presence"
,
:avatar
,
:unless
=>
lambda
{
|
i
|
i
.
foo
})
@dummy
=
Dummy
.
new
@dummy
.
stubs
(
:avatar_file_name
).
returns
(
nil
)
end
should
"attempt validation if the guard returns true"
do
@dummy
.
expects
(
:foo
).
returns
(
false
)
@dummy
.
avatar
.
expects
(
:validate_presence
).
returns
(
nil
)
@dummy
.
valid?
assert
!
@dummy
.
valid?
end
should
"not attempt validation if the guard returns false"
do
@dummy
.
expects
(
:foo
).
returns
(
true
)
@dummy
.
avatar
.
expects
(
:validate_presence
).
never
@dummy
.
valid?
assert
@dummy
.
valid?
end
end
...
...
@@ -259,11 +257,11 @@ class PaperclipTest < Test::Unit::TestCase
end
if
validation
==
:presence
should
"have an error on the attachment"
do
assert
@dummy
.
errors
.
on
(
:avatar
)
assert
@dummy
.
errors
.
on
(
:avatar
_file_name
)
end
else
should
"not have an error on the attachment"
do
assert_nil
@dummy
.
errors
.
on
(
:avatar
)
assert_nil
@dummy
.
errors
.
on
(
:avatar
_file_name
),
@dummy
.
errors
.
full_messages
.
join
(
", "
)
end
end
end
...
...
@@ -273,10 +271,7 @@ class PaperclipTest < Test::Unit::TestCase
@dummy
.
valid?
end
should
"not have an error when assigned a valid file"
do
assert
!
@dummy
.
avatar
.
errors
.
key?
(
validation
)
end
should
"not have an error on the attachment"
do
assert_nil
@dummy
.
errors
.
on
(
:avatar
)
assert_equal
0
,
@dummy
.
errors
.
length
,
@dummy
.
errors
.
full_messages
.
join
(
", "
)
end
end
context
"and assigned an invalid file"
do
...
...
@@ -285,17 +280,14 @@ class PaperclipTest < Test::Unit::TestCase
@dummy
.
valid?
end
should
"have an error when assigned a valid file"
do
assert_not_nil
@dummy
.
avatar
.
errors
[
validation
]
end
should
"have an error on the attachment"
do
assert
@dummy
.
errors
.
on
(
:avatar
)
assert
@dummy
.
errors
.
length
>
0
end
end
end
end
[[
:presence
,
{},
"5k.png"
,
nil
],
[
:size
,
{
:in
=>
1
..
10240
},
nil
,
"12k.png"
],
[
:size
,
{
:in
=>
1
..
10240
},
"5k.png"
,
"12k.png"
],
[
:size
,
{
:less_than
=>
10240
},
"5k.png"
,
"12k.png"
],
[
:size
,
{
:greater_than
=>
8096
},
"12k.png"
,
"5k.png"
],
[
:content_type
,
{
:content_type
=>
"image/png"
},
"5k.png"
,
"text.txt"
],
...
...
@@ -318,7 +310,7 @@ class PaperclipTest < Test::Unit::TestCase
end
should
"have a file size min/max error message"
do
assert_match
/between 0 and 10240 bytes/
,
@dummy
.
errors
.
on
(
:avatar
)
assert_match
%r/between 0 and 10240 bytes/
,
@dummy
.
errors
.
on
(
:avatar_file_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