paperclip 4.2.2 → 6.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (141) hide show
  1. checksums.yaml +5 -5
  2. data/.codeclimate.yml +17 -0
  3. data/.github/issue_template.md +3 -0
  4. data/.hound.yml +1055 -0
  5. data/.rubocop.yml +1 -0
  6. data/.travis.yml +17 -15
  7. data/Appraisals +4 -16
  8. data/CONTRIBUTING.md +19 -8
  9. data/Gemfile +5 -9
  10. data/LICENSE +1 -1
  11. data/MIGRATING-ES.md +317 -0
  12. data/MIGRATING.md +375 -0
  13. data/NEWS +184 -31
  14. data/README.md +371 -201
  15. data/RELEASING.md +17 -0
  16. data/Rakefile +2 -2
  17. data/UPGRADING +12 -9
  18. data/features/basic_integration.feature +10 -6
  19. data/features/migration.feature +0 -24
  20. data/features/step_definitions/attachment_steps.rb +41 -35
  21. data/features/step_definitions/html_steps.rb +2 -2
  22. data/features/step_definitions/rails_steps.rb +39 -38
  23. data/features/step_definitions/s3_steps.rb +2 -2
  24. data/features/step_definitions/web_steps.rb +1 -103
  25. data/features/support/env.rb +1 -0
  26. data/features/support/file_helpers.rb +2 -2
  27. data/features/support/paths.rb +1 -1
  28. data/features/support/rails.rb +0 -24
  29. data/gemfiles/4.2.gemfile +6 -8
  30. data/gemfiles/5.0.gemfile +17 -0
  31. data/lib/generators/paperclip/paperclip_generator.rb +9 -1
  32. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +1 -1
  33. data/lib/paperclip/attachment.rb +51 -26
  34. data/lib/paperclip/attachment_registry.rb +3 -2
  35. data/lib/paperclip/callbacks.rb +8 -6
  36. data/lib/paperclip/content_type_detector.rb +27 -11
  37. data/lib/paperclip/errors.rb +3 -1
  38. data/lib/paperclip/file_command_content_type_detector.rb +6 -8
  39. data/lib/paperclip/filename_cleaner.rb +0 -1
  40. data/lib/paperclip/geometry_detector_factory.rb +3 -3
  41. data/lib/paperclip/geometry_parser_factory.rb +1 -1
  42. data/lib/paperclip/glue.rb +1 -1
  43. data/lib/paperclip/has_attached_file.rb +9 -2
  44. data/lib/paperclip/helpers.rb +15 -11
  45. data/lib/paperclip/interpolations/plural_cache.rb +6 -5
  46. data/lib/paperclip/interpolations.rb +24 -14
  47. data/lib/paperclip/io_adapters/abstract_adapter.rb +32 -4
  48. data/lib/paperclip/io_adapters/attachment_adapter.rb +17 -6
  49. data/lib/paperclip/io_adapters/data_uri_adapter.rb +8 -8
  50. data/lib/paperclip/io_adapters/empty_string_adapter.rb +5 -4
  51. data/lib/paperclip/io_adapters/file_adapter.rb +12 -6
  52. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +8 -7
  53. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
  54. data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
  55. data/lib/paperclip/io_adapters/registry.rb +6 -2
  56. data/lib/paperclip/io_adapters/stringio_adapter.rb +9 -6
  57. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +10 -6
  58. data/lib/paperclip/io_adapters/uri_adapter.rb +43 -19
  59. data/lib/paperclip/logger.rb +1 -1
  60. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
  61. data/lib/paperclip/media_type_spoof_detector.rb +13 -9
  62. data/lib/paperclip/processor.rb +15 -6
  63. data/lib/paperclip/rails_environment.rb +25 -0
  64. data/lib/paperclip/schema.rb +4 -10
  65. data/lib/paperclip/storage/filesystem.rb +13 -2
  66. data/lib/paperclip/storage/fog.rb +33 -20
  67. data/lib/paperclip/storage/s3.rb +89 -70
  68. data/lib/paperclip/style.rb +0 -1
  69. data/lib/paperclip/thumbnail.rb +24 -12
  70. data/lib/paperclip/url_generator.rb +17 -13
  71. data/lib/paperclip/validators/attachment_size_validator.rb +1 -7
  72. data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +4 -0
  73. data/lib/paperclip/validators.rb +1 -1
  74. data/lib/paperclip/version.rb +3 -1
  75. data/lib/paperclip.rb +27 -13
  76. data/lib/tasks/paperclip.rake +33 -3
  77. data/paperclip.gemspec +18 -15
  78. data/spec/paperclip/attachment_definitions_spec.rb +1 -1
  79. data/spec/paperclip/attachment_processing_spec.rb +2 -5
  80. data/spec/paperclip/attachment_registry_spec.rb +84 -13
  81. data/spec/paperclip/attachment_spec.rb +147 -41
  82. data/spec/paperclip/content_type_detector_spec.rb +9 -2
  83. data/spec/paperclip/file_command_content_type_detector_spec.rb +15 -2
  84. data/spec/paperclip/filename_cleaner_spec.rb +0 -1
  85. data/spec/paperclip/geometry_spec.rb +1 -1
  86. data/spec/paperclip/glue_spec.rb +44 -0
  87. data/spec/paperclip/has_attached_file_spec.rb +24 -8
  88. data/spec/paperclip/integration_spec.rb +42 -5
  89. data/spec/paperclip/interpolations_spec.rb +21 -9
  90. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +106 -23
  91. data/spec/paperclip/io_adapters/attachment_adapter_spec.rb +6 -3
  92. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +7 -1
  93. data/spec/paperclip/io_adapters/file_adapter_spec.rb +6 -3
  94. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +51 -14
  95. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +1 -1
  96. data/spec/paperclip/io_adapters/registry_spec.rb +2 -2
  97. data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +5 -1
  98. data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +5 -5
  99. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +126 -8
  100. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +10 -0
  101. data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +1 -1
  102. data/spec/paperclip/media_type_spoof_detector_spec.rb +75 -11
  103. data/spec/paperclip/paperclip_spec.rb +15 -40
  104. data/spec/paperclip/plural_cache_spec.rb +17 -16
  105. data/spec/paperclip/processor_spec.rb +4 -4
  106. data/spec/paperclip/rails_environment_spec.rb +33 -0
  107. data/spec/paperclip/schema_spec.rb +46 -46
  108. data/spec/paperclip/storage/fog_spec.rb +63 -3
  109. data/spec/paperclip/storage/s3_live_spec.rb +20 -14
  110. data/spec/paperclip/storage/s3_spec.rb +400 -215
  111. data/spec/paperclip/style_spec.rb +0 -1
  112. data/spec/paperclip/tempfile_factory_spec.rb +4 -0
  113. data/spec/paperclip/tempfile_spec.rb +35 -0
  114. data/spec/paperclip/thumbnail_spec.rb +59 -38
  115. data/spec/paperclip/url_generator_spec.rb +55 -45
  116. data/spec/paperclip/validators/attachment_size_validator_spec.rb +26 -20
  117. data/spec/paperclip/validators_spec.rb +5 -5
  118. data/spec/spec_helper.rb +7 -1
  119. data/spec/support/assertions.rb +12 -1
  120. data/spec/support/fake_model.rb +4 -0
  121. data/spec/support/fixtures/empty.xlsx +0 -0
  122. data/spec/support/matchers/have_column.rb +11 -2
  123. data/spec/support/mock_attachment.rb +2 -0
  124. data/spec/support/mock_url_generator_builder.rb +2 -2
  125. data/spec/support/model_reconstruction.rb +11 -3
  126. data/spec/support/reporting.rb +11 -0
  127. metadata +110 -63
  128. data/RUNNING_TESTS.md +0 -4
  129. data/cucumber/paperclip_steps.rb +0 -6
  130. data/gemfiles/3.2.gemfile +0 -19
  131. data/gemfiles/4.0.gemfile +0 -19
  132. data/gemfiles/4.1.gemfile +0 -19
  133. data/lib/paperclip/locales/de.yml +0 -18
  134. data/lib/paperclip/locales/es.yml +0 -18
  135. data/lib/paperclip/locales/ja.yml +0 -18
  136. data/lib/paperclip/locales/pt-BR.yml +0 -18
  137. data/lib/paperclip/locales/zh-CN.yml +0 -18
  138. data/lib/paperclip/locales/zh-HK.yml +0 -18
  139. data/lib/paperclip/locales/zh-TW.yml +0 -18
  140. data/spec/support/mock_model.rb +0 -2
  141. data/spec/support/rails_helpers.rb +0 -7
