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
3eb7b902
Commit
3eb7b902
authored
Jan 02, 2010
by
Mike Mondragon
Browse files
Options
Browse Files
Download
Plain Diff
Merge commit 'thoughtbot/master'
parents
a3703752
51637fe9
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
7 additions
and
107 deletions
+7
-107
lib/paperclip.rb
+1
-1
lib/paperclip/attachment.rb
+6
-71
test/attachment_test.rb
+0
-14
test/paperclip_test.rb
+0
-21
No files found.
lib/paperclip.rb
View file @
3eb7b902
...
...
@@ -240,7 +240,7 @@ module Paperclip
validates_each
(
name
)
do
|
record
,
attr
,
value
|
attachment
=
record
.
attachment_for
(
name
)
attachment
.
send
(
:flush_errors
)
unless
attachment
.
valid?
attachment
.
send
(
:flush_errors
)
end
end
...
...
lib/paperclip/attachment.rb
View file @
3eb7b902
...
...
@@ -14,7 +14,6 @@ module Paperclip
:convert_options
=>
{},
:default_url
=>
"/:attachment/:style/missing.png"
,
:default_style
=>
:original
,
:validations
=>
[],
:storage
=>
:filesystem
,
:whiny
=>
Paperclip
.
options
[
:whiny
]
||
Paperclip
.
options
[
:whiny_thumbnails
]
}
...
...
@@ -38,7 +37,6 @@ module Paperclip
@styles
=
options
[
:styles
]
@normalized_styles
=
nil
@default_url
=
options
[
:default_url
]
@validations
=
options
[
:validations
]
@default_style
=
options
[
:default_style
]
@storage
=
options
[
:storage
]
@whiny
=
options
[
:whiny_thumbnails
]
||
options
[
:whiny
]
...
...
@@ -48,7 +46,6 @@ module Paperclip
@queued_for_delete
=
[]
@queued_for_write
=
{}
@errors
=
{}
@validation_errors
=
nil
@dirty
=
false
initialize_storage
...
...
@@ -69,13 +66,11 @@ module Paperclip
end
# What gets called when you call instance.attachment = File. It clears
# errors, assigns attributes,
processes the file, and runs validations
. It
# errors, assigns attributes,
and processes the file
. It
# also queues up the previous file for deletion, to be flushed away on
# #save of its host. In addition to form uploads, you can also assign
# another Paperclip attachment:
# new_user.avatar = old_user.avatar
# If the file that is assigned is not valid, the processing (i.e.
# thumbnailing, etc) will NOT be run.
def
assign
uploaded_file
ensure_required_accessors!
...
...
@@ -99,13 +94,12 @@ module Paperclip
@dirty
=
true
post_process
if
valid?
post_process
# Reset the file size if the original file was reprocessed.
instance_write
(
:file_size
,
@queued_for_write
[
:original
].
size
.
to_i
)
ensure
uploaded_file
.
close
if
close_uploaded_file
validate
end
# Returns the public URL of the attachment, with a given style. Note that
...
...
@@ -133,12 +127,6 @@ module Paperclip
url
(
style_name
)
end
# Returns true if there are no errors on this attachment.
def
valid?
validate
errors
.
empty?
end
# Returns an array containing the errors on this attachment.
def
errors
@errors
...
...
@@ -152,15 +140,10 @@ module Paperclip
# Saves the file, if there are no errors. If there are, it flushes them to
# the instance's errors and returns false, cancelling the save.
def
save
if
valid?
flush_deletes
flush_writes
@dirty
=
false
true
else
flush_errors
false
end
flush_deletes
flush_writes
@dirty
=
false
true
end
# Clears out the attachment. Has the same effect as previously assigning
...
...
@@ -169,7 +152,6 @@ module Paperclip
def
clear
queue_existing_for_delete
@errors
=
{}
@validation_errors
=
nil
end
# Destroys the attachment. Has the same effect as previously assigning
...
...
@@ -282,53 +264,6 @@ module Paperclip
file
.
nil?
||
(
file
.
respond_to?
(
:original_filename
)
&&
file
.
respond_to?
(
:content_type
))
end
def
validate
#:nodoc:
unless
@validation_errors
@validation_errors
=
@validations
.
inject
({})
do
|
errors
,
validation
|
name
,
options
=
validation
errors
[
name
]
=
send
(
:"validate_
#{
name
}
"
,
options
)
if
allow_validation?
(
options
)
errors
end
@validation_errors
.
reject!
{
|
k
,
v
|
v
==
nil
}
@errors
.
merge!
(
@validation_errors
)
end
@validation_errors
end
def
allow_validation?
options
#:nodoc:
(
options
[
:if
].
nil?
||
check_guard
(
options
[
:if
]))
&&
(
options
[
:unless
].
nil?
||
!
check_guard
(
options
[
:unless
]))
end
def
check_guard
guard
#:nodoc:
if
guard
.
respond_to?
:call
guard
.
call
(
instance
)
elsif
!
guard
.
blank?
instance
.
send
(
guard
.
to_s
)
end
end
def
validate_size
options
#:nodoc:
if
file?
&&
!
options
[
:range
].
include?
(
size
.
to_i
)
options
[
:message
].
gsub
(
/:min/
,
options
[
:min
].
to_s
).
gsub
(
/:max/
,
options
[
:max
].
to_s
)
end
end
def
validate_presence
options
#:nodoc:
options
[
:message
]
unless
file?
end
def
validate_content_type
options
#:nodoc:
valid_types
=
[
options
[
:content_type
]].
flatten
unless
original_filename
.
blank?
unless
valid_types
.
blank?
content_type
=
instance_read
(
:content_type
)
unless
valid_types
.
any?
{
|
t
|
content_type
.
nil?
||
t
===
content_type
}
options
[
:message
]
||
"is not one of the allowed file types."
end
end
end
end
def
initialize_storage
#:nodoc:
@storage_module
=
Paperclip
::
Storage
.
const_get
(
@storage
.
to_s
.
capitalize
)
self
.
extend
(
@storage_module
)
...
...
test/attachment_test.rb
View file @
3eb7b902
...
...
@@ -14,18 +14,6 @@ class AttachmentTest < Test::Unit::TestCase
assert_equal
"
#{
RAILS_ROOT
}
/public/fake_models/1234/fake"
,
@attachment
.
path
end
should
"call a proc sent to check_guard"
do
@dummy
=
Dummy
.
new
@dummy
.
expects
(
:one
).
returns
(
:one
)
assert_equal
:one
,
@dummy
.
avatar
.
send
(
:check_guard
,
lambda
{
|
x
|
x
.
one
})
end
should
"call a method name sent to check_guard"
do
@dummy
=
Dummy
.
new
@dummy
.
expects
(
:one
).
returns
(
:one
)
assert_equal
:one
,
@dummy
.
avatar
.
send
(
:check_guard
,
:one
)
end
context
"Attachment default_options"
do
setup
do
rebuild_model
...
...
@@ -474,8 +462,6 @@ class AttachmentTest < Test::Unit::TestCase
@attachment
.
expects
(
:valid_assignment?
).
with
(
@not_file
).
returns
(
true
)
@attachment
.
expects
(
:queue_existing_for_delete
)
@attachment
.
expects
(
:post_process
)
@attachment
.
expects
(
:valid?
).
returns
(
true
)
@attachment
.
expects
(
:validate
)
@dummy
.
avatar
=
@not_file
end
...
...
test/paperclip_test.rb
View file @
3eb7b902
...
...
@@ -185,27 +185,6 @@ class PaperclipTest < Test::Unit::TestCase
should
"be valid"
do
assert
@dummy
.
valid?
end
context
"then has a validation added that makes it invalid"
do
setup
do
assert
@dummy
.
save
Dummy
.
class_eval
do
validates_attachment_content_type
:avatar
,
:content_type
=>
[
"text/plain"
]
end
@dummy2
=
Dummy
.
find
(
@dummy
.
id
)
end
should
"be invalid when reloaded"
do
assert
!
@dummy2
.
valid?
,
@dummy2
.
errors
.
inspect
end
should
"be able to call #valid? twice without having duplicate errors"
do
@dummy2
.
avatar
.
valid?
first_errors
=
@dummy2
.
avatar
.
errors
@dummy2
.
avatar
.
valid?
assert_equal
first_errors
,
@dummy2
.
avatar
.
errors
end
end
end
context
"a validation with an if guard clause"
do
...
...
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