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
93faccfa
Commit
93faccfa
authored
Oct 20, 2011
by
Jon Yurek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Attachment#url's second argument should be a hash, but can be a boolean for backwards' sake
parent
94d35f98
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
77 additions
and
9 deletions
+77
-9
lib/paperclip.rb
+3
-4
lib/paperclip/attachment.rb
+44
-5
test/attachment_test.rb
+30
-0
No files found.
lib/paperclip.rb
View file @
93faccfa
...
...
@@ -387,10 +387,9 @@ module Paperclip
# * +unless+: Same as +if+ but validates if lambda or method returns false.
def
validates_attachment_presence
name
,
options
=
{}
message
=
options
[
:message
]
||
:empty
validates_presence_of
:"
#{
name
}
_file_name"
,
:message
=>
message
,
:if
=>
options
[
:if
],
:unless
=>
options
[
:unless
]
validates_each
:"
#{
name
}
_file_name"
do
|
record
,
attr
,
value
|
record
.
errors
.
add
(
name
,
message
)
if
attr
.
blank?
end
end
# Places ActiveRecord-style validations on the content type of the file
...
...
lib/paperclip/attachment.rb
View file @
93faccfa
...
...
@@ -128,12 +128,13 @@ module Paperclip
# grained security. This is not recommended if you don't need the
# security, however, for performance reasons. Set use_timestamp to false
# if you want to stop the attachment update time appended to the url
def
url
(
style_name
=
default_style
,
use_timestamp
=
@options
.
use_timestamp
)
default_url
=
@options
.
default_url
.
is_a?
(
Proc
)
?
@options
.
default_url
.
call
(
self
)
:
@options
.
default_url
url
=
original_filename
.
nil?
?
interpolate
(
default_url
,
style_name
)
:
interpolate
(
@options
.
url
,
style_name
)
def
url
(
style_name
=
default_style
,
options
=
{}
)
options
=
handle_url_options
(
options
)
url
=
interpolate
(
most_appropriate_
url
,
style_name
)
url
<<
(
url
.
include?
(
"?"
)
?
"&"
:
"?"
)
+
updated_at
.
to_s
if
use_timestamp
&&
updated_at
url
.
respond_to?
(
:escape
)
?
url
.
escape
:
URI
.
escape
(
url
)
url
=
url_timestamp
(
url
)
if
options
[
:timestamp
]
url
=
escape_url
(
url
)
if
options
[
:escape
]
url
end
# Returns the path of the attachment as defined by the :path option. If the
...
...
@@ -320,6 +321,44 @@ module Paperclip
private
def
handle_url_options
(
options
)
timestamp
=
extract_timestamp
(
options
)
options
=
{}
if
options
==
true
||
options
==
false
options
[
:timestamp
]
=
timestamp
options
[
:escape
]
=
true
if
options
[
:escape
].
nil?
options
end
def
extract_timestamp
(
options
)
possibilities
=
[((
options
==
true
||
options
==
false
)
?
options
:
nil
),
(
options
.
respond_to?
(
:[]
)
?
options
[:
timestamp
]
:
nil
),
@options
.
use_timestamp
]
possibilities
.
find
{
|
n
|
!
n
.
nil?
}
end
def
default_url
return
@options
.
default_url
.
call
(
self
)
if
@options
.
default_url
.
is_a?
(
Proc
)
@options
.
default_url
end
def
most_appropriate_url
if
original_filename
.
nil?
default_url
else
@options
.
url
end
end
def
url_timestamp
(
url
)
return
url
unless
updated_at
delimiter_char
=
url
.
include?
(
"?"
)
?
"&"
:
"?"
"
#{
url
}#{
delimiter_char
}#{
updated_at
.
to_s
}
"
end
def
escape_url
(
url
)
url
.
respond_to?
(
:escape
)
?
url
.
escape
:
URI
.
escape
(
url
)
end
def
ensure_required_accessors!
#:nodoc:
%w(file_name)
.
each
do
|
field
|
unless
@instance
.
respond_to?
(
"
#{
name
}
_
#{
field
}
"
)
&&
@instance
.
respond_to?
(
"
#{
name
}
_
#{
field
}
="
)
...
...
test/attachment_test.rb
View file @
93faccfa
...
...
@@ -160,6 +160,36 @@ class AttachmentTest < Test::Unit::TestCase
end
end
context
"An attachment"
do
setup
do
@file
=
StringIO
.
new
(
"..."
)
end
context
"using default time zone"
do
setup
do
rebuild_model
:url
=>
"X"
@dummy
=
Dummy
.
new
@dummy
.
avatar
=
@file
end
should
"generate a url with a timestamp when passing true"
do
assert_equal
"X?
#{
@dummy
.
avatar_updated_at
.
to_i
.
to_s
}
"
,
@dummy
.
avatar
.
url
(
:style
,
true
)
end
should
"not generate a url with a timestamp when passing false"
do
assert_equal
"X"
,
@dummy
.
avatar
.
url
(
:style
,
false
)
end
should
"generate a url with a timestamp when setting a timestamp option"
do
assert_equal
"X?
#{
@dummy
.
avatar_updated_at
.
to_i
.
to_s
}
"
,
@dummy
.
avatar
.
url
(
:style
,
:timestamp
=>
true
)
end
should
"not generate a url with a timestamp when setting a timestamp option to false"
do
assert_equal
"X"
,
@dummy
.
avatar
.
url
(
:style
,
:timestamp
=>
false
)
end
end
end
context
"An attachment with :hash interpolations"
do
setup
do
@file
=
StringIO
.
new
(
"..."
)
...
...
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