@@ -1,4 +1,3 @@
1
- # encoding: utf-8
2
1
  require 'spec_helper'
3
2
 
4
3
  describe Paperclip::Attachment do
@@ -13,7 +12,7 @@ describe Paperclip::Attachment do
13
12
  it "is present when the file is set" do
14
13
  rebuild_class
15
14
  dummy = Dummy.new
16
- dummy.avatar = File.new(fixture_file("50x50.png"), "rb")
15
+ dummy.avatar = File.new(fixture_file("50x50.png"), "rb")
17
16
  expect(dummy.avatar).to_not be_blank
18
17
  expect(dummy.avatar).to be_present
19
18
  end
@@ -34,9 +33,9 @@ describe Paperclip::Attachment do
34
33
  it "does not delete styles that don't get reprocessed" do
35
34
  file = File.new(fixture_file("50x50.png"), 'rb')
36
35
  rebuild_class styles: {
37
- small: '100x>',
38
- large: '500x>',
39
- original: '42x42#'
36
+ small: "100x>",
37
+ large: "500x>",
38
+ original: "42x42#"
40
39
  }
41
40
 
42
41
  dummy = Dummy.new
@@ -54,6 +53,22 @@ describe Paperclip::Attachment do
54
53
  expect(dummy.avatar.path(:original)).to exist
