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
5d4ba628
Commit
5d4ba628
authored
Feb 03, 2012
by
Jon Yurek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Validation matchers respect conditionals on the validations
parent
1cb40e35
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
80 additions
and
15 deletions
+80
-15
lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
+4
-4
lib/paperclip/matchers/validate_attachment_presence_matcher.rb
+7
-7
lib/paperclip/matchers/validate_attachment_size_matcher.rb
+4
-4
test/matchers/validate_attachment_content_type_matcher_test.rb
+23
-0
test/matchers/validate_attachment_presence_matcher_test.rb
+21
-0
test/matchers/validate_attachment_size_matcher_test.rb
+21
-0
No files found.
lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
View file @
5d4ba628
...
...
@@ -33,7 +33,7 @@ module Paperclip
def
matches?
subject
@subject
=
subject
@subject
=
@subject
.
class
unless
Class
===
@subject
@subject
=
@subject
.
new
if
@subject
.
class
==
Class
@allowed_types
&&
@rejected_types
&&
allowed_types_allowed?
&&
rejected_types_rejected?
end
...
...
@@ -63,9 +63,9 @@ module Paperclip
def
type_allowed?
(
type
)
file
=
StringIO
.
new
(
"."
)
file
.
content_type
=
type
(
subject
=
@subject
.
new
)
.
attachment_for
(
@attachment_name
).
assign
(
file
)
subject
.
valid?
subject
.
errors
[
:"
#{
@attachment_name
}
_content_type"
].
blank?
@subject
.
attachment_for
(
@attachment_name
).
assign
(
file
)
@
subject
.
valid?
@
subject
.
errors
[
:"
#{
@attachment_name
}
_content_type"
].
blank?
end
def
allowed_types_allowed?
...
...
lib/paperclip/matchers/validate_attachment_presence_matcher.rb
View file @
5d4ba628
...
...
@@ -18,7 +18,7 @@ module Paperclip
def
matches?
subject
@subject
=
subject
@subject
=
@subject
.
class
unless
Class
===
@subject
@subject
=
subject
.
new
if
subject
.
class
==
Class
error_when_not_valid?
&&
no_error_when_valid?
end
...
...
@@ -37,16 +37,16 @@ module Paperclip
protected
def
error_when_not_valid?
(
subject
=
@subject
.
new
)
.
send
(
@attachment_name
).
assign
(
nil
)
subject
.
valid?
not
subject
.
errors
[
:"
#{
@attachment_name
}
_file_name"
].
blank?
@subject
.
send
(
@attachment_name
).
assign
(
nil
)
@
subject
.
valid?
not
@
subject
.
errors
[
:"
#{
@attachment_name
}
_file_name"
].
blank?
end
def
no_error_when_valid?
@file
=
StringIO
.
new
(
"."
)
(
subject
=
@subject
.
new
)
.
send
(
@attachment_name
).
assign
(
@file
)
subject
.
valid?
subject
.
errors
[
:"
#{
@attachment_name
}
_file_name"
].
blank?
@subject
.
send
(
@attachment_name
).
assign
(
@file
)
@
subject
.
valid?
@
subject
.
errors
[
:"
#{
@attachment_name
}
_file_name"
].
blank?
end
end
end
...
...
lib/paperclip/matchers/validate_attachment_size_matcher.rb
View file @
5d4ba628
...
...
@@ -38,7 +38,7 @@ module Paperclip
def
matches?
subject
@subject
=
subject
@subject
=
@subject
.
class
unless
Class
===
@subject
@subject
=
@subject
.
new
if
@subject
.
class
==
Class
lower_than_low?
&&
higher_than_low?
&&
lower_than_high?
&&
higher_than_high?
end
...
...
@@ -67,9 +67,9 @@ module Paperclip
override_method
(
file
,
:size
){
new_size
}
override_method
(
file
,
:to_tempfile
){
file
}
(
subject
=
@subject
.
new
)
.
send
(
@attachment_name
).
assign
(
file
)
subject
.
valid?
subject
.
errors
[
:"
#{
@attachment_name
}
_file_size"
].
blank?
@subject
.
send
(
@attachment_name
).
assign
(
file
)
@
subject
.
valid?
@
subject
.
errors
[
:"
#{
@attachment_name
}
_file_size"
].
blank?
end
def
lower_than_low?
...
...
test/matchers/validate_attachment_content_type_matcher_test.rb
View file @
5d4ba628
...
...
@@ -83,5 +83,28 @@ class ValidateAttachmentContentTypeMatcherTest < Test::Unit::TestCase
should_reject_dummy_class
end
context
"using an :if to control the validation"
do
setup
do
@dummy_class
.
class_eval
do
validates_attachment_content_type
:avatar
,
:content_type
=>
%r{image/*}
,
:if
=>
:go
attr_accessor
:go
end
@matcher
=
self
.
class
.
validate_attachment_content_type
(
:avatar
).
allowing
(
%w(image/png image/jpeg)
).
rejecting
(
%w(audio/mp3 application/octet-stream)
)
@dummy
=
@dummy_class
.
new
end
should
"run the validation if the control is true"
do
@dummy
.
go
=
true
assert_accepts
@matcher
,
@dummy
end
should
"not run the validation if the control is false"
do
@dummy
.
go
=
false
assert_rejects
@matcher
,
@dummy
end
end
end
end
test/matchers/validate_attachment_presence_matcher_test.rb
View file @
5d4ba628
...
...
@@ -22,5 +22,26 @@ class ValidateAttachmentPresenceMatcherTest < Test::Unit::TestCase
should_accept_dummy_class
end
context
"using an :if to control the validation"
do
setup
do
@dummy_class
.
class_eval
do
validates_attachment_presence
:avatar
,
:if
=>
:go
attr_accessor
:go
end
@dummy
=
@dummy_class
.
new
@dummy
.
avatar
=
nil
end
should
"run the validation if the control is true"
do
@dummy
.
go
=
true
assert_accepts
@matcher
,
@dummy
end
should
"not run the validation if the control is false"
do
@dummy
.
go
=
false
assert_rejects
@matcher
,
@dummy
end
end
end
end
test/matchers/validate_attachment_size_matcher_test.rb
View file @
5d4ba628
...
...
@@ -47,5 +47,26 @@ class ValidateAttachmentSizeMatcherTest < Test::Unit::TestCase
should_accept_dummy_class
end
end
context
"using an :if to control the validation"
do
setup
do
@dummy_class
.
class_eval
do
validates_attachment_size
:avatar
,
:greater_than
=>
1024
,
:if
=>
:go
attr_accessor
:go
end
@dummy
=
@dummy_class
.
new
@matcher
=
self
.
class
.
validate_attachment_size
(
:avatar
).
greater_than
(
1024
)
end
should
"run the validation if the control is true"
do
@dummy
.
go
=
true
assert_accepts
@matcher
,
@dummy
end
should
"not run the validation if the control is false"
do
@dummy
.
go
=
false
assert_rejects
@matcher
,
@dummy
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