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
17a03b28
Commit
17a03b28
authored
Dec 12, 2008
by
Jon Yurek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Presence and conten_type validation shoulda macros
parent
e8518e0e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
63 additions
and
1 deletions
+63
-1
shoulda_macros/paperclip.rb
+63
-1
No files found.
shoulda_macros/paperclip.rb
View file @
17a03b28
...
@@ -11,12 +11,74 @@ module Paperclip
...
@@ -11,12 +11,74 @@ module Paperclip
end
end
end
end
def
should_validate_attachment_presence
name
klass
=
self
.
name
.
gsub
(
/Test$/
,
''
).
constantize
context
"Class
#{
klass
.
name
}
validating presence on
#{
name
}
"
do
context
"when the assignment is nil"
do
setup
do
@attachment
=
klass
.
new
.
send
(
name
)
@attachment
.
assign
(
nil
)
end
should
"have a :presence validation error"
do
assert
@assignment
.
errors
[
:presence
]
end
end
context
"when the assignment is valid"
do
setup
do
@attachment
=
klass
.
new
.
send
(
name
)
@attachment
.
assign
(
nil
)
end
should
"have a :presence validation error"
do
assert
!
@assignment
.
errors
[
:presence
]
end
end
end
end
def
should_validate_attachment_content_type
name
,
options
=
{}
klass
=
self
.
name
.
gsub
(
/Test$/
,
''
).
constantize
valid
=
[
options
[
:valid
]].
flatten
invalid
=
[
options
[
:invalid
]].
flatten
context
"Class
#{
klass
.
name
}
validating content_types on
#{
name
}
"
do
valid
.
each
do
|
type
|
context
"being assigned a file with a content_type of
#{
type
}
"
do
setup
do
@file
=
StringIO
.
new
(
"."
)
class
<<
@file
;
attr_accessor
:content_type
;
end
@file
.
content_type
=
type
@attachment
=
klass
.
new
.
send
(
name
)
@attachment
.
assign
(
@file
)
end
should
"not have a :content_type validation error"
do
assert
!
@assignment
.
errors
[
:content_type
]
end
end
end
invalid
.
each
do
|
type
|
context
"being assigned a file with a content_type of
#{
type
}
"
do
setup
do
@file
=
StringIO
.
new
(
"."
)
class
<<
@file
;
attr_accessor
:content_type
;
end
@file
.
content_type
=
type
@attachment
=
klass
.
new
.
send
(
name
)
@attachment
.
assign
(
@file
)
end
should
"have a :content_type validation error"
do
assert
@assignment
.
errors
[
:content_type
]
end
end
end
end
end
def
should_validate_attachment_size
name
,
options
=
{}
def
should_validate_attachment_size
name
,
options
=
{}
klass
=
self
.
name
.
gsub
(
/Test$/
,
''
).
constantize
klass
=
self
.
name
.
gsub
(
/Test$/
,
''
).
constantize
min
=
options
[
:greater_than
]
||
(
options
[
:in
]
&&
options
[
:in
].
first
)
||
0
min
=
options
[
:greater_than
]
||
(
options
[
:in
]
&&
options
[
:in
].
first
)
||
0
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
)
context
"Class
#{
klass
.
name
}
with attachment
#{
name
}
"
do
context
"Class
#{
klass
.
name
}
validating file size on
#{
name
}
"
do
context
"with an attachment that is
#{
max
+
1
}
bytes"
do
context
"with an attachment that is
#{
max
+
1
}
bytes"
do
setup
do
setup
do
@file
=
StringIO
.
new
(
"."
*
(
max
+
1
))
@file
=
StringIO
.
new
(
"."
*
(
max
+
1
))
...
...
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