55
54
  end
56
55
 
56
+ it "reprocess works with virtual content_type attribute" do
57
+ rebuild_class styles: { small: "100x>" }
58
+ modify_table { |t| t.remove :avatar_content_type }
59
+ Dummy.send :attr_accessor, :avatar_content_type
60
+ Dummy.validates_attachment_content_type(
61
+ :avatar,
62
+ content_type: %w(image/jpeg image/png)
63
+ )
64
+ Dummy.create!(avatar: File.new(fixture_file("50x50.png"), "rb"))
65
+
66
+ dummy = Dummy.first
67
+ dummy.avatar.reprocess!(:small)
68
+
69
+ expect(dummy.avatar.path(:small)).to exist
70
+ end
71
+
57
72
  context "having a not empty hash as a default option" do
58
73
  before do
59
74
  @old_default_options = Paperclip::Attachment.default_options.dup
@@ -75,7 +90,11 @@ describe Paperclip::Attachment do
75
90
 
76
91
  it "handles a boolean second argument to #url" do
77
92
  mock_url_generator_builder = MockUrlGeneratorBuilder.new
78
- attachment = Paperclip::Attachment.new(:name, :instance, url_generator: mock_url_generator_builder)
93
+ attachment = Paperclip::Attachment.new(
94
+ :name,
95
+ FakeModel.new,
96
+ url_generator: mock_url_generator_builder
97
+ )
79
98
 
80
99
  attachment.url(:style_name, true)
81
100
  expect(mock_url_generator_builder.has_generated_url_with_options?(timestamp: true, escape: true)).to eq true
@@ -86,7 +105,11 @@ describe Paperclip::Attachment do
86
105
 
87
106
  it "passes the style and options through to the URL generator on #url" do
88
107
  mock_url_generator_builder = MockUrlGeneratorBuilder.new
89
- attachment = Paperclip::Attachment.new(:name, :instance, url_generator: mock_url_generator_builder)
108
+ attachment = Paperclip::Attachment.new(
109
+ :name,
110
+ FakeModel.new,
111
+ url_generator: mock_url_generator_builder
112
+ )
90
113
 
91
114
  attachment.url(:style_name, options: :values)
92
115
  expect(mock_url_generator_builder.has_generated_url_with_options?(options: :values)).to eq true
@@ -95,7 +118,7 @@ describe Paperclip::Attachment do
95
118
  it "passes default options through when #url is given one argument" do
96
119
  mock_url_generator_builder = MockUrlGeneratorBuilder.new
97
120
  attachment = Paperclip::Attachment.new(:name,
98
- :instance,
121
+ FakeModel.new,
99
122
  url_generator: mock_url_generator_builder,
100
123
  use_timestamp: true)
101
124
 
@@ -106,7 +129,7 @@ describe Paperclip::Attachment do
106
129
  it "passes default style and options through when #url is given no arguments" do
107
130
  mock_url_generator_builder = MockUrlGeneratorBuilder.new
108
131
  attachment = Paperclip::Attachment.new(:name,
109
- :instance,
132
+ FakeModel.new,
110
133
  default_style: 'default style',
111
134
  url_generator: mock_url_generator_builder,
112
135
  use_timestamp: true)
@@ -119,7 +142,7 @@ describe Paperclip::Attachment do
119
142
  it "passes the option timestamp: true if :use_timestamp is true and :timestamp is not passed" do
120
143
  mock_url_generator_builder = MockUrlGeneratorBuilder.new
121
144
  attachment = Paperclip::Attachment.new(:name,
122
- :instance,
145
+ FakeModel.new,
123
146
  url_generator: mock_url_generator_builder,
124
147
  use_timestamp: true)
125
148
 
@@ -130,7 +153,7 @@ describe Paperclip::Attachment do
130
153
  it "passes the option timestamp: false if :use_timestamp is false and :timestamp is not passed" do
131
154
  mock_url_generator_builder = MockUrlGeneratorBuilder.new
132
155
  attachment = Paperclip::Attachment.new(:name,
133
- :instance,
156
+ FakeModel.new,
134
157
  url_generator: mock_url_generator_builder,
135
158
  use_timestamp: false)
136
159
 
@@ -141,7 +164,7 @@ describe Paperclip::Attachment do
141
164
  it "does not change the :timestamp if :timestamp is passed" do
142
165
  mock_url_generator_builder = MockUrlGeneratorBuilder.new
