paperclip-cloudfiles 2.3.1.1.6 → 2.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,15 @@
1
1
  module Paperclip
2
2
  module Shoulda
3
3
  module Matchers
4
+ # Ensures that the given instance or class validates the content type of
5
+ # the given attachment as specified.
6
+ #
7
+ # Example:
8
+ # describe User do
9
+ # it { should validate_attachment_content_type(:icon).
10
+ # allowing('image/png', 'image/gif').
11
+ # rejecting('text/plain', 'text/xml') }
12
+ # end
4
13
  def validate_attachment_content_type name
5
14
  ValidateAttachmentContentTypeMatcher.new(name)
6
15
  end
@@ -22,6 +31,7 @@ module Paperclip
22
31
 
23
32
  def matches? subject
24
33
  @subject = subject
34
+ @subject = @subject.class unless Class === @subject
25
35
  @allowed_types && @rejected_types &&
26
36
  allowed_types_allowed? && rejected_types_rejected?
27
37
  end
@@ -47,7 +57,7 @@ module Paperclip
47
57
  file = StringIO.new(".")
48
58
  file.content_type = type
49
59
  (subject = @subject.new).attachment_for(@attachment_name).assign(file)
50
- subject.valid? && subject.errors.on(:"#{@attachment_name}_content_type").blank?
60
+ subject.valid? && subject.errors[:"#{@attachment_name}_content_type"].blank?
51
61
  end
52
62
  end
53
63
 
@@ -1,6 +1,12 @@
1
1
  module Paperclip
2
2
  module Shoulda
3
3
  module Matchers
4
+ # Ensures that the given instance or class validates the presence of the
5
+ # given attachment.
6
+ #
7
+ # describe User do
8
+ # it { should validate_attachment_presence(:avatar) }
9
+ # end
4
10
  def validate_attachment_presence name
5
11
  ValidateAttachmentPresenceMatcher.new(name)
6
12
  end
@@ -12,6 +18,7 @@ module Paperclip
12
18
 
13
19
  def matches? subject
14
20
  @subject = subject
21
+ @subject = @subject.class unless Class === @subject
15
22
  error_when_not_valid? && no_error_when_valid?
16
23
  end
17
24
 
@@ -32,14 +39,14 @@ module Paperclip
32
39
  def error_when_not_valid?
33
40
  (subject = @subject.new).send(@attachment_name).assign(nil)
34
41
  subject.valid?
35
- not subject.errors.on(:"#{@attachment_name}_file_name").blank?
42
+ not subject.errors[:"#{@attachment_name}_file_name"].blank?
36
43
  end
37
44
 
38
45
  def no_error_when_valid?
39
46
  @file = StringIO.new(".")
40
47
  (subject = @subject.new).send(@attachment_name).assign(@file)
41
48
  subject.valid?
42
- subject.errors.on(:"#{@attachment_name}_file_name").blank?
49
+ subject.errors[:"#{@attachment_name}_file_name"].blank?
43
50
  end
44
51
  end
45
52
  end
@@ -1,6 +1,16 @@
1
1
  module Paperclip
2
2
  module Shoulda
3
3
  module Matchers
4
+ # Ensures that the given instance or class validates the size of the
5
+ # given attachment as specified.
6
+ #
7
+ # Examples:
8
+ # it { should validate_attachment_size(:avatar).
9
+ # less_than(2.megabytes) }
10
+ # it { should validate_attachment_size(:icon).
11
+ # greater_than(1024) }
12
+ # it { should validate_attachment_size(:icon).
13
+ # in(0..100) }
4
14
  def validate_attachment_size name
5
15
  ValidateAttachmentSizeMatcher.new(name)
6
16
  end
@@ -28,6 +38,7 @@ module Paperclip
28
38
 
29
39
  def matches? subject
30
40
  @subject = subject
41
+ @subject = @subject.class unless Class === @subject
31
42
  lower_than_low? && higher_than_low? && lower_than_high? && higher_than_high?
32
43
  end
33
44
 
@@ -58,7 +69,7 @@ module Paperclip
58
69
 
59
70
  (subject = @subject.new).send(@attachment_name).assign(file)
60
71
  subject.valid?
61
- subject.errors.on(:"#{@attachment_name}_file_size").blank?
72
+ subject.errors[:"#{@attachment_name}_file_size"].blank?
62
73
  end
