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