143
166
  attachment = Paperclip::Attachment.new(:name,
144
- :instance,
167
+ FakeModel.new,
145
168
  url_generator: mock_url_generator_builder,
146
169
  use_timestamp: false)
147
170
 
@@ -152,7 +175,7 @@ describe Paperclip::Attachment do
152
175
  it "renders JSON as default style" do
153
176
  mock_url_generator_builder = MockUrlGeneratorBuilder.new
154
177
  attachment = Paperclip::Attachment.new(:name,
155
- :instance,
178
+ FakeModel.new,
156
179
  default_style: 'default style',
157
180
  url_generator: mock_url_generator_builder)
158
181
 
@@ -163,7 +186,7 @@ describe Paperclip::Attachment do
163
186
  it "passes the option escape: true if :escape_url is true and :escape is not passed" do
164
187
  mock_url_generator_builder = MockUrlGeneratorBuilder.new
165
188
  attachment = Paperclip::Attachment.new(:name,
166
- :instance,
189
+ FakeModel.new,
167
190
  url_generator: mock_url_generator_builder,
168
191
  escape_url: true)
169
192
 
@@ -174,7 +197,7 @@ describe Paperclip::Attachment do
174
197
  it "passes the option escape: false if :escape_url is false and :escape is not passed" do
175
198
  mock_url_generator_builder = MockUrlGeneratorBuilder.new
176
199
  attachment = Paperclip::Attachment.new(:name,
177
- :instance,
200
+ FakeModel.new,
178
201
  url_generator: mock_url_generator_builder,
179
202
  escape_url: false)
180
203
 
@@ -212,10 +235,8 @@ describe Paperclip::Attachment do
212
235
  dummy = Dummy.new
213
236
  dummy.id = 1234
214
237
  dummy.avatar_file_name = "fake.jpg"
238
+ dummy.stubs(:new_record?).returns(false)
215
239
  expected_string = '{"avatar":"/system/dummies/avatars/000/001/234/original/fake.jpg"}'
216
- if ActiveRecord::Base.include_root_in_json # This is true by default in Rails 3, and false in 4
217
- expected_string = %({"dummy":#{expected_string}})
218
- end
219
240
  # active_model pre-3.2 checks only by calling any? on it, thus it doesn't work if it is empty
220
241
  assert_equal expected_string, dummy.to_json(only: [:dummy_key_for_old_active_model], methods: [:avatar])
221
242
  end
@@ -245,12 +266,17 @@ describe Paperclip::Attachment do
245
266
 
246
267
  context "without an Attachment" do
247
268
  before do
269
+ rebuild_model default_url: "default.url"
248
270
  @dummy = Dummy.new
249
271
  end
250
272
 
251
273
  it "returns false when asked exists?" do
252
274
  assert !@dummy.avatar.exists?
253
275
  end
276
+
277
+ it "#url returns the default_url" do
278
+ expect(@dummy.avatar.url).to eq "default.url"
279
+ end
254
280
  end
255
281
 
256
282
  context "on an Attachment" do
@@ -489,6 +515,7 @@ describe Paperclip::Attachment do
489
515
  @attachment.expects(:post_process).with(:thumb)
490
516
  @attachment.expects(:post_process).with(:large).never
491
517
  @attachment.assign(@file)
518
+ @attachment.save
492
519
  end
493
520
  end
494
521
 
@@ -635,15 +662,40 @@ describe Paperclip::Attachment do
635
662
  before do
636
663
  rebuild_model processor: [:thumbnail], styles: { small: '' }, whiny_thumbnails: true
637
664
  @dummy = Dummy.new
638
- Paperclip::Thumbnail.expects(:make).raises(Paperclip::Error, "cannot be processed.")
639
665
  @file = StringIO.new("...")
640
666
  @file.stubs(:to_tempfile).returns(@file)
641
- @dummy.avatar = @file
642
667
  end
643
668
 
644
- it "correctly forwards processing error message to the instance" do
645
- @dummy.valid?
646
- assert_contains @dummy.errors.full_messages, "Avatar cannot be processed."
669
+ context "when error is meaningful for the end user" do
670
+ before do
671
+ Paperclip::Thumbnail.expects(:make).raises(
672
+ Paperclip::Errors::NotIdentifiedByImageMagickError,
673
+ "cannot be processed."
674
+ )
675
+ end
676
+
677
+ it "correctly forwards processing error message to the instance" do
678
+ @dummy.avatar = @file
679
+ @dummy.valid?
680
+ assert_contains(
681
+ @dummy.errors.full_messages,
682
+ "Avatar cannot be processed."
683
+ )
684
+ end
685
+ end
686
+
687
+ context "when error is intended for the developer" do
688
+ before do
689
+ Paperclip::Thumbnail.expects(:make).raises(
690
+ Paperclip::Errors::CommandNotFoundError
691
+ )
692
+ end
693
+
694
+ it "propagates the error" do
695
+ assert_raises(Paperclip::Errors::CommandNotFoundError) do
696
+ @dummy.avatar = @file
697
+ end
698
+ end
647
699
  end
648
700
  end
649
701
 
@@ -661,9 +713,6 @@ describe Paperclip::Attachment do
661
713
 
662
714
  context "when assigned" do
663
715
  it "calls #make on all specified processors" do
664
- Paperclip::Thumbnail.stubs(:make).with(any_parameters).returns(@file)
665
- Paperclip::Test.stubs(:make).with(any_parameters).returns(@file)
666
-
667
716
  @dummy.avatar = @file
668
717
 
669
718
  expect(Paperclip::Thumbnail).to have_received(:make)
@@ -678,7 +727,6 @@ describe Paperclip::Attachment do
678
727
  convert_options: "",
679
728
  source_file_options: ""
680
729
  })
