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
@file = File.new(fixture_file("5k.png"))
@file.binmode
@attachment.assign(@file)
@attachment.save
@subject = Paperclip.io_adapters.for(@attachment)
end end
context "for an attachment" do
setup do
@file = File.new(fixture_file("5k.png"))
@file.binmode
def teardown @attachment.assign(@file)
@file.close @attachment.save
end @subject = Paperclip.io_adapters.for(@attachment)
end
should "get the right filename" do teardown do
assert_equal "5k.png", @subject.original_filename @file.close
end end
should "force binmode on tempfile" do should "get the right filename" do
assert @subject.instance_variable_get("@tempfile").binmode? assert_equal "5k.png", @subject.original_filename
end end
should "get the content type" do should "force binmode on tempfile" do
assert_equal "image/png", @subject.content_type assert @subject.instance_variable_get("@tempfile").binmode?
end end
should "get the file's size" do should "get the content type" do
assert_equal 4456, @subject.size assert_equal "image/png", @subject.content_type
end end
should "return false for a call to nil?" do should "get the file's size" do
assert ! @subject.nil? assert_equal 4456, @subject.size
end 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(@file.path).to_s
assert_equal expected, @subject.fingerprint
end
should "generate a MD5 hash of the contents" do should "read the contents of the file" do
expected = Digest::MD5.file(@file.path).to_s expected = @file.read
assert_equal expected, @subject.fingerprint actual = @subject.read
assert expected.length > 0
assert_equal expected.length, actual.length
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 file" do should "read the contents of the thumbnail" do
expected = @file.read @thumb.rewind
actual = @subject.read expected = @thumb.read
assert expected.length > 0 actual = @subject.read
assert_equal expected.length, actual.length assert expected.length > 0
assert_equal expected, actual assert_equal expected.length, actual.length
assert_equal expected, actual
end
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