63
74
 
64
75
  def lower_than_low?
@@ -8,18 +8,18 @@ module Paperclip
8
8
  # * +path+: The location of the repository of attachments on disk. This can (and, in
9
9
  # almost all cases, should) be coordinated with the value of the +url+ option to
10
10
  # allow files to be saved into a place where Apache can serve them without
11
- # hitting your app. Defaults to
11
+ # hitting your app. Defaults to
12
12
  # ":rails_root/public/:attachment/:id/:style/:basename.:extension"
13
- # By default this places the files in the app's public directory which can be served
14
- # directly. If you are using capistrano for deployment, a good idea would be to
15
- # make a symlink to the capistrano-created system directory from inside your app's
13
+ # By default this places the files in the app's public directory which can be served
14
+ # directly. If you are using capistrano for deployment, a good idea would be to
15
+ # make a symlink to the capistrano-created system directory from inside your app's
16
16
  # public directory.
17
17
  # See Paperclip::Attachment#interpolate for more information on variable interpolaton.
18
18
  # :path => "/var/app/attachments/:class/:id/:style/:basename.:extension"
19
19
  module Filesystem
20
20
  def self.extended base
21
21
  end
22
-
22
+
23
23
  def exists?(style_name = default_style)
24
24
  if original_filename
25
25
  File.exist?(path(style_name))
@@ -78,25 +78,25 @@ module Paperclip
78
78
  # database.yml file, so different environments can use different accounts:
79
79
  # development:
80
80
  # access_key_id: 123...
81
- # secret_access_key: 123...
81
+ # secret_access_key: 123...
82
82
  # test:
83
83
  # access_key_id: abc...
84
- # secret_access_key: abc...
84
+ # secret_access_key: abc...
85
85
  # production:
86
86
  # access_key_id: 456...
87
- # secret_access_key: 456...
87
+ # secret_access_key: 456...
88
88
  # This is not required, however, and the file may simply look like this:
89
89
  # access_key_id: 456...
90
- # secret_access_key: 456...
90
+ # secret_access_key: 456...
91
91
  # In which case, those access keys will be used in all environments. You can also
92
92
  # put your bucket name in this file, instead of adding it to the code directly.
93
- # This is useful when you want the same account but a different bucket for
93
+ # This is useful when you want the same account but a different bucket for
94
94
  # development versus production.
95
95
  # * +s3_permissions+: This is a String that should be one of the "canned" access
96
96
  # policies that S3 provides (more information can be found here:
97
97
  # http://docs.amazonwebservices.com/AmazonS3/2006-03-01/RESTAccessPolicy.html#RESTCannedAccessPolicies)
98
98
  # The default for Paperclip is :public_read.
99
- # * +s3_protocol+: The protocol for the URLs generated to your S3 assets. Can be either
99
+ # * +s3_protocol+: The protocol for the URLs generated to your S3 assets. Can be either
100
100
  # 'http' or 'https'. Defaults to 'http' when your :s3_permissions are :public_read (the
101
101
  # default), and 'https' when your :s3_permissions are anything else.
102
102
  # * +s3_headers+: A hash of headers such as {'Expires' => 1.year.from_now.httpdate}
@@ -111,7 +111,7 @@ module Paperclip
111
111
  # * +url+: There are three options for the S3 url. You can choose to have the bucket's name
112
112
  # placed domain-style (bucket.s3.amazonaws.com) or path-style (s3.amazonaws.com/bucket).
113
113
  # Lastly, you can specify a CNAME (which requires the CNAME to be specified as
114
- # :s3_alias_url. You can read more about CNAMEs and S3 at
114
+ # :s3_alias_url. You can read more about CNAMEs and S3 at
115
115
  # http://docs.amazonwebservices.com/AmazonS3/latest/index.html?VirtualHosting.html
116
116
  # Normally, this won't matter in the slightest and you can leave the default (which is
117
117
  # path-style, or :s3_path_url). But in some cases paths don't work and you need to use
@@ -159,7 +159,7 @@ module Paperclip
159
159
  "#{attachment.s3_protocol}://#{attachment.bucket_name}.s3.amazonaws.com/#{attachment.path(style).gsub(%r{^/}, "")}"
160
160
  end
161
161
  end
162
-
162
+
163
163
  def expiring_url(time = 3600)
164
164
  AWS::S3::S3Object.url_for(path, bucket_name, :expires_in => time )