681
- Paperclip::Thumbnail.stubs(:make).returns(@file)
682
730
 
683
731
  @dummy.avatar = @file
684
732
 
@@ -686,12 +734,36 @@ describe Paperclip::Attachment do
686
734
  end
687
735
 
688
736
  it "calls #make with attachment passed as third argument" do
689
- Paperclip::Test.expects(:make).returns(@file)
690
-
691
737
  @dummy.avatar = @file
692
738
 
693
739
  expect(Paperclip::Test).to have_received(:make).with(anything, anything, @dummy.avatar)
694
740
  end
741
+
742
+ it "calls #make and unlinks intermediary files afterward" do
743
+ @dummy.avatar.expects(:unlink_files).with([@file, @file])
744
+
745
+ @dummy.avatar = @file
746
+ end
747
+ end
748
+ end
749
+
750
+ context "An attachment with a processor that returns original file" do
751
+ before do
752
+ class Paperclip::Test < Paperclip::Processor
753
+ def make; @file; end
754
+ end
755
+ rebuild_model processors: [:test], styles: { once: "100x100" }
756
+ @file = StringIO.new("...")
757
+ @file.stubs(:close)
758
+ @dummy = Dummy.new
759
+ end
760
+
761
+ context "when assigned" do
762
+ it "#calls #make and doesn't unlink the original file" do
763
+ @dummy.avatar.expects(:unlink_files).with([])
764
+
765
+ @dummy.avatar = @file
766
+ end
695
767
  end
696
768
  end
697
769
 
@@ -1065,7 +1137,7 @@ describe Paperclip::Attachment do
1065
1137
  context "with a file assigned but not saved yet" do
1066
1138
  it "clears out any attached files" do
1067
1139
  @attachment.assign(@file)
1068
- assert !@attachment.queued_for_write.blank?
1140
+ assert @attachment.queued_for_write.present?
1069
1141
  @attachment.clear
1070
1142
  assert @attachment.queued_for_write.blank?
1071
1143
  end
@@ -1315,6 +1387,12 @@ describe Paperclip::Attachment do
1315
1387
  end
1316
1388
 
1317
1389
  it "does not calculate fingerprint" do
1390
+ Digest::MD5.stubs(:file)
1391
+ @dummy.avatar = @file
1392
+ expect(Digest::MD5).to have_received(:file).never
1393
+ end
1394
+
1395
+ it "does not assign fingerprint" do
1318
1396
  @dummy.avatar = @file
1319
1397
  assert_nil @dummy.avatar.fingerprint
1320
1398
  end
@@ -1338,7 +1416,7 @@ describe Paperclip::Attachment do
1338
1416
 
1339
1417
  context "and avatar_file_size column" do
1340
1418
  before do
1341
- ActiveRecord::Base.connection.add_column :dummies, :avatar_file_size, :integer
1419
+ ActiveRecord::Base.connection.add_column :dummies, :avatar_file_size, :bigint
1342
1420
  rebuild_class
1343
1421
  @dummy = Dummy.new
1344
1422
  end
@@ -1371,16 +1449,46 @@ describe Paperclip::Attachment do
1371
1449
  assert_nothing_raised { @dummy.avatar = @file }
1372
1450
  end
1373
1451
 
1374
- it "returns the right value when sent #avatar_fingerprint" do
1375
- @dummy.avatar = @file
1376
- assert_equal 'aec488126c3b33c08a10c3fa303acf27', @dummy.avatar_fingerprint
1452
+ context "with explicitly set digest" do
1453
+ before do
1454
+ rebuild_class adapter_options: { hash_digest: Digest::SHA256 }
1455
+ @dummy = Dummy.new
1456
+ end
1457
+
1458
+ it "returns the right value when sent #avatar_fingerprint" do
1459
+ @dummy.avatar = @file
1460
+ assert_equal "734016d801a497f5579cdd4ef2ae1d020088c1db754dc434482d76dd5486520a",
1461
+ @dummy.avatar_fingerprint
1462
+ end
1463
+
1464
+ it "returns the right value when saved, reloaded, and sent #avatar_fingerprint" do
1465
+ @dummy.avatar = @file
1466
+ @dummy.save
1467
+ @dummy = Dummy.find(@dummy.id)
1468
+ assert_equal "734016d801a497f5579cdd4ef2ae1d020088c1db754dc434482d76dd5486520a",
1469
+ @dummy.avatar_fingerprint
1470
+ end
1377
1471
  end
