Commit 24d14397 by Geoffrey Hichborn Committed by Mike Burns

Added support for Paperclip::Style in AttachmentAdapter

parent e9f825a5
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
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
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