165
165
  end
@@ -174,9 +174,9 @@ module Paperclip
174
174
 
175
175
  def parse_credentials creds
176
176
  creds = find_credentials(creds).stringify_keys
177
- (creds[RAILS_ENV] || creds).symbolize_keys
177
+ (creds[Rails.env] || creds).symbolize_keys
178
178
  end
179
-
179
+
180
180
  def exists?(style = default_style)
181
181
  if original_filename
182
182
  AWS::S3::S3Object.exists?(path(style), bucket_name)
@@ -227,12 +227,12 @@ module Paperclip
227
227
  end
228
228
  @queued_for_delete = []
229
229
  end
230
-
230
+
231
231
  def find_credentials creds
232
232
  case creds
233
233
  when File
234
234
  YAML::load(ERB.new(File.read(creds.path)).result)
235
- when String
235
+ when String, Pathname
236
236
  YAML::load(ERB.new(File.read(creds)).result)
237
237
  when Hash
238
238
  creds
@@ -23,6 +23,9 @@ module Paperclip
23
23
  @whiny = options[:whiny].nil? ? true : options[:whiny]
24
24
  @format = options[:format]
25
25
 
26
+ @source_file_options = @source_file_options.split(/\s+/) if @source_file_options.respond_to?(:split)
27
+ @convert_options = @convert_options.split(/\s+/) if @convert_options.respond_to?(:split)
28
+
26
29
  @current_format = File.extname(@file.path)
27
30
  @basename = File.basename(@file.path, @current_format)
28
31
 
@@ -45,16 +48,17 @@ module Paperclip
45
48
  dst = Tempfile.new([@basename, @format].compact.join("."))
46
49
  dst.binmode
47
50
 
48
- command = <<-end_command
49
- #{ source_file_options }
50
- "#{ File.expand_path(src.path) }[0]"
51
- #{ transformation_command }
52
- "#{ File.expand_path(dst.path) }"
53
- end_command
54
-
55
51
  begin
56
- success = Paperclip.run("convert", command.gsub(/\s+/, " "))
57
- rescue PaperclipCommandLineError
52
+ options = [
53
+ source_file_options,
54
+ "#{ File.expand_path(src.path) }[0]",
55
+ transformation_command,
56
+ convert_options,
57
+ "#{ File.expand_path(dst.path) }"
58
+ ].flatten.compact
59
+
60
+ success = Paperclip.run("convert", *options)
61
+ rescue PaperclipCommandLineError => e
58
62
  raise PaperclipError, "There was an error processing the thumbnail for #{@basename}" if @whiny
59
63
  end
60
64
 
@@ -65,10 +69,9 @@ module Paperclip
65
69
  # into the thumbnail.
66
70
  def transformation_command
67
71
  scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
68
- trans = ""
69
- trans << " -resize \"#{scale}\"" unless scale.nil? || scale.empty?
70
- trans << " -crop \"#{crop}\" +repage" if crop
71
- trans << " #{convert_options}" if convert_options?
72
+ trans = []
73
+ trans << "-resize" << scale unless scale.nil? || scale.empty?
74
+ trans << "-crop" << crop << "+repage" if crop
72
75
  trans
73
76
  end
74
77
  end
@@ -15,7 +15,11 @@ module Paperclip
15
15
  when %r"html?" then "text/html"
16
16
  when "js" then "application/js"
17
17
  when "csv", "xml", "css" then "text/#{type}"
18
- else "application/x-#{type}"
18
+ else
19
+ # On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
20
+ content_type = (Paperclip.run("file", "--mime-type", self.path).split(':').last.strip rescue "application/x-#{type}")
21
+ content_type = "application/x-#{type}" if content_type.match(/\(.*?\)/)
22
+ content_type
19
23
  end
20
24
  end
21
25
 
@@ -104,8 +104,10 @@ module Paperclip
104
104
  end
105
105
  end
106
106
 
107
- class ActionController::Integration::Session #:nodoc:
108
- include Paperclip::Shoulda
107
+ if defined?(ActionController::Integration::Session)
108
+ class ActionController::Integration::Session #:nodoc:
109
+ include Paperclip::Shoulda
110
+ end
109
111
  end
110
112
 
111
113
  class Factory
@@ -11,7 +11,7 @@ class AttachmentTest < Test::Unit::TestCase
11
11
  @model = @attachment.instance