1378
1472
 
1379
- it "returns the right value when saved, reloaded, and sent #avatar_fingerprint" do
1380
- @dummy.avatar = @file
1381
- @dummy.save
1382
- @dummy = Dummy.find(@dummy.id)
1383
- assert_equal 'aec488126c3b33c08a10c3fa303acf27', @dummy.avatar_fingerprint
1473
+ context "with the default digest" do
1474
+ before do
1475
+ rebuild_class # MD5 is the default
1476
+ @dummy = Dummy.new
1477
+ end
1478
+
1479
+ it "returns the right value when sent #avatar_fingerprint" do
1480
+ @dummy.avatar = @file
1481
+ assert_equal "aec488126c3b33c08a10c3fa303acf27",
1482
+ @dummy.avatar_fingerprint
1483
+ end
1484
+
1485
+ it "returns the right value when saved, reloaded, and sent #avatar_fingerprint" do
1486
+ @dummy.avatar = @file
1487
+ @dummy.save
1488
+ @dummy = Dummy.find(@dummy.id)
1489
+ assert_equal "aec488126c3b33c08a10c3fa303acf27",
1490
+ @dummy.avatar_fingerprint
1491
+ end
1384
1492
  end
1385
1493
  end
1386
1494
  end
@@ -1452,6 +1560,4 @@ describe Paperclip::Attachment do
1452
1560
  assert_file_exists(@path)
1453
1561
  end
1454
1562
  end
1455
-
1456
1563
  end
1457
-
@@ -1,6 +1,12 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Paperclip::ContentTypeDetector do
4
+ it 'returns a meaningful content type for open xml spreadsheets' do
5
+ file = File.new(fixture_file("empty.xlsx"))
6
+ assert_equal "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
7
+ Paperclip::ContentTypeDetector.new(file.path).detect
8
+ end
9
+
4
10
  it 'gives a sensible default when the name is empty' do
5
11
  assert_equal "application/octet-stream", Paperclip::ContentTypeDetector.new("").detect
6
12
  end
@@ -13,7 +19,8 @@ describe Paperclip::ContentTypeDetector do
13
19
 
14
20
  it 'returns content type of file if it is an acceptable type' do
15
21
  MIME::Types.stubs(:type_for).returns([MIME::Type.new('application/mp4'), MIME::Type.new('video/mp4'), MIME::Type.new('audio/mp4')])
16
- Paperclip.stubs(:run).returns("video/mp4")
22
+ Paperclip::ContentTypeDetector.any_instance
23
+ .stubs(:type_from_file_contents).returns("video/mp4")
17
24
  @filename = "my_file.mp4"
18
25
  assert_equal "video/mp4", Paperclip::ContentTypeDetector.new(@filename).detect
19
26
  end
@@ -34,7 +41,7 @@ describe Paperclip::ContentTypeDetector do
34
41
  end
35
42
 
36
43
  it 'returns a sensible default when the file command is missing' do
37
- Paperclip.stubs(:run).raises(Cocaine::CommandLineError.new)
44
+ Paperclip.stubs(:run).raises(Terrapin::CommandLineError.new)
38
45
  @filename = "/path/to/something"
39
46
  assert_equal "application/octet-stream", Paperclip::ContentTypeDetector.new(@filename).detect
40
47
  end
@@ -12,7 +12,7 @@ describe Paperclip::FileCommandContentTypeDetector do
12
12
  end
13
13
 
14
14
  it 'returns a sensible default when the file command is missing' do
15
- Paperclip.stubs(:run).raises(Cocaine::CommandLineError.new)
15
+ Paperclip.stubs(:run).raises(Terrapin::CommandLineError.new)
16
16
  @filename = "/path/to/something"
17
17
  assert_equal "application/octet-stream",
18
18
  Paperclip::FileCommandContentTypeDetector.new(@filename).detect
@@ -23,5 +23,18 @@ describe Paperclip::FileCommandContentTypeDetector do
23
23
  assert_equal "application/octet-stream",
24
24
  Paperclip::FileCommandContentTypeDetector.new("windows").detect
25
25
  end
26
- end
27
26
 
