Commit 24d14397 by Geoffrey Hichborn Committed by Mike Burns

Added support for Paperclip::Style in AttachmentAdapter

parent e9f825a5
module Paperclip module Paperclip
class AttachmentAdapter class AttachmentAdapter
def initialize(target) 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 cache_current_values
end end
...@@ -57,9 +63,9 @@ module Paperclip ...@@ -57,9 +63,9 @@ module Paperclip
dest = Tempfile.new([basename, extension]) dest = Tempfile.new([basename, extension])
dest.binmode dest.binmode
if src.respond_to? :copy_to_local_file 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 else
FileUtils.cp(src.path(:original), dest.path) FileUtils.cp(src.path(@style), dest.path)
end end
dest dest
end end
...@@ -67,5 +73,5 @@ module Paperclip ...@@ -67,5 +73,5 @@ module Paperclip
end end
Paperclip.io_adapters.register Paperclip::AttachmentAdapter do |target| Paperclip.io_adapters.register Paperclip::AttachmentAdapter do |target|
Paperclip::Attachment === target Paperclip::Attachment === target || Paperclip::Style === target
end end
require './test/helper' require './test/helper'
class AttachmentAdapterTest < Test::Unit::TestCase class AttachmentAdapterTest < Test::Unit::TestCase
def setup 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 @attachment = Dummy.new.avatar
end
context "for an attachment" do
setup do
@file = File.new(fixture_file("5k.png")) @file = File.new(fixture_file("5k.png"))
@file.binmode @file.binmode
...@@ -12,7 +17,7 @@ class AttachmentAdapterTest < Test::Unit::TestCase ...@@ -12,7 +17,7 @@ class AttachmentAdapterTest < Test::Unit::TestCase
@subject = Paperclip.io_adapters.for(@attachment) @subject = Paperclip.io_adapters.for(@attachment)
end end
def teardown teardown do
@file.close @file.close
end end
...@@ -48,4 +53,59 @@ class AttachmentAdapterTest < Test::Unit::TestCase ...@@ -48,4 +53,59 @@ class AttachmentAdapterTest < Test::Unit::TestCase
assert_equal expected.length, actual.length assert_equal expected.length, actual.length
assert_equal expected, actual assert_equal expected, actual
end 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 end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment