Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use ActiveSupport::Delegation to delegate method

  • Loading branch information...
commit d9de7ba5d2190c45c616eb95b51e02ff8a0582a1 1 parent 2059c5e
@sikachu sikachu authored
View
41 lib/paperclip/io_adapters/abstract_adapter.rb
@@ -1,51 +1,18 @@
+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
View
30 test/io_adapters/abstract_adapter_test.rb
@@ -2,7 +2,7 @@
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 @@ def content_type
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 @@ def content_type
@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
Please sign in to comment.
Something went wrong with that request. Please try again.