27
+ context "#type_from_file_command" do
28
+ let(:detector) { Paperclip::FileCommandContentTypeDetector.new("html") }
29
+
30
+ it "does work with the output of old versions of file" do
31
+ Paperclip.stubs(:run).returns("text/html charset=us-ascii")
32
+ expect(detector.detect).to eq("text/html")
33
+ end
34
+
35
+ it "does work with the output of new versions of file" do
36
+ Paperclip.stubs(:run).returns("text/html; charset=us-ascii")
37
+ expect(detector.detect).to eq("text/html")
38
+ end
39
+ end
40
+ end
@@ -1,4 +1,3 @@
1
- # encoding: utf-8
2
1
  require 'spec_helper'
3
2
 
4
3
  describe Paperclip::FilenameCleaner do
@@ -82,7 +82,7 @@ describe Paperclip::Geometry do
82
82
  assert_equal 456, @upper.height
83
83
  end
84
84
 
85
- ['>', '<', '#', '@', '%', '^', '!', nil].each do |mod|
85
+ ['>', '<', '#', '@', '@>', '>@', '%', '^', '!', nil].each do |mod|
86
86
  it "ensures the modifier #{description} is preserved" do
87
87
  assert @geo = Paperclip::Geometry.parse("123x456#{mod}")
88
88
  assert_equal mod, @geo.modifier
@@ -0,0 +1,44 @@
1
+ # require "spec_helper"
2
+
3
+ describe Paperclip::Glue do
4
+ describe "when ActiveRecord does not exist" do
5
+ before do
6
+ ActiveRecordSaved = ActiveRecord
7
+ Object.send :remove_const, "ActiveRecord"
8
+ end
9
+
10
+ after do
11
+ ActiveRecord = ActiveRecordSaved
12
+ Object.send :remove_const, "ActiveRecordSaved"
13
+ end
14
+
15
+ it "does not fail" do
16
+ NonActiveRecordModel = Class.new
17
+ NonActiveRecordModel.send :include, Paperclip::Glue
18
+ Object.send :remove_const, "NonActiveRecordModel"
19
+ end
20
+ end
21
+
22
+ describe "when ActiveRecord does exist" do
23
+ before do
24
+ if Object.const_defined?("ActiveRecord")
25
+ @defined_active_record = false
26
+ else
27
+ ActiveRecord = :defined
28
+ @defined_active_record = true
29
+ end
30
+ end
31
+
32
+ after do
33
+ if @defined_active_record
34
+ Object.send :remove_const, "ActiveRecord"
35
+ end
36
+ end
37
+
38
+ it "does not fail" do
39
+ NonActiveRecordModel = Class.new
40
+ NonActiveRecordModel.send :include, Paperclip::Glue
41
+ Object.send :remove_const, "NonActiveRecordModel"
42
+ end
43
+ end
44
+ end
@@ -38,6 +38,15 @@ describe Paperclip::HasAttachedFile do
38
38
  assert_adding_attachment('avatar').defines_callback('after_commit')
39
39
  end
40
40
 
41
+ context 'when the class does not allow after_commit callbacks' do
42
+ it 'defines an after_destroy callback' do
43
+ assert_adding_attachment(
44
+ 'avatar',
45
+ unstub_methods: [:after_commit]
46
+ ).defines_callback('after_destroy')
47
+ end
48
+ end
49
+
41
50
  it 'defines the Paperclip-specific callbacks' do
42
51
  assert_adding_attachment('avatar').defines_callback('define_paperclip_callbacks')
43
52
  end
@@ -53,20 +62,26 @@ describe Paperclip::HasAttachedFile do
53
62
 
54
63
  private
55
64
 
56
- def assert_adding_attachment(attachment_name)
57
- AttachmentAdder.new(attachment_name)
65
+ def assert_adding_attachment(attachment_name, options={})
66
+ AttachmentAdder.new(attachment_name, options)
58
67
  end
59
68
 
60
69
  class AttachmentAdder
61
70
  include Mocha::API
62
71
  include RSpec::Matchers
63
72
 
64
- def initialize(attachment_name)
73
+ def initialize(attachment_name, options = {})
65
74
  @attachment_name = attachment_name
75
+ @stubbed_class = stub_class
76
+ if options.present?
77
+ options[:unstub_methods].each do |method|
78
+ @stubbed_class.unstub(method)
79
+ end
80
+ end
66
81
  end
67
82
 
68
83
  def defines_method(method_name)
69
- a_class = stub_class
84
+ a_class = @stubbed_class
70
85
 
71
86
  Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {})
72
87
 
@@ -74,7 +89,7 @@ describe Paperclip::HasAttachedFile do
74
89
  end
75
90
 
76
91
  def defines_class_method(method_name)
77
- a_class = stub_class
92
+ a_class = @stubbed_class
78
93
  a_class.class.stubs(:define_method)
79
94
 
80
95
  Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {})
@@ -83,7 +98,7 @@ describe Paperclip::HasAttachedFile do
83
98
  end
84
99
 
85
100
  def defines_validation
