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
24d14397
Commit
24d14397
authored
May 31, 2012
by
Geoffrey Hichborn
Committed by
Mike Burns
May 31, 2012
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added support for Paperclip::Style in AttachmentAdapter
parent
e9f825a5
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
6 deletions
+72
-6
lib/paperclip/io_adapters/attachment_adapter.rb
+10
-4
test/io_adapters/attachment_adapter_test.rb
+62
-2
No files found.
lib/paperclip/io_adapters/attachment_adapter.rb
View file @
24d14397
module
Paperclip
class
AttachmentAdapter
def
initialize
(
target
)
@target
=
target
@target
,
@style
=
case
target
when
Paperclip
::
Attachment
[
target
,
:original
]
when
Paperclip
::
Style
[
target
.
attachment
,
target
.
name
]
end
cache_current_values
end
...
...
@@ -57,9 +63,9 @@ module Paperclip
dest
=
Tempfile
.
new
([
basename
,
extension
])
dest
.
binmode
if
src
.
respond_to?
:copy_to_local_file
src
.
copy_to_local_file
(
:original
,
dest
.
path
)
src
.
copy_to_local_file
(
@style
,
dest
.
path
)
else
FileUtils
.
cp
(
src
.
path
(
:original
),
dest
.
path
)
FileUtils
.
cp
(
src
.
path
(
@style
),
dest
.
path
)
end
dest
end
...
...
@@ -67,5 +73,5 @@ module Paperclip
end
Paperclip
.
io_adapters
.
register
Paperclip
::
AttachmentAdapter
do
|
target
|
Paperclip
::
Attachment
===
target
Paperclip
::
Attachment
===
target
||
Paperclip
::
Style
===
target
end
test/io_adapters/attachment_adapter_test.rb
View file @
24d14397
require
'./test/helper'
class
AttachmentAdapterTest
<
Test
::
Unit
::
TestCase
def
setup
rebuild_model
:path
=>
"tmp/:class/:attachment/:style/:filename"
rebuild_model
:path
=>
"tmp/:class/:attachment/:style/:filename"
,
:styles
=>
{
:thumb
=>
'50x50'
}
@attachment
=
Dummy
.
new
.
avatar
end
context
"for an attachment"
do
setup
do
@file
=
File
.
new
(
fixture_file
(
"5k.png"
))
@file
.
binmode
...
...
@@ -12,7 +17,7 @@ class AttachmentAdapterTest < Test::Unit::TestCase
@subject
=
Paperclip
.
io_adapters
.
for
(
@attachment
)
end
def
teardown
teardown
do
@file
.
close
end
...
...
@@ -48,4 +53,59 @@ class AttachmentAdapterTest < Test::Unit::TestCase
assert_equal
expected
.
length
,
actual
.
length
assert_equal
expected
,
actual
end
end
context
"for a style"
do
setup
do
@file
=
File
.
new
(
fixture_file
(
"5k.png"
))
@file
.
binmode
@attachment
.
assign
(
@file
)
@thumb
=
@attachment
.
queued_for_write
[
:thumb
]
@attachment
.
save
@subject
=
Paperclip
.
io_adapters
.
for
(
@attachment
.
styles
[
:thumb
])
end
teardown
do
@file
.
close
end
should
"get the original filename"
do
assert_equal
"5k.png"
,
@subject
.
original_filename
end
should
"force binmode on tempfile"
do
assert
@subject
.
instance_variable_get
(
"@tempfile"
).
binmode?
end
should
"get the content type"
do
assert_equal
"image/png"
,
@subject
.
content_type
end
should
"get the thumbnail's file size"
do
assert_equal
@thumb
.
size
,
@subject
.
size
end
should
"return false for a call to nil?"
do
assert
!
@subject
.
nil?
end
should
"generate a MD5 hash of the contents"
do
expected
=
Digest
::
MD5
.
file
(
@thumb
.
path
).
to_s
assert_equal
expected
,
@subject
.
fingerprint
end
should
"read the contents of the thumbnail"
do
@thumb
.
rewind
expected
=
@thumb
.
read
actual
=
@subject
.
read
assert
expected
.
length
>
0
assert_equal
expected
.
length
,
actual
.
length
assert_equal
expected
,
actual
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