@@ -60,9 +60,57 @@ def should_validate_attachment_size name, options = {}
60
60
assert_accepts ( matcher , klass )
61
61
end
62
62
end
63
+
64
+ # Stubs the HTTP PUT for an attachment using S3 storage.
65
+ #
66
+ # @example
67
+ # stub_paperclip_s3('user', 'avatar', 'png')
68
+ def stub_paperclip_s3 ( model , attachment , extension )
69
+ definition = model . gsub ( " " , "_" ) . classify . constantize .
70
+ attachment_definitions [ attachment . to_sym ]
71
+
72
+ path = "http://s3.amazonaws.com/:id/#{ definition [ :path ] } "
73
+ path . gsub! ( /:([^\/ \. ]+)/ ) do |match |
74
+ "([^\/ \. ]+)"
75
+ end
76
+
77
+ begin
78
+ FakeWeb . register_uri ( :put , Regexp . new ( path ) , :body => "OK" )
79
+ rescue NameError
80
+ raise NameError , "the stub_paperclip_s3 shoulda macro requires the fakeweb gem."
81
+ end
82
+ end
83
+
84
+ # Stub S3 and return a file for attachment. Best with Factory Girl.
85
+ # Uses a strict directory convention:
86
+ #
87
+ # features/support/paperclip
88
+ #
89
+ # This method is used by the Paperclip-provided Cucumber step:
90
+ #
91
+ # When I attach a "demo_tape" "mp3" file to a "band" on S3
92
+ #
93
+ # @example
94
+ # Factory.define :band_with_demo_tape, :parent => :band do |band|
95
+ # band.demo_tape { band.paperclip_fixture("band", "demo_tape", "png") }
96
+ # end
97
+ def paperclip_fixture ( model , attachment , extension )
98
+ stub_paperclip_s3 ( model , attachment , extension )
99
+ base_path = File . join ( File . dirname ( __FILE__ ) , ".." , ".." ,
100
+ "features" , "support" , "paperclip" )
101
+ File . new ( File . join ( base_path , model , "#{ attachment } .#{ extension } " ) )
102
+ end
63
103
end
64
104
end
65
105
106
+ class ActionController ::Integration ::Session #:nodoc:
107
+ include Paperclip ::Shoulda
108
+ end
109
+
110
+ class Factory
111
+ include Paperclip ::Shoulda #:nodoc:
112
+ end
113
+
66
114
class Test ::Unit ::TestCase #:nodoc:
67
115
extend Paperclip ::Shoulda
68
116
end
0 commit comments