86
- a_class = stub_class
101
+ a_class = @stubbed_class
87
102
 
88
103
  Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {})
89
104
 
@@ -91,7 +106,7 @@ describe Paperclip::HasAttachedFile do
91
106
  end
92
107
 
93
108
  def registers_attachment
94
- a_class = stub_class
109
+ a_class = @stubbed_class
95
110
  Paperclip::AttachmentRegistry.stubs(:register)
96
111
 
97
112
  Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {size: 1})
@@ -100,7 +115,7 @@ describe Paperclip::HasAttachedFile do
100
115
  end
101
116
 
102
117
  def defines_callback(callback_name)
103
- a_class = stub_class
118
+ a_class = @stubbed_class
104
119
 
105
120
  Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {})
106
121
 
@@ -132,6 +147,7 @@ describe Paperclip::HasAttachedFile do
132
147
  after_save: nil,
133
148
  before_destroy: nil,
134
149
  after_commit: nil,
150
+ after_destroy: nil,
135
151
  define_paperclip_callbacks: nil,
136
152
  extend: nil,
137
153
  name: 'Billy',
@@ -1,15 +1,33 @@
1
- # encoding: utf-8
2
1
  require 'spec_helper'
3
2
  require 'open-uri'
4
3
 
5
4
  describe 'Paperclip' do
5
+ around do |example|
6
+ files_before = ObjectSpace.each_object(Tempfile).select do |file|
7
+ file.path && File.file?(file.path)
8
+ end
9
+
10
+ example.run
11
+
12
+ files_after = ObjectSpace.each_object(Tempfile).select do |file|
13
+ file.path && File.file?(file.path)
14
+ end
15
+
16
+ diff = files_after - files_before
17
+ expect(diff).to eq([]), "Leaked tempfiles: #{diff.inspect}"
18
+ end
19
+
6
20
  context "Many models at once" do
7
21
  before do
8
22
  rebuild_model
9
23
  @file = File.new(fixture_file("5k.png"), 'rb')
10
- 300.times do |i|
11
- Dummy.create! avatar: @file
12
- end
24
+ # Deals with `Too many open files` error
25
+ dummies = Array.new(300) { Dummy.new avatar: @file }
26
+ Dummy.import dummies
27
+ # save attachment instances to run after hooks including tempfile cleanup
28
+ # since activerecord-import does not use our usually hooked-in hooks
29
+ # (such as after_save)
30
+ dummies.each { |dummy| dummy.avatar.save }
13
31
  end
14
32
 
15
33
  after { @file.close }
@@ -134,6 +152,14 @@ describe 'Paperclip' do
134
152
  end
135
153
 
136
154
  it "allows us to selectively create each thumbnail" do
155
+ skip <<-EXPLANATION
156
+ #reprocess! calls #assign which calls Paperclip.io_adapters.for
157
+ which creates the tempfile. #assign then calls #post_process_file which
158
+ calls MediaTypeSpoofDetectionValidator#validate_each which calls
159
+ Paperclip.io_adapters.for, which creates another tempfile. That first
160
+ tempfile is the one that leaks.
161
+ EXPLANATION
162
+
137
163
  assert_file_not_exists(@thumb_small_path)
138
164
  assert_file_not_exists(@thumb_large_path)
139
165
 
@@ -158,7 +184,11 @@ describe 'Paperclip' do
158
184
  assert_not_equal File.size(@file.path), @dummy.avatar.size
159
185
  end
160
186
 
161
- after { @file.close }
187
+ after do
188
+ @file.close
189
+ # save attachment instance to run after hooks (including tempfile cleanup)
190
+ @dummy.avatar.save
191
+ end
162
192
  end
163
193
 
164
194
  context "A model with attachments scoped under an id" do
@@ -346,6 +376,8 @@ describe 'Paperclip' do
346
376
  it "is not ok with bad files" do
347
377
  @dummy.avatar = @bad_file
348
378
  assert ! @dummy.valid?
379
+ # save attachment instance to run after hooks (including tempfile cleanup)
380
+ @dummy.avatar.save
349
381
  end
350
382
 
351
383
  it "knows the difference between good files, bad files, and not files when validating" do
@@ -353,8 +385,13 @@ describe 'Paperclip' do
353
385
  @d2 = Dummy.find(@dummy.id)
354
386
  @d2.avatar = @file
355
387
  assert @d2.valid?, @d2.errors.full_messages.inspect
388
+ # save attachment instance to run after hooks (including tempfile cleanup)
389
+ @d2.avatar.save
390
+
356
391
  @d2.avatar = @bad_file
357
392
  assert ! @d2.valid?
393
+ # save attachment instance to run after hooks (including tempfile cleanup)
394
+ @d2.avatar.save
358
395
  end
359
396
 
360
397
  it "is able to reload without saving and not have the file disappear" do