kt-paperclip 5.4.0 → 6.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/issue_template.md +3 -0
- data/.hound.yml +27 -32
- data/.travis.yml +23 -2
- data/Appraisals +17 -0
- data/Gemfile +9 -7
- data/NEWS +21 -0
- data/README.md +27 -37
- data/Rakefile +29 -21
- data/UPGRADING +3 -3
- data/features/basic_integration.feature +4 -0
- data/features/migration.feature +10 -51
- data/features/step_definitions/attachment_steps.rb +12 -12
- data/features/step_definitions/html_steps.rb +5 -5
- data/features/step_definitions/rails_steps.rb +29 -9
- data/features/step_definitions/s3_steps.rb +3 -3
- data/features/step_definitions/web_steps.rb +5 -6
- data/features/support/env.rb +4 -4
- data/features/support/fakeweb.rb +3 -5
- data/features/support/file_helpers.rb +2 -2
- data/features/support/paths.rb +4 -4
- data/features/support/rails.rb +7 -7
- data/features/support/selectors.rb +1 -1
- data/gemfiles/4.2.gemfile +7 -4
- data/gemfiles/5.0.gemfile +7 -4
- data/gemfiles/5.1.gemfile +20 -0
- data/gemfiles/5.2.gemfile +20 -0
- data/gemfiles/6.0.gemfile +20 -0
- data/lib/generators/paperclip/paperclip_generator.rb +6 -8
- data/lib/paperclip/attachment.rb +102 -104
- data/lib/paperclip/attachment_registry.rb +2 -2
- data/lib/paperclip/file_command_content_type_detector.rb +1 -3
- data/lib/paperclip/filename_cleaner.rb +0 -1
- data/lib/paperclip/geometry.rb +18 -19
- data/lib/paperclip/geometry_detector_factory.rb +13 -16
- data/lib/paperclip/geometry_parser_factory.rb +5 -5
- data/lib/paperclip/glue.rb +3 -3
- data/lib/paperclip/has_attached_file.rb +5 -4
- data/lib/paperclip/helpers.rb +3 -3
- data/lib/paperclip/interpolations.rb +42 -38
- data/lib/paperclip/io_adapters/abstract_adapter.rb +16 -14
- data/lib/paperclip/io_adapters/attachment_adapter.rb +12 -6
- data/lib/paperclip/io_adapters/data_uri_adapter.rb +1 -1
- data/lib/paperclip/io_adapters/file_adapter.rb +1 -3
- data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +3 -3
- data/lib/paperclip/io_adapters/identity_adapter.rb +1 -2
- data/lib/paperclip/io_adapters/registry.rb +1 -1
- data/lib/paperclip/io_adapters/stringio_adapter.rb +1 -1
- data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +6 -8
- data/lib/paperclip/io_adapters/uri_adapter.rb +9 -7
- data/lib/paperclip/logger.rb +1 -1
- data/lib/paperclip/matchers/have_attached_file_matcher.rb +4 -4
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +19 -18
- data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +4 -4
- data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +11 -10
- data/lib/paperclip/matchers.rb +4 -4
- data/lib/paperclip/media_type_spoof_detector.rb +13 -13
- data/lib/paperclip/missing_attachment_styles.rb +11 -6
- data/lib/paperclip/processor.rb +13 -6
- data/lib/paperclip/processor_helpers.rb +3 -1
- data/lib/paperclip/rails_environment.rb +1 -5
- data/lib/paperclip/railtie.rb +5 -5
- data/lib/paperclip/schema.rb +18 -14
- data/lib/paperclip/storage/filesystem.rb +5 -7
- data/lib/paperclip/storage/fog.rb +36 -32
- data/lib/paperclip/storage/s3.rb +67 -75
- data/lib/paperclip/style.rb +3 -6
- data/lib/paperclip/tempfile.rb +4 -5
- data/lib/paperclip/tempfile_factory.rb +0 -1
- data/lib/paperclip/thumbnail.rb +11 -11
- data/lib/paperclip/url_generator.rb +5 -12
- data/lib/paperclip/validators/attachment_content_type_validator.rb +3 -3
- data/lib/paperclip/validators/attachment_file_name_validator.rb +5 -10
- data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +1 -2
- data/lib/paperclip/validators/attachment_presence_validator.rb +3 -5
- data/lib/paperclip/validators/attachment_size_validator.rb +8 -8
- data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +3 -1
- data/lib/paperclip/validators.rb +12 -13
- data/lib/paperclip/version.rb +1 -3
- data/lib/paperclip.rb +49 -48
- data/lib/tasks/paperclip.rake +23 -24
- data/paperclip.gemspec +29 -33
- data/shoulda_macros/paperclip.rb +16 -16
- data/spec/paperclip/attachment_definitions_spec.rb +5 -5
- data/spec/paperclip/attachment_processing_spec.rb +22 -23
- data/spec/paperclip/attachment_registry_spec.rb +15 -15
- data/spec/paperclip/attachment_spec.rb +238 -196
- data/spec/paperclip/content_type_detector_spec.rb +11 -12
- data/spec/paperclip/file_command_content_type_detector_spec.rb +10 -10
- data/spec/paperclip/filename_cleaner_spec.rb +3 -4
- data/spec/paperclip/geometry_detector_spec.rb +7 -8
- data/spec/paperclip/geometry_parser_spec.rb +31 -31
- data/spec/paperclip/geometry_spec.rb +24 -24
- data/spec/paperclip/glue_spec.rb +3 -5
- data/spec/paperclip/has_attached_file_spec.rb +46 -126
- data/spec/paperclip/integration_spec.rb +111 -77
- data/spec/paperclip/interpolations_spec.rb +101 -93
- data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +41 -13
- data/spec/paperclip/io_adapters/attachment_adapter_spec.rb +8 -10
- data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +13 -14
- data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +4 -4
- data/spec/paperclip/io_adapters/file_adapter_spec.rb +12 -12
- data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +42 -26
- data/spec/paperclip/io_adapters/identity_adapter_spec.rb +1 -1
- data/spec/paperclip/io_adapters/nil_adapter_spec.rb +2 -2
- data/spec/paperclip/io_adapters/registry_spec.rb +4 -4
- data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +10 -10
- data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +6 -6
- data/spec/paperclip/io_adapters/uri_adapter_spec.rb +80 -31
- data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +3 -3
- data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +4 -5
- data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +4 -4
- data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +4 -4
- data/spec/paperclip/media_type_spoof_detector_spec.rb +50 -24
- data/spec/paperclip/meta_class_spec.rb +3 -3
- data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +28 -24
- data/spec/paperclip/paperclip_spec.rb +15 -11
- data/spec/paperclip/plural_cache_spec.rb +8 -8
- data/spec/paperclip/processor_helpers_spec.rb +35 -35
- data/spec/paperclip/processor_spec.rb +8 -8
- data/spec/paperclip/rails_environment_spec.rb +7 -10
- data/spec/paperclip/rake_spec.rb +39 -39
- data/spec/paperclip/schema_spec.rb +57 -53
- data/spec/paperclip/storage/filesystem_spec.rb +6 -6
- data/spec/paperclip/storage/fog_spec.rb +76 -82
- data/spec/paperclip/storage/s3_live_spec.rb +22 -22
- data/spec/paperclip/storage/s3_spec.rb +585 -583
- data/spec/paperclip/style_spec.rb +67 -71
- data/spec/paperclip/tempfile_factory_spec.rb +5 -5
- data/spec/paperclip/thumbnail_spec.rb +68 -67
- data/spec/paperclip/url_generator_spec.rb +18 -29
- data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +27 -27
- data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +15 -16
- data/spec/paperclip/validators/attachment_presence_validator_spec.rb +5 -5
- data/spec/paperclip/validators/attachment_size_validator_spec.rb +21 -21
- data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +9 -13
- data/spec/paperclip/validators_spec.rb +40 -40
- data/spec/spec_helper.rb +21 -23
- data/spec/support/assertions.rb +8 -6
- data/spec/support/fake_model.rb +1 -2
- data/spec/support/fake_rails.rb +1 -1
- data/spec/support/matchers/exist.rb +1 -1
- data/spec/support/matchers/have_column.rb +1 -1
- data/spec/support/mock_url_generator_builder.rb +2 -3
- data/spec/support/model_reconstruction.rb +16 -12
- data/spec/support/reporting.rb +1 -1
- data/spec/support/test_data.rb +2 -2
- metadata +49 -105
- data/lib/kt-paperclip.rb +0 -1
- data/spec/support/conditional_filter_helper.rb +0 -5
@@ -1,8 +1,6 @@
|
|
1
|
-
|
2
|
-
require 'spec_helper'
|
1
|
+
require "spec_helper"
|
3
2
|
|
4
3
|
describe Paperclip::Attachment do
|
5
|
-
|
6
4
|
it "is not present when file not set" do
|
7
5
|
rebuild_class
|
8
6
|
dummy = Dummy.new
|
@@ -19,8 +17,8 @@ describe Paperclip::Attachment do
|
|
19
17
|
end
|
20
18
|
|
21
19
|
it "processes :original style first" do
|
22
|
-
file = File.new(fixture_file("50x50.png"),
|
23
|
-
rebuild_class styles: { small:
|
20
|
+
file = File.new(fixture_file("50x50.png"), "rb")
|
21
|
+
rebuild_class styles: { small: "100x>", original: "42x42#" }
|
24
22
|
dummy = Dummy.new
|
25
23
|
dummy.avatar = file
|
26
24
|
dummy.save
|
@@ -32,7 +30,7 @@ describe Paperclip::Attachment do
|
|
32
30
|
end
|
33
31
|
|
34
32
|
it "does not delete styles that don't get reprocessed" do
|
35
|
-
file = File.new(fixture_file("50x50.png"),
|
33
|
+
file = File.new(fixture_file("50x50.png"), "rb")
|
36
34
|
rebuild_class styles: {
|
37
35
|
small: "100x>",
|
38
36
|
large: "500x>",
|
@@ -54,6 +52,22 @@ describe Paperclip::Attachment do
|
|
54
52
|
expect(dummy.avatar.path(:original)).to exist
|
55
53
|
end
|
56
54
|
|
55
|
+
it "reprocess works with virtual content_type attribute" do
|
56
|
+
rebuild_class styles: { small: "100x>" }
|
57
|
+
modify_table { |t| t.remove :avatar_content_type }
|
58
|
+
Dummy.send :attr_accessor, :avatar_content_type
|
59
|
+
Dummy.validates_attachment_content_type(
|
60
|
+
:avatar,
|
61
|
+
content_type: %w(image/jpeg image/png)
|
62
|
+
)
|
63
|
+
Dummy.create!(avatar: File.new(fixture_file("50x50.png"), "rb"))
|
64
|
+
|
65
|
+
dummy = Dummy.first
|
66
|
+
dummy.avatar.reprocess!(:small)
|
67
|
+
|
68
|
+
expect(dummy.avatar.path(:small)).to exist
|
69
|
+
end
|
70
|
+
|
57
71
|
context "having a not empty hash as a default option" do
|
58
72
|
before do
|
59
73
|
@old_default_options = Paperclip::Attachment.default_options.dup
|
@@ -79,7 +93,7 @@ describe Paperclip::Attachment do
|
|
79
93
|
:name,
|
80
94
|
FakeModel.new,
|
81
95
|
url_generator: mock_url_generator_builder
|
82
|
-
|
96
|
+
)
|
83
97
|
|
84
98
|
attachment.url(:style_name, true)
|
85
99
|
expect(mock_url_generator_builder.has_generated_url_with_options?(timestamp: true, escape: true)).to eq true
|
@@ -94,7 +108,7 @@ describe Paperclip::Attachment do
|
|
94
108
|
:name,
|
95
109
|
FakeModel.new,
|
96
110
|
url_generator: mock_url_generator_builder
|
97
|
-
|
111
|
+
)
|
98
112
|
|
99
113
|
attachment.url(:style_name, options: :values)
|
100
114
|
expect(mock_url_generator_builder.has_generated_url_with_options?(options: :values)).to eq true
|
@@ -115,13 +129,13 @@ describe Paperclip::Attachment do
|
|
115
129
|
mock_url_generator_builder = MockUrlGeneratorBuilder.new
|
116
130
|
attachment = Paperclip::Attachment.new(:name,
|
117
131
|
FakeModel.new,
|
118
|
-
default_style:
|
132
|
+
default_style: "default style",
|
119
133
|
url_generator: mock_url_generator_builder,
|
120
134
|
use_timestamp: true)
|
121
135
|
|
122
136
|
attachment.url
|
123
137
|
assert mock_url_generator_builder.has_generated_url_with_options?(escape: true, timestamp: true)
|
124
|
-
assert mock_url_generator_builder.has_generated_url_with_style_name?(
|
138
|
+
assert mock_url_generator_builder.has_generated_url_with_style_name?("default style")
|
125
139
|
end
|
126
140
|
|
127
141
|
it "passes the option timestamp: true if :use_timestamp is true and :timestamp is not passed" do
|
@@ -161,11 +175,11 @@ describe Paperclip::Attachment do
|
|
161
175
|
mock_url_generator_builder = MockUrlGeneratorBuilder.new
|
162
176
|
attachment = Paperclip::Attachment.new(:name,
|
163
177
|
FakeModel.new,
|
164
|
-
default_style:
|
178
|
+
default_style: "default style",
|
165
179
|
url_generator: mock_url_generator_builder)
|
166
180
|
|
167
181
|
attachment.as_json
|
168
|
-
assert mock_url_generator_builder.has_generated_url_with_style_name?(
|
182
|
+
assert mock_url_generator_builder.has_generated_url_with_style_name?("default style")
|
169
183
|
end
|
170
184
|
|
171
185
|
it "passes the option escape: true if :escape_url is true and :escape is not passed" do
|
@@ -220,7 +234,7 @@ describe Paperclip::Attachment do
|
|
220
234
|
dummy = Dummy.new
|
221
235
|
dummy.id = 1234
|
222
236
|
dummy.avatar_file_name = "fake.jpg"
|
223
|
-
dummy.
|
237
|
+
allow(dummy).to receive(:new_record?).and_return(false)
|
224
238
|
expected_string = '{"avatar":"/system/dummies/avatars/000/001/234/original/fake.jpg"}'
|
225
239
|
# active_model pre-3.2 checks only by calling any? on it, thus it doesn't work if it is empty
|
226
240
|
assert_equal expected_string, dummy.to_json(only: [:dummy_key_for_old_active_model], methods: [:avatar])
|
@@ -230,11 +244,11 @@ describe Paperclip::Attachment do
|
|
230
244
|
before do
|
231
245
|
rebuild_model
|
232
246
|
@old_default_options = Paperclip::Attachment.default_options.dup
|
233
|
-
@new_default_options = @old_default_options.merge(
|
247
|
+
@new_default_options = @old_default_options.merge(
|
234
248
|
path: "argle/bargle",
|
235
249
|
url: "fooferon",
|
236
250
|
default_url: "not here.png"
|
237
|
-
|
251
|
+
)
|
238
252
|
end
|
239
253
|
|
240
254
|
after do
|
@@ -300,8 +314,9 @@ describe Paperclip::Attachment do
|
|
300
314
|
before do
|
301
315
|
rebuild_model path: ":id.omg/:id-bbq/:idwhat/:id_partition.wtf"
|
302
316
|
@dummy = Dummy.new
|
303
|
-
|
304
|
-
@
|
317
|
+
|
318
|
+
allow(@dummy).to receive(:id).and_return(1024)
|
319
|
+
@file = File.new(fixture_file("5k.png"), "rb")
|
305
320
|
@dummy.avatar = @file
|
306
321
|
end
|
307
322
|
|
@@ -315,10 +330,10 @@ describe Paperclip::Attachment do
|
|
315
330
|
context "An attachment with :timestamp interpolations" do
|
316
331
|
before do
|
317
332
|
@file = StringIO.new("...")
|
318
|
-
@zone =
|
319
|
-
Time.
|
320
|
-
@zone_default =
|
321
|
-
Time.
|
333
|
+
@zone = "UTC"
|
334
|
+
allow(Time).to receive(:zone).and_return(@zone)
|
335
|
+
@zone_default = "Eastern Time (US & Canada)"
|
336
|
+
allow(Time).to receive(:zone_default).and_return(@zone_default)
|
322
337
|
end
|
323
338
|
|
324
339
|
context "using default time zone" do
|
@@ -368,17 +383,17 @@ describe Paperclip::Attachment do
|
|
368
383
|
context "when secret is set" do
|
369
384
|
before do
|
370
385
|
rebuild_model path: ":hash",
|
371
|
-
|
372
|
-
|
386
|
+
hash_secret: "w00t",
|
387
|
+
hash_data: ":class/:attachment/:style/:filename"
|
373
388
|
@attachment = Dummy.new.avatar
|
374
389
|
@attachment.assign @file
|
375
390
|
end
|
376
391
|
|
377
392
|
it "results in the correct interpolation" do
|
378
393
|
assert_equal "dummies/avatars/original/5k.png",
|
379
|
-
|
394
|
+
@attachment.send(:interpolate, @attachment.options[:hash_data])
|
380
395
|
assert_equal "dummies/avatars/thumb/5k.png",
|
381
|
-
|
396
|
+
@attachment.send(:interpolate, @attachment.options[:hash_data], :thumb)
|
382
397
|
end
|
383
398
|
|
384
399
|
it "results in a correct hash" do
|
@@ -394,10 +409,10 @@ describe Paperclip::Attachment do
|
|
394
409
|
@id = 1024
|
395
410
|
rebuild_model path: ":rails_env/:id.png"
|
396
411
|
@dummy = Dummy.new
|
397
|
-
@dummy.
|
412
|
+
allow(@dummy).to receive(:id).and_return(@id)
|
398
413
|
@file = StringIO.new(".")
|
399
414
|
@dummy.avatar = @file
|
400
|
-
Rails.
|
415
|
+
allow(Rails).to receive(:env).and_return(@rails_env)
|
401
416
|
end
|
402
417
|
|
403
418
|
it "returns the proper path" do
|
@@ -408,11 +423,11 @@ describe Paperclip::Attachment do
|
|
408
423
|
context "An attachment with a default style and an extension interpolation" do
|
409
424
|
before do
|
410
425
|
rebuild_model path: ":basename.:extension",
|
411
|
-
|
412
|
-
|
426
|
+
styles: { default: ["100x100", :jpg] },
|
427
|
+
default_style: :default
|
413
428
|
@attachment = Dummy.new.avatar
|
414
429
|
@file = File.open(fixture_file("5k.png"))
|
415
|
-
@file.
|
430
|
+
allow(@file).to receive(:original_filename).and_return("file.png")
|
416
431
|
end
|
417
432
|
it "returns the right extension for the path" do
|
418
433
|
@attachment.assign(@file)
|
@@ -423,9 +438,9 @@ describe Paperclip::Attachment do
|
|
423
438
|
context "An attachment with :convert_options" do
|
424
439
|
before do
|
425
440
|
rebuild_model styles: {
|
426
|
-
|
427
|
-
|
428
|
-
|
441
|
+
thumb: "100x100",
|
442
|
+
large: "400x400"
|
443
|
+
},
|
429
444
|
convert_options: {
|
430
445
|
all: "-do_stuff",
|
431
446
|
thumb: "-thumbnailize"
|
@@ -446,9 +461,9 @@ describe Paperclip::Attachment do
|
|
446
461
|
context "An attachment with :source_file_options" do
|
447
462
|
before do
|
448
463
|
rebuild_model styles: {
|
449
|
-
|
450
|
-
|
451
|
-
|
464
|
+
thumb: "100x100",
|
465
|
+
large: "400x400"
|
466
|
+
},
|
452
467
|
source_file_options: {
|
453
468
|
all: "-density 400",
|
454
469
|
thumb: "-depth 8"
|
@@ -469,17 +484,17 @@ describe Paperclip::Attachment do
|
|
469
484
|
context "An attachment with :only_process" do
|
470
485
|
before do
|
471
486
|
rebuild_model styles: {
|
472
|
-
|
473
|
-
|
474
|
-
|
487
|
+
thumb: "100x100",
|
488
|
+
large: "400x400"
|
489
|
+
},
|
475
490
|
only_process: [:thumb]
|
476
491
|
@file = StringIO.new("...")
|
477
492
|
@attachment = Dummy.new.avatar
|
478
493
|
end
|
479
494
|
|
480
495
|
it "only processes the provided style" do
|
481
|
-
@attachment.
|
482
|
-
@attachment.
|
496
|
+
expect(@attachment).to receive(:post_process).with(:thumb)
|
497
|
+
expect(@attachment).to receive(:post_process).with(:large).never
|
483
498
|
@attachment.assign(@file)
|
484
499
|
end
|
485
500
|
end
|
@@ -487,18 +502,18 @@ describe Paperclip::Attachment do
|
|
487
502
|
context "An attachment with :only_process that is a proc" do
|
488
503
|
before do
|
489
504
|
rebuild_model styles: {
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
only_process: lambda { |
|
505
|
+
thumb: "100x100",
|
506
|
+
large: "400x400"
|
507
|
+
},
|
508
|
+
only_process: lambda { |_attachment| [:thumb] }
|
494
509
|
|
495
510
|
@file = StringIO.new("...")
|
496
511
|
@attachment = Dummy.new.avatar
|
497
512
|
end
|
498
513
|
|
499
514
|
it "only processes the provided style" do
|
500
|
-
@attachment.
|
501
|
-
@attachment.
|
515
|
+
expect(@attachment).to receive(:post_process).with(:thumb)
|
516
|
+
expect(@attachment).to receive(:post_process).with(:large).never
|
502
517
|
@attachment.assign(@file)
|
503
518
|
@attachment.save
|
504
519
|
end
|
@@ -507,15 +522,16 @@ describe Paperclip::Attachment do
|
|
507
522
|
context "An attachment with :convert_options that is a proc" do
|
508
523
|
before do
|
509
524
|
rebuild_model styles: {
|
510
|
-
|
511
|
-
|
512
|
-
|
525
|
+
thumb: "100x100",
|
526
|
+
large: "400x400"
|
527
|
+
},
|
513
528
|
convert_options: {
|
514
|
-
all: lambda{|i| i.all },
|
515
|
-
thumb: lambda{|i| i.thumb }
|
529
|
+
all: lambda { |i| i.all },
|
530
|
+
thumb: lambda { |i| i.thumb }
|
516
531
|
}
|
517
532
|
Dummy.class_eval do
|
518
533
|
def all; "-all"; end
|
534
|
+
|
519
535
|
def thumb; "-thumb"; end
|
520
536
|
end
|
521
537
|
@dummy = Dummy.new
|
@@ -533,12 +549,12 @@ describe Paperclip::Attachment do
|
|
533
549
|
|
534
550
|
context "An attachment with :path that is a proc" do
|
535
551
|
before do
|
536
|
-
rebuild_model path: lambda{ |attachment| "path/#{attachment.instance.other}.:extension" }
|
552
|
+
rebuild_model path: lambda { |attachment| "path/#{attachment.instance.other}.:extension" }
|
537
553
|
|
538
|
-
@file = File.new(fixture_file("5k.png"),
|
539
|
-
@dummyA = Dummy.new(other:
|
554
|
+
@file = File.new(fixture_file("5k.png"), "rb")
|
555
|
+
@dummyA = Dummy.new(other: "a")
|
540
556
|
@dummyA.avatar = @file
|
541
|
-
@dummyB = Dummy.new(other:
|
557
|
+
@dummyB = Dummy.new(other: "b")
|
542
558
|
@dummyB.avatar = @file
|
543
559
|
end
|
544
560
|
|
@@ -552,7 +568,7 @@ describe Paperclip::Attachment do
|
|
552
568
|
|
553
569
|
context "An attachment with :styles that is a proc" do
|
554
570
|
before do
|
555
|
-
rebuild_model styles: lambda{ |
|
571
|
+
rebuild_model styles: lambda { |_attachment| { thumb: "50x50#", large: "400x400" } }
|
556
572
|
|
557
573
|
@attachment = Dummy.new.avatar
|
558
574
|
end
|
@@ -564,16 +580,16 @@ describe Paperclip::Attachment do
|
|
564
580
|
|
565
581
|
context "An attachment with conditional :styles that is a proc" do
|
566
582
|
before do
|
567
|
-
rebuild_model styles: lambda{ |attachment| attachment.instance.other ==
|
583
|
+
rebuild_model styles: lambda { |attachment| attachment.instance.other == "a" ? { thumb: "50x50#" } : { large: "400x400" } }
|
568
584
|
|
569
|
-
@dummy = Dummy.new(other:
|
585
|
+
@dummy = Dummy.new(other: "a")
|
570
586
|
end
|
571
587
|
|
572
588
|
it "has the correct styles for the assigned instance values" do
|
573
589
|
assert_equal "50x50#", @dummy.avatar.styles[:thumb][:geometry]
|
574
590
|
assert_nil @dummy.avatar.styles[:large]
|
575
591
|
|
576
|
-
@dummy.other =
|
592
|
+
@dummy.other = "b"
|
577
593
|
|
578
594
|
assert_equal "400x400", @dummy.avatar.styles[:large][:geometry]
|
579
595
|
assert_nil @dummy.avatar.styles[:thumb]
|
@@ -581,9 +597,9 @@ describe Paperclip::Attachment do
|
|
581
597
|
end
|
582
598
|
|
583
599
|
geometry_specs = [
|
584
|
-
[
|
585
|
-
lambda{|
|
586
|
-
{ geometry: lambda{|
|
600
|
+
[lambda { |_z| "50x50#" }, :png],
|
601
|
+
lambda { |_z| "50x50#" },
|
602
|
+
{ geometry: lambda { |_z| "50x50#" } }
|
587
603
|
]
|
588
604
|
geometry_specs.each do |geometry_spec|
|
589
605
|
context "An attachment geometry like #{geometry_spec}" do
|
@@ -608,9 +624,9 @@ describe Paperclip::Attachment do
|
|
608
624
|
context "An attachment with both 'normal' and hash-style styles" do
|
609
625
|
before do
|
610
626
|
rebuild_model styles: {
|
611
|
-
|
612
|
-
|
613
|
-
|
627
|
+
normal: ["50x50#", :png],
|
628
|
+
hash: { geometry: "50x50#", format: :png }
|
629
|
+
}
|
614
630
|
@dummy = Dummy.new
|
615
631
|
@attachment = @dummy.avatar
|
616
632
|
end
|
@@ -626,9 +642,9 @@ describe Paperclip::Attachment do
|
|
626
642
|
before do
|
627
643
|
class Paperclip::Test < Paperclip::Processor; end
|
628
644
|
@file = StringIO.new("...")
|
629
|
-
Paperclip::Test.
|
645
|
+
allow(Paperclip::Test).to receive(:make).and_return(@file)
|
630
646
|
|
631
|
-
rebuild_model styles: { normal:
|
647
|
+
rebuild_model styles: { normal: "" }, processors: lambda { |_a| [:test] }
|
632
648
|
@attachment = Dummy.new.avatar
|
633
649
|
end
|
634
650
|
|
@@ -638,22 +654,22 @@ describe Paperclip::Attachment do
|
|
638
654
|
end
|
639
655
|
|
640
656
|
it "has the correct processors" do
|
641
|
-
assert_equal [
|
657
|
+
assert_equal [:test], @attachment.styles[:normal][:processors]
|
642
658
|
end
|
643
659
|
end
|
644
660
|
end
|
645
661
|
|
646
662
|
context "An attachment with erroring processor" do
|
647
663
|
before do
|
648
|
-
rebuild_model processor: [:thumbnail], styles: { small:
|
664
|
+
rebuild_model processor: [:thumbnail], styles: { small: "" }, whiny_thumbnails: true
|
649
665
|
@dummy = Dummy.new
|
650
666
|
@file = StringIO.new("...")
|
651
|
-
@file.
|
667
|
+
allow(@file).to receive(:to_tempfile).and_return(@file)
|
652
668
|
end
|
653
669
|
|
654
670
|
context "when error is meaningful for the end user" do
|
655
671
|
before do
|
656
|
-
Paperclip::Thumbnail.
|
672
|
+
expect(Paperclip::Thumbnail).to receive(:make).and_raise(
|
657
673
|
Paperclip::Errors::NotIdentifiedByImageMagickError,
|
658
674
|
"cannot be processed."
|
659
675
|
)
|
@@ -671,7 +687,7 @@ describe Paperclip::Attachment do
|
|
671
687
|
|
672
688
|
context "when error is intended for the developer" do
|
673
689
|
before do
|
674
|
-
Paperclip::Thumbnail.
|
690
|
+
expect(Paperclip::Thumbnail).to receive(:make).and_raise(
|
675
691
|
Paperclip::Errors::CommandNotFoundError
|
676
692
|
)
|
677
693
|
end
|
@@ -687,13 +703,13 @@ describe Paperclip::Attachment do
|
|
687
703
|
context "An attachment with multiple processors" do
|
688
704
|
before do
|
689
705
|
class Paperclip::Test < Paperclip::Processor; end
|
690
|
-
@style_params = { once: {one: 1, two: 2} }
|
706
|
+
@style_params = { once: { one: 1, two: 2 } }
|
691
707
|
rebuild_model processors: [:thumbnail, :test], styles: @style_params
|
692
708
|
@dummy = Dummy.new
|
693
709
|
@file = StringIO.new("...")
|
694
|
-
@file.
|
695
|
-
Paperclip::Test.
|
696
|
-
Paperclip::Thumbnail.
|
710
|
+
allow(@file).to receive(:close)
|
711
|
+
allow(Paperclip::Test).to receive(:make).and_return(@file)
|
712
|
+
allow(Paperclip::Thumbnail).to receive(:make).and_return(@file)
|
697
713
|
end
|
698
714
|
|
699
715
|
context "when assigned" do
|
@@ -705,13 +721,13 @@ describe Paperclip::Attachment do
|
|
705
721
|
end
|
706
722
|
|
707
723
|
it "calls #make with the right parameters passed as second argument" do
|
708
|
-
expected_params = @style_params[:once].merge(
|
724
|
+
expected_params = @style_params[:once].merge(
|
709
725
|
style: :once,
|
710
726
|
processors: [:thumbnail, :test],
|
711
727
|
whiny: true,
|
712
728
|
convert_options: "",
|
713
729
|
source_file_options: ""
|
714
|
-
|
730
|
+
)
|
715
731
|
|
716
732
|
@dummy.avatar = @file
|
717
733
|
|
@@ -725,7 +741,7 @@ describe Paperclip::Attachment do
|
|
725
741
|
end
|
726
742
|
|
727
743
|
it "calls #make and unlinks intermediary files afterward" do
|
728
|
-
@dummy.avatar.
|
744
|
+
expect(@dummy.avatar).to receive(:unlink_files).with([@file, @file])
|
729
745
|
|
730
746
|
@dummy.avatar = @file
|
731
747
|
end
|
@@ -739,13 +755,13 @@ describe Paperclip::Attachment do
|
|
739
755
|
end
|
740
756
|
rebuild_model processors: [:test], styles: { once: "100x100" }
|
741
757
|
@file = StringIO.new("...")
|
742
|
-
@file.
|
758
|
+
allow(@file).to receive(:close)
|
743
759
|
@dummy = Dummy.new
|
744
760
|
end
|
745
761
|
|
746
762
|
context "when assigned" do
|
747
763
|
it "#calls #make and doesn't unlink the original file" do
|
748
|
-
@dummy.avatar.
|
764
|
+
expect(@dummy.avatar).to receive(:unlink_files).with([])
|
749
765
|
|
750
766
|
@dummy.avatar = @file
|
751
767
|
end
|
@@ -786,12 +802,12 @@ describe Paperclip::Attachment do
|
|
786
802
|
|
787
803
|
context "An attachment with styles but no processors defined" do
|
788
804
|
before do
|
789
|
-
rebuild_model processors: [], styles: {something:
|
805
|
+
rebuild_model processors: [], styles: { something: "1" }
|
790
806
|
@dummy = Dummy.new
|
791
807
|
@file = StringIO.new("...")
|
792
808
|
end
|
793
809
|
it "raises when assigned to" do
|
794
|
-
assert_raises(RuntimeError){ @dummy.avatar = @file }
|
810
|
+
assert_raises(RuntimeError) { @dummy.avatar = @file }
|
795
811
|
end
|
796
812
|
end
|
797
813
|
|
@@ -815,50 +831,78 @@ describe Paperclip::Attachment do
|
|
815
831
|
before_post_process :do_before_all
|
816
832
|
after_post_process :do_after_all
|
817
833
|
def do_before_avatar; end
|
834
|
+
|
818
835
|
def do_after_avatar; end
|
836
|
+
|
819
837
|
def do_before_all; end
|
838
|
+
|
820
839
|
def do_after_all; end
|
821
840
|
end
|
822
|
-
@file
|
823
|
-
@file.
|
841
|
+
@file = StringIO.new(".")
|
842
|
+
allow(@file).to receive(:to_tempfile).and_return(@file)
|
824
843
|
@dummy = Dummy.new
|
825
|
-
Paperclip::Thumbnail.
|
844
|
+
allow(Paperclip::Thumbnail).to receive(:make).and_return(@file)
|
826
845
|
@attachment = @dummy.avatar
|
827
846
|
end
|
828
847
|
|
829
848
|
it "calls the defined callbacks when assigned" do
|
830
|
-
@dummy.
|
831
|
-
@dummy.
|
832
|
-
@dummy.
|
833
|
-
@dummy.
|
834
|
-
Paperclip::Thumbnail.
|
849
|
+
expect(@dummy).to receive(:do_before_avatar)
|
850
|
+
expect(@dummy).to receive(:do_after_avatar)
|
851
|
+
expect(@dummy).to receive(:do_before_all)
|
852
|
+
expect(@dummy).to receive(:do_after_all)
|
853
|
+
expect(Paperclip::Thumbnail).to receive(:make).and_return(@file)
|
835
854
|
@dummy.avatar = @file
|
836
855
|
end
|
837
856
|
|
838
857
|
it "does not cancel the processing if a before_post_process returns nil" do
|
839
|
-
@dummy.
|
840
|
-
@dummy.
|
841
|
-
@dummy.
|
842
|
-
@dummy.
|
843
|
-
Paperclip::Thumbnail.
|
858
|
+
expect(@dummy).to receive(:do_before_avatar).and_return(nil)
|
859
|
+
expect(@dummy).to receive(:do_after_avatar)
|
860
|
+
expect(@dummy).to receive(:do_before_all).and_return(nil)
|
861
|
+
expect(@dummy).to receive(:do_after_all)
|
862
|
+
expect(Paperclip::Thumbnail).to receive(:make).and_return(@file)
|
844
863
|
@dummy.avatar = @file
|
845
864
|
end
|
846
865
|
|
847
866
|
it "cancels the processing if a before_post_process returns false" do
|
848
|
-
@dummy.
|
849
|
-
@dummy.
|
850
|
-
@dummy.
|
851
|
-
@dummy.
|
852
|
-
Paperclip::Thumbnail.
|
867
|
+
expect(@dummy).to receive(:do_before_avatar).never
|
868
|
+
expect(@dummy).to receive(:do_after_avatar).never
|
869
|
+
expect(@dummy).to receive(:do_before_all).and_return(false)
|
870
|
+
expect(@dummy).to receive(:do_after_all)
|
871
|
+
expect(Paperclip::Thumbnail).not_to receive(:make)
|
853
872
|
@dummy.avatar = @file
|
854
873
|
end
|
855
874
|
|
856
875
|
it "cancels the processing if a before_avatar_post_process returns false" do
|
857
|
-
@dummy.
|
858
|
-
@dummy.
|
859
|
-
@dummy.
|
860
|
-
@dummy.
|
861
|
-
Paperclip::Thumbnail.
|
876
|
+
expect(@dummy).to receive(:do_before_avatar).and_return(false)
|
877
|
+
expect(@dummy).to receive(:do_after_avatar)
|
878
|
+
expect(@dummy).to receive(:do_before_all).and_return(true)
|
879
|
+
expect(@dummy).to receive(:do_after_all)
|
880
|
+
expect(Paperclip::Thumbnail).not_to receive(:make)
|
881
|
+
@dummy.avatar = @file
|
882
|
+
end
|
883
|
+
|
884
|
+
it "should not call process hooks if validation fails" do
|
885
|
+
Dummy.class_eval do
|
886
|
+
validates_attachment_content_type :avatar, content_type: 'image/jpeg'
|
887
|
+
end
|
888
|
+
expect(@dummy).not_to receive(:do_before_avatar)
|
889
|
+
expect(@dummy).not_to receive(:do_after_avatar)
|
890
|
+
expect(@dummy).not_to receive(:do_before_all)
|
891
|
+
expect(@dummy).not_to receive(:do_after_all)
|
892
|
+
expect(Paperclip::Thumbnail).not_to receive(:make)
|
893
|
+
@dummy.avatar = @file
|
894
|
+
end
|
895
|
+
|
896
|
+
it "should call process hooks if validation would fail but check validity flag is false" do
|
897
|
+
Dummy.class_eval do
|
898
|
+
validates_attachment_content_type :avatar, content_type: 'image/jpeg'
|
899
|
+
end
|
900
|
+
@dummy.avatar.options[:check_validity_before_processing] = false
|
901
|
+
expect(@dummy).to receive(:do_before_avatar)
|
902
|
+
expect(@dummy).to receive(:do_after_avatar)
|
903
|
+
expect(@dummy).to receive(:do_before_all)
|
904
|
+
expect(@dummy).to receive(:do_after_all)
|
905
|
+
expect(Paperclip::Thumbnail).to receive(:make).and_return(@file)
|
862
906
|
@dummy.avatar = @file
|
863
907
|
end
|
864
908
|
end
|
@@ -897,7 +941,7 @@ describe Paperclip::Attachment do
|
|
897
941
|
before do
|
898
942
|
rebuild_model
|
899
943
|
@file = File.new(fixture_file("5k.png"), "rb")
|
900
|
-
@file.
|
944
|
+
allow(@file).to receive(:original_filename).and_return("sheep_say_bæ.png")
|
901
945
|
@dummy = Dummy.new
|
902
946
|
@dummy.avatar = @file
|
903
947
|
end
|
@@ -910,7 +954,7 @@ describe Paperclip::Attachment do
|
|
910
954
|
context "Attachment with reserved filename" do
|
911
955
|
before do
|
912
956
|
rebuild_model
|
913
|
-
@file = Tempfile.new(["filename","png"])
|
957
|
+
@file = Tempfile.new(["filename", "png"])
|
914
958
|
end
|
915
959
|
|
916
960
|
after do
|
@@ -920,10 +964,9 @@ describe Paperclip::Attachment do
|
|
920
964
|
context "with default configuration" do
|
921
965
|
"&$+,/:;=?@<>[]{}|\^~%# ".split(//).each do |character|
|
922
966
|
context "with character #{character}" do
|
923
|
-
|
924
967
|
context "at beginning of filename" do
|
925
968
|
before do
|
926
|
-
@file.
|
969
|
+
allow(@file).to receive(:original_filename).and_return("#{character}filename.png")
|
927
970
|
@dummy = Dummy.new
|
928
971
|
@dummy.avatar = @file
|
929
972
|
end
|
@@ -935,7 +978,7 @@ describe Paperclip::Attachment do
|
|
935
978
|
|
936
979
|
context "at end of filename" do
|
937
980
|
before do
|
938
|
-
@file.
|
981
|
+
allow(@file).to receive(:original_filename).and_return("filename.png#{character}")
|
939
982
|
@dummy = Dummy.new
|
940
983
|
@dummy.avatar = @file
|
941
984
|
end
|
@@ -947,7 +990,7 @@ describe Paperclip::Attachment do
|
|
947
990
|
|
948
991
|
context "in the middle of filename" do
|
949
992
|
before do
|
950
|
-
@file.
|
993
|
+
allow(@file).to receive(:original_filename).and_return("file#{character}name.png")
|
951
994
|
@dummy = Dummy.new
|
952
995
|
@dummy.avatar = @file
|
953
996
|
end
|
@@ -956,7 +999,6 @@ describe Paperclip::Attachment do
|
|
956
999
|
assert_equal "file_name.png", @dummy.avatar.original_filename
|
957
1000
|
end
|
958
1001
|
end
|
959
|
-
|
960
1002
|
end
|
961
1003
|
end
|
962
1004
|
end
|
@@ -970,11 +1012,11 @@ describe Paperclip::Attachment do
|
|
970
1012
|
Paperclip::Attachment.default_options.merge! @old_defaults
|
971
1013
|
end
|
972
1014
|
|
973
|
-
context
|
1015
|
+
context "as another regexp" do
|
974
1016
|
before do
|
975
1017
|
Paperclip::Attachment.default_options.merge! restricted_characters: /o/
|
976
1018
|
|
977
|
-
@file.
|
1019
|
+
allow(@file).to receive(:original_filename).and_return("goood.png")
|
978
1020
|
@dummy = Dummy.new
|
979
1021
|
@dummy.avatar = @file
|
980
1022
|
end
|
@@ -984,11 +1026,11 @@ describe Paperclip::Attachment do
|
|
984
1026
|
end
|
985
1027
|
end
|
986
1028
|
|
987
|
-
context
|
1029
|
+
context "as nil" do
|
988
1030
|
before do
|
989
1031
|
Paperclip::Attachment.default_options.merge! restricted_characters: nil
|
990
1032
|
|
991
|
-
@file.
|
1033
|
+
allow(@file).to receive(:original_filename).and_return("goood.png")
|
992
1034
|
@dummy = Dummy.new
|
993
1035
|
@dummy.avatar = @file
|
994
1036
|
end
|
@@ -999,7 +1041,7 @@ describe Paperclip::Attachment do
|
|
999
1041
|
end
|
1000
1042
|
end
|
1001
1043
|
|
1002
|
-
context
|
1044
|
+
context "with specified cleaner" do
|
1003
1045
|
before do
|
1004
1046
|
@old_defaults = Paperclip::Attachment.default_options.dup
|
1005
1047
|
end
|
@@ -1008,26 +1050,26 @@ describe Paperclip::Attachment do
|
|
1008
1050
|
Paperclip::Attachment.default_options.merge! @old_defaults
|
1009
1051
|
end
|
1010
1052
|
|
1011
|
-
it
|
1053
|
+
it "calls the given proc and take the result as cleaned filename" do
|
1012
1054
|
Paperclip::Attachment.default_options[:filename_cleaner] = lambda do |str|
|
1013
1055
|
"from_proc_#{str}"
|
1014
1056
|
end
|
1015
1057
|
|
1016
|
-
@file.
|
1058
|
+
allow(@file).to receive(:original_filename).and_return("goood.png")
|
1017
1059
|
@dummy = Dummy.new
|
1018
1060
|
@dummy.avatar = @file
|
1019
1061
|
assert_equal "from_proc_goood.png", @dummy.avatar.original_filename
|
1020
1062
|
end
|
1021
1063
|
|
1022
|
-
it
|
1064
|
+
it "calls the given object and take the result as the cleaned filename" do
|
1023
1065
|
class MyCleaner
|
1024
|
-
def call(
|
1066
|
+
def call(_filename)
|
1025
1067
|
"foo"
|
1026
1068
|
end
|
1027
1069
|
end
|
1028
1070
|
Paperclip::Attachment.default_options[:filename_cleaner] = MyCleaner.new
|
1029
1071
|
|
1030
|
-
@file.
|
1072
|
+
allow(@file).to receive(:original_filename).and_return("goood.png")
|
1031
1073
|
@dummy = Dummy.new
|
1032
1074
|
@dummy.avatar = @file
|
1033
1075
|
assert_equal "foo", @dummy.avatar.original_filename
|
@@ -1038,22 +1080,22 @@ describe Paperclip::Attachment do
|
|
1038
1080
|
context "Attachment with uppercase extension and a default style" do
|
1039
1081
|
before do
|
1040
1082
|
@old_defaults = Paperclip::Attachment.default_options.dup
|
1041
|
-
Paperclip::Attachment.default_options.merge!(
|
1083
|
+
Paperclip::Attachment.default_options.merge!(
|
1042
1084
|
path: ":rails_root/:attachment/:class/:style/:id/:basename.:extension"
|
1043
|
-
|
1085
|
+
)
|
1044
1086
|
FileUtils.rm_rf("tmp")
|
1045
1087
|
rebuild_model styles: { large: ["400x400", :jpg],
|
1046
|
-
|
1047
|
-
|
1088
|
+
medium: ["100x100", :jpg],
|
1089
|
+
small: ["32x32#", :jpg] },
|
1048
1090
|
default_style: :small
|
1049
1091
|
@instance = Dummy.new
|
1050
|
-
@instance.
|
1051
|
-
@file = File.new(fixture_file("uppercase.PNG"),
|
1092
|
+
allow(@instance).to receive(:id).and_return 123
|
1093
|
+
@file = File.new(fixture_file("uppercase.PNG"), "rb")
|
1052
1094
|
|
1053
1095
|
@attachment = @instance.avatar
|
1054
1096
|
|
1055
1097
|
now = Time.now
|
1056
|
-
Time.
|
1098
|
+
allow(Time).to receive(:now).and_return(now)
|
1057
1099
|
@attachment.assign(@file)
|
1058
1100
|
@attachment.save
|
1059
1101
|
end
|
@@ -1076,16 +1118,16 @@ describe Paperclip::Attachment do
|
|
1076
1118
|
context "An attachment" do
|
1077
1119
|
before do
|
1078
1120
|
@old_defaults = Paperclip::Attachment.default_options.dup
|
1079
|
-
Paperclip::Attachment.default_options.merge!(
|
1121
|
+
Paperclip::Attachment.default_options.merge!(
|
1080
1122
|
path: ":rails_root/:attachment/:class/:style/:id/:basename.:extension"
|
1081
|
-
|
1123
|
+
)
|
1082
1124
|
FileUtils.rm_rf("tmp")
|
1083
1125
|
rebuild_model
|
1084
1126
|
@instance = Dummy.new
|
1085
|
-
@instance.
|
1127
|
+
allow(@instance).to receive(:id).and_return 123
|
1086
1128
|
# @attachment = Paperclip::Attachment.new(:avatar, @instance)
|
1087
1129
|
@attachment = @instance.avatar
|
1088
|
-
@file = File.new(fixture_file("5k.png"),
|
1130
|
+
@file = File.new(fixture_file("5k.png"), "rb")
|
1089
1131
|
end
|
1090
1132
|
|
1091
1133
|
after do
|
@@ -1100,14 +1142,14 @@ describe Paperclip::Attachment do
|
|
1100
1142
|
end
|
1101
1143
|
end
|
1102
1144
|
|
1103
|
-
it
|
1145
|
+
it "clears out the previous assignment when assigned nil" do
|
1104
1146
|
@attachment.assign(@file)
|
1105
1147
|
@attachment.queued_for_write[:original]
|
1106
1148
|
@attachment.assign(nil)
|
1107
1149
|
assert_nil @attachment.queued_for_write[:original]
|
1108
1150
|
end
|
1109
1151
|
|
1110
|
-
it
|
1152
|
+
it "does not do anything when it is assigned an empty string" do
|
1111
1153
|
@attachment.assign(@file)
|
1112
1154
|
original_file = @attachment.queued_for_write[:original]
|
1113
1155
|
@attachment.assign("")
|
@@ -1130,13 +1172,13 @@ describe Paperclip::Attachment do
|
|
1130
1172
|
|
1131
1173
|
context "with a file assigned in the database" do
|
1132
1174
|
before do
|
1133
|
-
@attachment.
|
1134
|
-
@attachment.
|
1135
|
-
@attachment.
|
1175
|
+
allow(@attachment).to receive(:instance_read).with(:file_name).and_return("5k.png")
|
1176
|
+
allow(@attachment).to receive(:instance_read).with(:content_type).and_return("image/png")
|
1177
|
+
allow(@attachment).to receive(:instance_read).with(:file_size).and_return(12345)
|
1136
1178
|
dtnow = DateTime.now
|
1137
1179
|
@now = Time.now
|
1138
|
-
Time.
|
1139
|
-
@attachment.
|
1180
|
+
allow(Time).to receive(:now).and_return(@now)
|
1181
|
+
allow(@attachment).to receive(:instance_read).with(:updated_at).and_return(dtnow)
|
1140
1182
|
end
|
1141
1183
|
|
1142
1184
|
it "returns the proper path when filename has a single .'s" do
|
@@ -1144,7 +1186,7 @@ describe Paperclip::Attachment do
|
|
1144
1186
|
end
|
1145
1187
|
|
1146
1188
|
it "returns the proper path when filename has multiple .'s" do
|
1147
|
-
@attachment.
|
1189
|
+
allow(@attachment).to receive(:instance_read).with(:file_name).and_return("5k.old.png")
|
1148
1190
|
assert_equal File.expand_path("tmp/avatars/dummies/original/#{@instance.id}/5k.old.png"), File.expand_path(@attachment.path)
|
1149
1191
|
end
|
1150
1192
|
|
@@ -1156,15 +1198,15 @@ describe Paperclip::Attachment do
|
|
1156
1198
|
small: ["32x32#", :jpg]
|
1157
1199
|
}
|
1158
1200
|
@instance = Dummy.new
|
1159
|
-
@instance.
|
1160
|
-
@file = File.new(fixture_file("5k.png"),
|
1201
|
+
allow(@instance).to receive(:id).and_return 123
|
1202
|
+
@file = File.new(fixture_file("5k.png"), "rb")
|
1161
1203
|
@attachment = @instance.avatar
|
1162
1204
|
end
|
1163
1205
|
|
1164
1206
|
context "and assigned a file" do
|
1165
1207
|
before do
|
1166
1208
|
now = Time.now
|
1167
|
-
Time.
|
1209
|
+
allow(Time).to receive(:now).and_return(now)
|
1168
1210
|
@attachment.assign(@file)
|
1169
1211
|
end
|
1170
1212
|
|
@@ -1187,7 +1229,7 @@ describe Paperclip::Attachment do
|
|
1187
1229
|
[[:large, 400, 61, "PNG"],
|
1188
1230
|
[:medium, 100, 15, "GIF"],
|
1189
1231
|
[:small, 32, 32, "JPEG"]].each do |style|
|
1190
|
-
cmd = %
|
1232
|
+
cmd = %[identify -format "%w %h %b %m" "#{@attachment.path(style.first)}"]
|
1191
1233
|
out = `#{cmd}`
|
1192
1234
|
width, height, _size, format = out.split(" ")
|
1193
1235
|
assert_equal style[1].to_s, width.to_s
|
@@ -1198,41 +1240,41 @@ describe Paperclip::Attachment do
|
|
1198
1240
|
|
1199
1241
|
context "and trying to delete" do
|
1200
1242
|
before do
|
1201
|
-
@existing_names = @attachment.styles.keys.
|
1243
|
+
@existing_names = @attachment.styles.keys.map do |style|
|
1202
1244
|
@attachment.path(style)
|
1203
1245
|
end
|
1204
1246
|
end
|
1205
1247
|
|
1206
1248
|
it "deletes the files after assigning nil" do
|
1207
|
-
@attachment.
|
1208
|
-
@attachment.
|
1209
|
-
@attachment.
|
1210
|
-
@attachment.
|
1211
|
-
@attachment.
|
1249
|
+
expect(@attachment).to receive(:instance_write).with(:file_name, nil)
|
1250
|
+
expect(@attachment).to receive(:instance_write).with(:content_type, nil)
|
1251
|
+
expect(@attachment).to receive(:instance_write).with(:file_size, nil)
|
1252
|
+
expect(@attachment).to receive(:instance_write).with(:fingerprint, nil)
|
1253
|
+
expect(@attachment).to receive(:instance_write).with(:updated_at, nil)
|
1212
1254
|
@attachment.assign nil
|
1213
1255
|
@attachment.save
|
1214
|
-
@existing_names.each{|f| assert_file_not_exists(f) }
|
1256
|
+
@existing_names.each { |f| assert_file_not_exists(f) }
|
1215
1257
|
end
|
1216
1258
|
|
1217
1259
|
it "deletes the files when you call #clear and #save" do
|
1218
|
-
@attachment.
|
1219
|
-
@attachment.
|
1220
|
-
@attachment.
|
1221
|
-
@attachment.
|
1222
|
-
@attachment.
|
1260
|
+
expect(@attachment).to receive(:instance_write).with(:file_name, nil)
|
1261
|
+
expect(@attachment).to receive(:instance_write).with(:content_type, nil)
|
1262
|
+
expect(@attachment).to receive(:instance_write).with(:file_size, nil)
|
1263
|
+
expect(@attachment).to receive(:instance_write).with(:fingerprint, nil)
|
1264
|
+
expect(@attachment).to receive(:instance_write).with(:updated_at, nil)
|
1223
1265
|
@attachment.clear
|
1224
1266
|
@attachment.save
|
1225
|
-
@existing_names.each{|f| assert_file_not_exists(f) }
|
1267
|
+
@existing_names.each { |f| assert_file_not_exists(f) }
|
1226
1268
|
end
|
1227
1269
|
|
1228
1270
|
it "deletes the files when you call #delete" do
|
1229
|
-
@attachment.
|
1230
|
-
@attachment.
|
1231
|
-
@attachment.
|
1232
|
-
@attachment.
|
1233
|
-
@attachment.
|
1271
|
+
expect(@attachment).to receive(:instance_write).with(:file_name, nil)
|
1272
|
+
expect(@attachment).to receive(:instance_write).with(:content_type, nil)
|
1273
|
+
expect(@attachment).to receive(:instance_write).with(:file_size, nil)
|
1274
|
+
expect(@attachment).to receive(:instance_write).with(:fingerprint, nil)
|
1275
|
+
expect(@attachment).to receive(:instance_write).with(:updated_at, nil)
|
1234
1276
|
@attachment.destroy
|
1235
|
-
@existing_names.each{|f| assert_file_not_exists(f) }
|
1277
|
+
@existing_names.each { |f| assert_file_not_exists(f) }
|
1236
1278
|
end
|
1237
1279
|
|
1238
1280
|
context "when keeping old files" do
|
@@ -1241,35 +1283,35 @@ describe Paperclip::Attachment do
|
|
1241
1283
|
end
|
1242
1284
|
|
1243
1285
|
it "keeps the files after assigning nil" do
|
1244
|
-
@attachment.
|
1245
|
-
@attachment.
|
1246
|
-
@attachment.
|
1247
|
-
@attachment.
|
1248
|
-
@attachment.
|
1286
|
+
expect(@attachment).to receive(:instance_write).with(:file_name, nil)
|
1287
|
+
expect(@attachment).to receive(:instance_write).with(:content_type, nil)
|
1288
|
+
expect(@attachment).to receive(:instance_write).with(:file_size, nil)
|
1289
|
+
expect(@attachment).to receive(:instance_write).with(:fingerprint, nil)
|
1290
|
+
expect(@attachment).to receive(:instance_write).with(:updated_at, nil)
|
1249
1291
|
@attachment.assign nil
|
1250
1292
|
@attachment.save
|
1251
|
-
@existing_names.each{|f| assert_file_exists(f) }
|
1293
|
+
@existing_names.each { |f| assert_file_exists(f) }
|
1252
1294
|
end
|
1253
1295
|
|
1254
1296
|
it "keeps the files when you call #clear and #save" do
|
1255
|
-
@attachment.
|
1256
|
-
@attachment.
|
1257
|
-
@attachment.
|
1258
|
-
@attachment.
|
1259
|
-
@attachment.
|
1297
|
+
expect(@attachment).to receive(:instance_write).with(:file_name, nil)
|
1298
|
+
expect(@attachment).to receive(:instance_write).with(:content_type, nil)
|
1299
|
+
expect(@attachment).to receive(:instance_write).with(:file_size, nil)
|
1300
|
+
expect(@attachment).to receive(:instance_write).with(:fingerprint, nil)
|
1301
|
+
expect(@attachment).to receive(:instance_write).with(:updated_at, nil)
|
1260
1302
|
@attachment.clear
|
1261
1303
|
@attachment.save
|
1262
|
-
@existing_names.each{|f| assert_file_exists(f) }
|
1304
|
+
@existing_names.each { |f| assert_file_exists(f) }
|
1263
1305
|
end
|
1264
1306
|
|
1265
1307
|
it "keeps the files when you call #delete" do
|
1266
|
-
@attachment.
|
1267
|
-
@attachment.
|
1268
|
-
@attachment.
|
1269
|
-
@attachment.
|
1270
|
-
@attachment.
|
1308
|
+
expect(@attachment).to receive(:instance_write).with(:file_name, nil)
|
1309
|
+
expect(@attachment).to receive(:instance_write).with(:content_type, nil)
|
1310
|
+
expect(@attachment).to receive(:instance_write).with(:file_size, nil)
|
1311
|
+
expect(@attachment).to receive(:instance_write).with(:fingerprint, nil)
|
1312
|
+
expect(@attachment).to receive(:instance_write).with(:updated_at, nil)
|
1271
1313
|
@attachment.destroy
|
1272
|
-
@existing_names.each{|f| assert_file_exists(f) }
|
1314
|
+
@existing_names.each { |f| assert_file_exists(f) }
|
1273
1315
|
end
|
1274
1316
|
end
|
1275
1317
|
end
|
@@ -1284,7 +1326,7 @@ describe Paperclip::Attachment do
|
|
1284
1326
|
end
|
1285
1327
|
|
1286
1328
|
it "is not able to find the module" do
|
1287
|
-
assert_raises(Paperclip::Errors::StorageMethodNotFound){ Dummy.new.avatar }
|
1329
|
+
assert_raises(Paperclip::Errors::StorageMethodNotFound) { Dummy.new.avatar }
|
1288
1330
|
end
|
1289
1331
|
end
|
1290
1332
|
end
|
@@ -1296,7 +1338,7 @@ describe Paperclip::Attachment do
|
|
1296
1338
|
end
|
1297
1339
|
rebuild_class
|
1298
1340
|
@dummy = Dummy.new
|
1299
|
-
@file = File.new(fixture_file("5k.png"),
|
1341
|
+
@file = File.new(fixture_file("5k.png"), "rb")
|
1300
1342
|
end
|
1301
1343
|
|
1302
1344
|
after { @file.close }
|
@@ -1328,7 +1370,7 @@ describe Paperclip::Attachment do
|
|
1328
1370
|
|
1329
1371
|
it "returns the creation time when sent #avatar_created_at" do
|
1330
1372
|
now = Time.now
|
1331
|
-
Time.
|
1373
|
+
allow(Time).to receive(:now).and_return(now)
|
1332
1374
|
@dummy.avatar = @file
|
1333
1375
|
assert_equal now.to_i, @dummy.avatar.created_at
|
1334
1376
|
end
|
@@ -1336,9 +1378,9 @@ describe Paperclip::Attachment do
|
|
1336
1378
|
it "returns the creation time when sent #avatar_created_at and the entry has been updated" do
|
1337
1379
|
creation = 2.hours.ago
|
1338
1380
|
now = Time.now
|
1339
|
-
Time.
|
1381
|
+
allow(Time).to receive(:now).and_return(creation)
|
1340
1382
|
@dummy.avatar = @file
|
1341
|
-
Time.
|
1383
|
+
allow(Time).to receive(:now).and_return(now)
|
1342
1384
|
@dummy.avatar = @file
|
1343
1385
|
assert_equal creation.to_i, @dummy.avatar.created_at
|
1344
1386
|
assert_not_equal now.to_i, @dummy.avatar.created_at
|
@@ -1365,16 +1407,16 @@ describe Paperclip::Attachment do
|
|
1365
1407
|
|
1366
1408
|
it "returns the right value when sent #avatar_updated_at" do
|
1367
1409
|
now = Time.now
|
1368
|
-
Time.
|
1410
|
+
allow(Time).to receive(:now).and_return(now)
|
1369
1411
|
@dummy.avatar = @file
|
1370
1412
|
assert_equal now.to_i, @dummy.avatar.updated_at
|
1371
1413
|
end
|
1372
1414
|
end
|
1373
1415
|
|
1374
1416
|
it "does not calculate fingerprint" do
|
1375
|
-
Digest::MD5.
|
1417
|
+
allow(Digest::MD5).to receive(:file)
|
1376
1418
|
@dummy.avatar = @file
|
1377
|
-
expect(Digest::MD5).
|
1419
|
+
expect(Digest::MD5).not_to have_received(:file)
|
1378
1420
|
end
|
1379
1421
|
|
1380
1422
|
it "does not assign fingerprint" do
|
@@ -1401,7 +1443,7 @@ describe Paperclip::Attachment do
|
|
1401
1443
|
|
1402
1444
|
context "and avatar_file_size column" do
|
1403
1445
|
before do
|
1404
|
-
ActiveRecord::Base.connection.add_column :dummies, :avatar_file_size, :
|
1446
|
+
ActiveRecord::Base.connection.add_column :dummies, :avatar_file_size, :bigint
|
1405
1447
|
rebuild_class
|
1406
1448
|
@dummy = Dummy.new
|
1407
1449
|
end
|
@@ -1482,7 +1524,7 @@ describe Paperclip::Attachment do
|
|
1482
1524
|
before do
|
1483
1525
|
rebuild_model preserve_files: true
|
1484
1526
|
@dummy = Dummy.new
|
1485
|
-
@file = File.new(fixture_file("5k.png"),
|
1527
|
+
@file = File.new(fixture_file("5k.png"), "rb")
|
1486
1528
|
@dummy.avatar = @file
|
1487
1529
|
@dummy.save!
|
1488
1530
|
@attachment = @dummy.avatar
|
@@ -1512,7 +1554,7 @@ describe Paperclip::Attachment do
|
|
1512
1554
|
before do
|
1513
1555
|
rebuild_model
|
1514
1556
|
@dummy = Dummy.new
|
1515
|
-
@file = File.new(fixture_file("5k.png"),
|
1557
|
+
@file = File.new(fixture_file("5k.png"), "rb")
|
1516
1558
|
@dummy.avatar = @file
|
1517
1559
|
@dummy.save!
|
1518
1560
|
@attachment = @dummy.avatar
|
@@ -1522,7 +1564,7 @@ describe Paperclip::Attachment do
|
|
1522
1564
|
after { @file.close }
|
1523
1565
|
|
1524
1566
|
it "is not deleted when the model fails to destroy" do
|
1525
|
-
@dummy.
|
1567
|
+
allow(@dummy).to receive(:destroy).and_raise(Exception)
|
1526
1568
|
|
1527
1569
|
assert_raises Exception do
|
1528
1570
|
@dummy.destroy
|