Commit d9de7ba5 by Prem Sichanugrist

Use ActiveSupport::Delegation to delegate method

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