12
12
  @model.id = 1234
13
13
  @model.avatar_file_name = "fake.jpg"
14
- assert_equal "#{RAILS_ROOT}/public/fake_models/1234/fake", @attachment.path
14
+ assert_equal "#{Rails.root}/public/fake_models/1234/fake", @attachment.path
15
15
  end
16
16
 
17
17
  should "call a proc sent to check_guard" do
@@ -118,12 +118,11 @@ class AttachmentTest < Test::Unit::TestCase
118
118
  @dummy.stubs(:id).returns(@id)
119
119
  @file = StringIO.new(".")
120
120
  @dummy.avatar = @file
121
+ Rails.stubs(:env).returns(@rails_env)
121
122
  end
122
123
 
123
124
  should "return the proper path" do
124
- temporary_rails_env(@rails_env) {
125
- assert_equal "#{@rails_env}/#{@id}.png", @dummy.avatar.path
126
- }
125
+ assert_equal "#{@rails_env}/#{@id}.png", @dummy.avatar.path
127
126
  end
128
127
  end
129
128
 
@@ -375,7 +374,7 @@ class AttachmentTest < Test::Unit::TestCase
375
374
 
376
375
  context "Assigning an attachment with post_process hooks" do
377
376
  setup do
378
- rebuild_model :styles => { :something => "100x100#" }
377
+ rebuild_class :styles => { :something => "100x100#" }
379
378
  Dummy.class_eval do
380
379
  before_avatar_post_process :do_before_avatar
381
380
  after_avatar_post_process :do_after_avatar
@@ -415,16 +414,16 @@ class AttachmentTest < Test::Unit::TestCase
415
414
  @dummy.expects(:do_before_avatar).never
416
415
  @dummy.expects(:do_after_avatar).never
417
416
  @dummy.expects(:do_before_all).with().returns(false)
418
- @dummy.expects(:do_after_all).never
417
+ @dummy.expects(:do_after_all)
419
418
  Paperclip::Thumbnail.expects(:make).never
420
419
  @dummy.avatar = @file
421
420
  end
422
421
 
423
422
  should "cancel the processing if a before_avatar_post_process returns false" do
424
423
  @dummy.expects(:do_before_avatar).with().returns(false)
425
- @dummy.expects(:do_after_avatar).never
424
+ @dummy.expects(:do_after_avatar)
426
425
  @dummy.expects(:do_before_all).with().returns(true)
427
- @dummy.expects(:do_after_all).never
426
+ @dummy.expects(:do_after_all)
428
427
  Paperclip::Thumbnail.expects(:make).never
429
428
  @dummy.avatar = @file
430
429
  end
@@ -434,15 +433,11 @@ class AttachmentTest < Test::Unit::TestCase
434
433
  setup do
435
434
  rebuild_model :styles => { :something => "100x100#" }
436
435
  @file = StringIO.new(".")
437
- @file.expects(:original_filename).returns("5k.png\n\n")
438
- @file.expects(:content_type).returns("image/png\n\n")
436
+ @file.stubs(:original_filename).returns("5k.png\n\n")
437
+ @file.stubs(:content_type).returns("image/png\n\n")
439
438
  @file.stubs(:to_tempfile).returns(@file)
440
439
  @dummy = Dummy.new
441
440
  Paperclip::Thumbnail.expects(:make).returns(@file)
442
- @dummy.expects(:run_callbacks).with(:before_avatar_post_process, {:original => @file})
443
- @dummy.expects(:run_callbacks).with(:before_post_process, {:original => @file})
444
- @dummy.expects(:run_callbacks).with(:after_avatar_post_process, {:original => @file, :something => @file})
445
- @dummy.expects(:run_callbacks).with(:after_post_process, {:original => @file, :something => @file})
446
441
  @attachment = @dummy.avatar
447
442
  @dummy.avatar = @file
448
443
  end
@@ -479,10 +474,9 @@ class AttachmentTest < Test::Unit::TestCase
479
474
  @dummy.avatar = @not_file
480
475
  end
481
476
 
482
- should "remove strange letters and replace with underscore (_)" do
483
- assert_equal "sheep_say_b_.png", @dummy.avatar.original_filename
477
+ should "not remove strange letters" do
478
+ assert_equal "sheep_say_bæ.png", @dummy.avatar.original_filename
484
479
  end
