Commit d9de7ba5 by Prem Sichanugrist

Use ActiveSupport::Delegation to delegate method

parent 2059c5ea
require 'active_support/core_ext/module/delegation'
module Paperclip module Paperclip
class AbstractAdapter class AbstractAdapter
attr_reader :content_type, :original_filename, :size
def original_filename delegate :close, :closed?, :eof?, :path, :rewind, :to => :@tempfile
@original_filename
end
def content_type
@content_type
end
def size
@size
end
def fingerprint def fingerprint
@fingerprint ||= Digest::MD5.file(path).to_s @fingerprint ||= Digest::MD5.file(path).to_s
end end
def nil?
false
end
def read(length = nil, buffer = nil) def read(length = nil, buffer = nil)
@tempfile.read(length, buffer) @tempfile.read(length, buffer)
end end
# We don't use this directly, but aws/sdk does.
def rewind
@tempfile.rewind
end
def eof?
@tempfile.eof?
end
def path
@tempfile.path
end
def close
@tempfile.close
end
def closed?
@tempfile.closed?
end
private private
def destination def destination
......
...@@ -2,7 +2,7 @@ require './test/helper' ...@@ -2,7 +2,7 @@ require './test/helper'
class AbstractAdapterTest < Test::Unit::TestCase class AbstractAdapterTest < Test::Unit::TestCase
class TestAdapter < Paperclip::AbstractAdapter class TestAdapter < Paperclip::AbstractAdapter
attr_accessor :path, :original_file_name, :tempfile attr_accessor :original_file_name, :tempfile
def content_type def content_type
type_from_file_command type_from_file_command
...@@ -11,11 +11,19 @@ class AbstractAdapterTest < Test::Unit::TestCase ...@@ -11,11 +11,19 @@ class AbstractAdapterTest < Test::Unit::TestCase
context "content type from file command" do context "content type from file command" do
setup do setup do
@adapter = TestAdapter.new
@adapter.stubs(:path)
Paperclip.stubs(:run).returns("image/png\n") Paperclip.stubs(:run).returns("image/png\n")
end end
should "return the content type without newline" do should "return the content type without newline" do
assert_equal "image/png", TestAdapter.new.content_type assert_equal "image/png", @adapter.content_type
end
end
context "nil?" do
should "return false" do
assert !TestAdapter.new.nil?
end end
end end
...@@ -25,19 +33,11 @@ class AbstractAdapterTest < Test::Unit::TestCase ...@@ -25,19 +33,11 @@ class AbstractAdapterTest < Test::Unit::TestCase
@adapter.tempfile = stub("Tempfile") @adapter.tempfile = stub("Tempfile")
end end
context "close" do [:close, :closed?, :eof?, :path, :rewind].each do |method|
should "delegate to tempfile" do should "delegate #{method} to @tempfile" do
@adapter.tempfile.stubs(:close) @adapter.tempfile.stubs(method)
@adapter.close @adapter.public_send(method)
assert_received @adapter.tempfile, :close assert_received @adapter.tempfile, method
end
end
context "closed?" do
should "delegate to tempfile" do
@adapter.tempfile.stubs(:closed?)
@adapter.closed?
assert_received @adapter.tempfile, :closed?
end 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