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
28242594
Commit
28242594
authored
Feb 07, 2009
by
Jon Yurek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Content type validation macro to matcher conversion
parent
4793daab
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
97 additions
and
0 deletions
+97
-0
shoulda_macros/matchers.rb
+1
-0
shoulda_macros/matchers/validate_attachment_content_type_matcher.rb
+66
-0
test/matchers/validate_attachment_content_type_matcher_test.rb
+30
-0
No files found.
shoulda_macros/matchers.rb
View file @
28242594
require
'shoulda_macros/matchers/have_attached_file_matcher'
require
'shoulda_macros/matchers/validate_attachment_presence_matcher'
require
'shoulda_macros/matchers/validate_attachment_content_type_matcher'
shoulda_macros/matchers/validate_attachment_content_type_matcher.rb
0 → 100644
View file @
28242594
module
Paperclip
module
Shoulda
module
Matchers
def
validate_attachment_content_type
name
ValidateAttachmentContentTypeMatcher
.
new
(
name
)
end
class
ValidateAttachmentContentTypeMatcher
def
initialize
attachment_name
@attachment_name
=
attachment_name
end
def
allowing
*
types
@allowed_types
=
types
.
flatten
self
end
def
rejecting
*
types
@rejected_types
=
types
.
flatten
self
end
def
matches?
subject
@subject
=
subject
@allowed_types
&&
@rejected_types
&&
allowed_types_allowed?
&&
rejected_types_rejected?
end
def
failure_message
"Content types
#{
@allowed_types
.
join
(
", "
)
}
should be accepted"
+
" and
#{
@rejected_types
.
join
(
", "
)
}
rejected by
#{
@attachment_name
}
"
end
def
negative_failure_message
"Content types
#{
@allowed_types
.
join
(
", "
)
}
should be rejected"
+
" and
#{
@rejected_types
.
join
(
", "
)
}
accepted by
#{
@attachment_name
}
"
end
def
description
"validate the content types allowed on attachment
#{
@attachment_name
}
"
end
protected
def
allow_types?
(
types
)
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?
end
end
def
allowed_types_allowed?
allow_types?
(
@allowed_types
)
end
def
rejected_types_rejected?
not
allow_types?
(
@rejected_types
)
end
end
end
end
end
test/matchers/validate_attachment_content_type_matcher_test.rb
0 → 100644
View file @
28242594
require
'test/helper'
class
ValidateAttachmentContentTypeMatcherTest
<
Test
::
Unit
::
TestCase
context
"validate_attachment_content_type"
do
setup
do
reset_table
(
"dummies"
)
do
|
d
|
d
.
string
:avatar_file_name
end
@dummy_class
=
reset_class
"Dummy"
@dummy_class
.
has_attached_file
:avatar
@matcher
=
validate_attachment_content_type
(
:avatar
).
allowing
(
%w(image/png image/jpeg)
).
rejecting
(
%w(audio/mp3 application/octet-stream)
)
end
should
"reject a class with no validation"
do
assert_rejects
@matcher
,
@dummy_class
end
should
"reject a class with a validation that doesn't match"
do
@dummy_class
.
validates_attachment_content_type
:avatar
,
:content_type
=>
%r{audio/.*}
assert_rejects
@matcher
,
@dummy_class
end
should
"accept a class with a validation"
do
@dummy_class
.
validates_attachment_content_type
:avatar
,
:content_type
=>
%r{image/.*}
assert_accepts
@matcher
,
@dummy_class
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