485
-
486
480
  end
487
481
 
488
482
  context "An attachment" do
data/test/helper.rb CHANGED
@@ -1,27 +1,57 @@
1
1
  $:.reject! { |e| e.include? 'TextMate' }
2
2
 
3
3
  require 'rubygems'
4
- require 'test/unit'
5
- require 'shoulda'
6
4
  require 'tempfile'
7
5
  require 'cloudfiles'
6
+ require 'test/unit'
8
7
 
9
- gem 'jferris-mocha'
8
+ require 'shoulda'
10
9
  require 'mocha'
11
10
 
12
- gem 'sqlite3-ruby'
11
+ case ENV['RAILS_VERSION']
12
+ when '2.1' then
13
+ gem 'activerecord', '~>2.1.0'
14
+ gem 'activesupport', '~>2.1.0'
15
+ gem 'actionpack', '~>2.1.0'
16
+ when '3.0' then
17
+ gem 'activerecord', '~>3.0.0'
18
+ gem 'activesupport', '~>3.0.0'
19
+ gem 'actionpack', '~>3.0.0'
20
+ else
21
+ gem 'activerecord', '~>2.3.0'
22
+ gem 'activesupport', '~>2.3.0'
23
+ gem 'actionpack', '~>2.3.0'
24
+ end
13
25
 
14
26
  require 'active_record'
27
+ require 'active_record/version'
15
28
  require 'active_support'
29
+ require 'action_pack'
30
+
31
+ puts "Testing againt version #{ActiveRecord::VERSION::STRING}"
32
+
16
33
  begin
17
34
  require 'ruby-debug'
18
- rescue LoadError
19
- puts "ruby-debug not loaded"
35
+ rescue LoadError => e
36
+ puts "debugger disabled"
20
37
  end
21
38
 
22
- ROOT = File.join(File.dirname(__FILE__), '..')
23
- RAILS_ROOT = ROOT
24
- RAILS_ENV = "test"
39
+ ROOT = File.join(File.dirname(__FILE__), '..')
40
+
41
+ def silence_warnings
42
+ old_verbose, $VERBOSE = $VERBOSE, nil
43
+ yield
44
+ ensure
45
+ $VERBOSE = old_verbose
46
+ end
47
+
48
+ class Test::Unit::TestCase
49
+ def setup
50
+ silence_warnings do
51
+ Object.const_set(:Rails, stub('Rails', :root => ROOT, :env => 'test'))
52
+ end
53
+ end
54
+ end
25
55
 
26
56
  $LOAD_PATH << File.join(ROOT, 'lib')
27
57
  $LOAD_PATH << File.join(ROOT, 'lib', 'paperclip')
@@ -73,17 +103,6 @@ def rebuild_class options = {}
73
103
  end
74
104
  end
75
105
 
76
- def temporary_rails_env(new_env)
77
- old_env = Object.const_defined?("RAILS_ENV") ? RAILS_ENV : nil
78
- silence_warnings do
79
- Object.const_set("RAILS_ENV", new_env)
80
- end
81
- yield
82
- silence_warnings do
83
- Object.const_set("RAILS_ENV", old_env)
84
- end
85
- end
86
-
87
106
  class FakeModel
88
107
  attr_accessor :avatar_file_name,
89
108
  :avatar_file_size,
@@ -95,8 +114,9 @@ class FakeModel
95
114
  @errors ||= []
96
115
  end
97
116
 
98
- def run_callbacks name, *args
117
+ def run_paperclip_callbacks name, *args
99
118
  end
119
+
100
120
  end
101
121
 
102
122
  def attachment options
@@ -109,3 +129,23 @@ def silence_warnings
109
129
  ensure
110
130
  $VERBOSE = old_verbose
111
131
  end
132
+
133
+ def should_accept_dummy_class
134
+ should "accept the class" do
135
+ assert_accepts @matcher, @dummy_class
136
+ end
137
+
138
+ should "accept an instance of that class" do
139
+ assert_accepts @matcher, @dummy_class.new
140
+ end
141
+ end
142
+
143
+ def should_reject_dummy_class
144
+ should "reject the class" do
145
+ assert_rejects @matcher, @dummy_class
146
+ end
147
+
148
+ should "reject an instance of that class" do
149
+ assert_rejects @matcher, @dummy_class.new
150
+ end
151
+ end