Skip to content

Commit

Permalink
shoulda macro and webrat step for testing paperclip on s3
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Croak committed Aug 24, 2009
1 parent 4beeb7a commit 98b90bc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cucumber/paperclip_steps.rb
@@ -0,0 +1,6 @@
When /^I attach an? "([^\"]*)" "([^\"]*)" file to an? "([^\"]*)" on S3$/ do |attachment, extension, model|
stub_paperclip_s3(model, attachment, extension)
attach_file attachment,
"features/support/paperclip/#{model.gsub(" ", "_").underscore}/#{attachment}.#{extension}"
end

48 changes: 48 additions & 0 deletions shoulda_macros/paperclip.rb
Expand Up @@ -60,9 +60,57 @@ def should_validate_attachment_size name, options = {}
assert_accepts(matcher, klass)
end
end

# Stubs the HTTP PUT for an attachment using S3 storage.
#
# @example
# stub_paperclip_s3('user', 'avatar', 'png')
def stub_paperclip_s3(model, attachment, extension)
definition = model.gsub(" ", "_").classify.constantize.
attachment_definitions[attachment.to_sym]

path = "http://s3.amazonaws.com/:id/#{definition[:path]}"
path.gsub!(/:([^\/\.]+)/) do |match|
"([^\/\.]+)"
end

begin
FakeWeb.register_uri(:put, Regexp.new(path), :body => "OK")
rescue NameError
raise NameError, "the stub_paperclip_s3 shoulda macro requires the fakeweb gem."
end
end

# Stub S3 and return a file for attachment. Best with Factory Girl.
# Uses a strict directory convention:
#
# features/support/paperclip
#
# This method is used by the Paperclip-provided Cucumber step:
#
# When I attach a "demo_tape" "mp3" file to a "band" on S3
#
# @example
# Factory.define :band_with_demo_tape, :parent => :band do |band|
# band.demo_tape { band.paperclip_fixture("band", "demo_tape", "png") }
# end
def paperclip_fixture(model, attachment, extension)
stub_paperclip_s3(model, attachment, extension)
base_path = File.join(File.dirname(__FILE__), "..", "..",
"features", "support", "paperclip")
File.new(File.join(base_path, model, "#{attachment}.#{extension}"))
end
end
end

class ActionController::Integration::Session #:nodoc:
include Paperclip::Shoulda
end

class Factory
include Paperclip::Shoulda #:nodoc:
end

class Test::Unit::TestCase #:nodoc:
extend Paperclip::Shoulda
end

0 comments on commit 98b90bc

Please sign in to comment.