paperclip 4.2.4 → 5.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (127) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +17 -0
  3. data/.hound.yml +1055 -0
  4. data/.rubocop.yml +1 -0
  5. data/.travis.yml +17 -15
  6. data/Appraisals +4 -16
  7. data/CONTRIBUTING.md +19 -8
  8. data/Gemfile +4 -8
  9. data/LICENSE +1 -1
  10. data/NEWS +105 -2
  11. data/README.md +327 -191
  12. data/RELEASING.md +17 -0
  13. data/Rakefile +2 -2
  14. data/UPGRADING +12 -9
  15. data/features/basic_integration.feature +10 -6
  16. data/features/migration.feature +0 -24
  17. data/features/step_definitions/attachment_steps.rb +33 -27
  18. data/features/step_definitions/html_steps.rb +2 -2
  19. data/features/step_definitions/rails_steps.rb +39 -38
  20. data/features/step_definitions/s3_steps.rb +2 -2
  21. data/features/step_definitions/web_steps.rb +1 -103
  22. data/features/support/env.rb +1 -0
  23. data/features/support/file_helpers.rb +2 -2
  24. data/features/support/paths.rb +1 -1
  25. data/features/support/rails.rb +0 -24
  26. data/gemfiles/4.2.gemfile +5 -7
  27. data/gemfiles/5.0.gemfile +17 -0
  28. data/lib/paperclip/attachment.rb +32 -20
  29. data/lib/paperclip/attachment_registry.rb +3 -2
  30. data/lib/paperclip/callbacks.rb +8 -6
  31. data/lib/paperclip/content_type_detector.rb +27 -11
  32. data/lib/paperclip/errors.rb +3 -1
  33. data/lib/paperclip/file_command_content_type_detector.rb +6 -8
  34. data/lib/paperclip/geometry_parser_factory.rb +1 -1
  35. data/lib/paperclip/glue.rb +1 -1
  36. data/lib/paperclip/has_attached_file.rb +9 -2
  37. data/lib/paperclip/helpers.rb +14 -10
  38. data/lib/paperclip/interpolations/plural_cache.rb +6 -5
  39. data/lib/paperclip/interpolations.rb +19 -14
  40. data/lib/paperclip/io_adapters/abstract_adapter.rb +26 -3
  41. data/lib/paperclip/io_adapters/attachment_adapter.rb +10 -5
  42. data/lib/paperclip/io_adapters/data_uri_adapter.rb +8 -8
  43. data/lib/paperclip/io_adapters/empty_string_adapter.rb +5 -4
  44. data/lib/paperclip/io_adapters/file_adapter.rb +12 -6
  45. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +7 -7
  46. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
  47. data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
  48. data/lib/paperclip/io_adapters/registry.rb +6 -2
  49. data/lib/paperclip/io_adapters/stringio_adapter.rb +9 -6
  50. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +10 -6
  51. data/lib/paperclip/io_adapters/uri_adapter.rb +41 -19
  52. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
  53. data/lib/paperclip/media_type_spoof_detector.rb +2 -2
  54. data/lib/paperclip/processor.rb +5 -4
  55. data/lib/paperclip/rails_environment.rb +25 -0
  56. data/lib/paperclip/schema.rb +3 -9
  57. data/lib/paperclip/storage/filesystem.rb +13 -2
  58. data/lib/paperclip/storage/fog.rb +30 -18
  59. data/lib/paperclip/storage/s3.rb +92 -65
  60. data/lib/paperclip/thumbnail.rb +16 -7
  61. data/lib/paperclip/url_generator.rb +16 -13
  62. data/lib/paperclip/validators/attachment_size_validator.rb +1 -7
  63. data/lib/paperclip/validators.rb +1 -1
  64. data/lib/paperclip/version.rb +3 -1
  65. data/lib/paperclip.rb +25 -12
  66. data/lib/tasks/paperclip.rake +33 -3
  67. data/paperclip.gemspec +17 -14
  68. data/spec/paperclip/attachment_definitions_spec.rb +1 -1
  69. data/spec/paperclip/attachment_processing_spec.rb +2 -4
  70. data/spec/paperclip/attachment_registry_spec.rb +84 -13
  71. data/spec/paperclip/attachment_spec.rb +130 -39
  72. data/spec/paperclip/content_type_detector_spec.rb +8 -1
  73. data/spec/paperclip/file_command_content_type_detector_spec.rb +0 -1
  74. data/spec/paperclip/geometry_spec.rb +1 -1
  75. data/spec/paperclip/glue_spec.rb +44 -0
  76. data/spec/paperclip/has_attached_file_spec.rb +24 -8
  77. data/spec/paperclip/integration_spec.rb +4 -3
  78. data/spec/paperclip/interpolations_spec.rb +16 -13
  79. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +47 -23
  80. data/spec/paperclip/io_adapters/attachment_adapter_spec.rb +6 -3
  81. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +7 -1
  82. data/spec/paperclip/io_adapters/file_adapter_spec.rb +6 -3
  83. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +26 -6
  84. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +1 -1
  85. data/spec/paperclip/io_adapters/registry_spec.rb +2 -2
  86. data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +5 -1
  87. data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +5 -5
  88. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +77 -7
  89. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +10 -0
  90. data/spec/paperclip/media_type_spoof_detector_spec.rb +28 -5
  91. data/spec/paperclip/paperclip_spec.rb +3 -28
  92. data/spec/paperclip/plural_cache_spec.rb +17 -16
  93. data/spec/paperclip/rails_environment_spec.rb +33 -0
  94. data/spec/paperclip/storage/fog_spec.rb +58 -3
  95. data/spec/paperclip/storage/s3_live_spec.rb +20 -14
  96. data/spec/paperclip/storage/s3_spec.rb +398 -213
  97. data/spec/paperclip/tempfile_factory_spec.rb +4 -0
  98. data/spec/paperclip/tempfile_spec.rb +35 -0
  99. data/spec/paperclip/thumbnail_spec.rb +51 -32
  100. data/spec/paperclip/url_generator_spec.rb +55 -44
  101. data/spec/paperclip/validators/attachment_size_validator_spec.rb +26 -20
  102. data/spec/paperclip/validators_spec.rb +5 -5
  103. data/spec/spec_helper.rb +8 -1
  104. data/spec/support/assertions.rb +12 -1
  105. data/spec/support/conditional_filter_helper.rb +5 -0
  106. data/spec/support/fake_model.rb +4 -0
  107. data/spec/support/fixtures/empty.xlsx +0 -0
  108. data/spec/support/matchers/have_column.rb +11 -2
  109. data/spec/support/mock_attachment.rb +2 -0
  110. data/spec/support/mock_url_generator_builder.rb +2 -2
  111. data/spec/support/model_reconstruction.rb +9 -1
  112. data/spec/support/reporting.rb +11 -0
  113. metadata +105 -164
  114. data/RUNNING_TESTS.md +0 -4
  115. data/cucumber/paperclip_steps.rb +0 -6
  116. data/gemfiles/3.2.gemfile +0 -19
  117. data/gemfiles/4.0.gemfile +0 -19
  118. data/gemfiles/4.1.gemfile +0 -19
  119. data/lib/paperclip/locales/de.yml +0 -18
  120. data/lib/paperclip/locales/es.yml +0 -18
  121. data/lib/paperclip/locales/ja.yml +0 -18
  122. data/lib/paperclip/locales/pt-BR.yml +0 -18
  123. data/lib/paperclip/locales/zh-CN.yml +0 -18
  124. data/lib/paperclip/locales/zh-HK.yml +0 -18
  125. data/lib/paperclip/locales/zh-TW.yml +0 -18
  126. data/spec/support/mock_model.rb +0 -2
  127. data/spec/support/rails_helpers.rb +0 -7
@@ -26,4 +26,8 @@ describe Paperclip::TempfileFactory do
26
26
  file = subject.generate
27
27
  assert File.exist?(file.path)
28
28
  end
29
+
30
+ it "does not throw Errno::ENAMETOOLONG when it has a really long name" do
31
+ expect { subject.generate("o" * 255) }.to_not raise_error
32
+ end
29
33
  end
@@ -0,0 +1,35 @@
1
+ require "spec_helper"
2
+
3
+ describe Paperclip::Tempfile do
4
+ context "A Paperclip Tempfile" do
5
+ before do
6
+ @tempfile = described_class.new(["file", ".jpg"])
7
+ end
8
+
9
+ after { @tempfile.close }
10
+
11
+ it "has its path contain a real extension" do
12
+ assert_equal ".jpg", File.extname(@tempfile.path)
13
+ end
14
+
15
+ it "is a real Tempfile" do
16
+ assert @tempfile.is_a?(::Tempfile)
17
+ end
18
+ end
19
+
20
+ context "Another Paperclip Tempfile" do
21
+ before do
22
+ @tempfile = described_class.new("file")
23
+ end
24
+
25
+ after { @tempfile.close }
26
+
27
+ it "does not have an extension if not given one" do
28
+ assert_equal "", File.extname(@tempfile.path)
29
+ end
30
+
31
+ it "is a real Tempfile" do
32
+ assert @tempfile.is_a?(::Tempfile)
33
+ end
34
+ end
35
+ end
@@ -1,38 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Paperclip::Thumbnail do
4
- context "A Paperclip Tempfile" do
5
- before do
6
- @tempfile = Paperclip::Tempfile.new(["file", ".jpg"])
7
- end
8
-
9
- after { @tempfile.close }
10
-
11
- it "has its path contain a real extension" do
12
- assert_equal ".jpg", File.extname(@tempfile.path)
13
- end
14
-
15
- it "is a real Tempfile" do
16
- assert @tempfile.is_a?(::Tempfile)
17
- end
18
- end
19
-
20
- context "Another Paperclip Tempfile" do
21
- before do
22
- @tempfile = Paperclip::Tempfile.new("file")
23
- end
24
-
25
- after { @tempfile.close }
26
-
27
- it "does not have an extension if not given one" do
28
- assert_equal "", File.extname(@tempfile.path)
29
- end
30
-
31
- it "is a real Tempfile" do
32
- assert @tempfile.is_a?(::Tempfile)
33
- end
34
- end
35
-
36
4
  context "An image" do
37
5
  before do
38
6
  @file = File.new(fixture_file("5k.png"), 'rb')
@@ -480,5 +448,56 @@ describe Paperclip::Thumbnail do
480
448
  assert_equal "50x50", `#{cmd}`.chomp
481
449
  end
482
450
  end
451
+
452
+ context "with a specified frame_index" do
453
+ before do
454
+ @thumb = Paperclip::Thumbnail.new(
455
+ @file,
456
+ geometry: "50x50",
457
+ frame_index: 5,
458
+ format: :jpg,
459
+ )
460
+ end
461
+
462
+ it "creates the thumbnail from the frame index when sent #make" do
463
+ @thumb.make
464
+ assert_equal 5, @thumb.frame_index
465
+ end
466
+ end
467
+
468
+ context "with a specified frame_index out of bounds" do
469
+ before do
470
+ @thumb = Paperclip::Thumbnail.new(
471
+ @file,
472
+ geometry: "50x50",
473
+ frame_index: 20,
474
+ format: :jpg,
475
+ )
476
+ end
477
+
478
+ it "errors when trying to create the thumbnail" do
479
+ assert_raises(Paperclip::Error) do
480
+ silence_stream(STDERR) do
481
+ @thumb.make
482
+ end
483
+ end
484
+ end
485
+ end
486
+ end
487
+
488
+ context "with a really long file name" do
489
+ before do
490
+ tempfile = Tempfile.new("f")
491
+ tempfile_additional_chars = tempfile.path.split("/")[-1].length + 15
492
+ image_file = File.new(fixture_file("5k.png"), "rb")
493
+ @file = Tempfile.new("f" * (255 - tempfile_additional_chars))
494
+ @file.write(image_file.read)
495
+ @file.rewind
496
+ end
497
+
498
+ it "does not throw Errno::ENAMETOOLONG" do
499
+ thumb = Paperclip::Thumbnail.new(@file, geometry: "50x50", format: :gif)
500
+ expect { thumb.make }.to_not raise_error
501
+ end
483
502
  end
484
503
  end
@@ -4,11 +4,10 @@ require 'spec_helper'
4
4
  describe Paperclip::UrlGenerator do
5
5
  it "uses the given interpolator" do
6
6
  expected = "the expected result"
7
- mock_attachment = MockAttachment.new
8
7
  mock_interpolator = MockInterpolator.new(result: expected)
8
+ mock_attachment = MockAttachment.new(interpolator: mock_interpolator)
9
9
 
10
- url_generator = Paperclip::UrlGenerator.new(mock_attachment,
11
- { interpolator: mock_interpolator })
10
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
12
11
  result = url_generator.for(:style_name, {})
13
12
 
14
13
  assert_equal expected, result
@@ -17,12 +16,12 @@ describe Paperclip::UrlGenerator do
17
16
  end
18
17
 
19
18
  it "uses the default URL when no file is assigned" do
20
- mock_attachment = MockAttachment.new
21
19
  mock_interpolator = MockInterpolator.new
22
20
  default_url = "the default url"
23
- options = { interpolator: mock_interpolator, default_url: default_url}
21
+ options = { interpolator: mock_interpolator, default_url: default_url }
22
+ mock_attachment = MockAttachment.new(options)
24
23
 
25
- url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
24
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
26
25
  url_generator.for(:style_name, {})
27
26
 
28
27
  assert mock_interpolator.has_interpolated_pattern?(default_url),
@@ -30,12 +29,12 @@ describe Paperclip::UrlGenerator do
30
29
  end
31
30
 
32
31
  it "executes the default URL lambda when no file is assigned" do
33
- mock_attachment = MockAttachment.new
34
32
  mock_interpolator = MockInterpolator.new
35
33
  default_url = lambda {|attachment| "the #{attachment.class.name} default url" }
36
34
  options = { interpolator: mock_interpolator, default_url: default_url}
35
+ mock_attachment = MockAttachment.new(options)
37
36
 
38
- url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
37
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
39
38
  url_generator.for(:style_name, {})
40
39
 
41
40
  assert mock_interpolator.has_interpolated_pattern?("the MockAttachment default url"),
@@ -43,13 +42,17 @@ describe Paperclip::UrlGenerator do
43
42
  end
44
43
 
45
44
  it "executes the method named by the symbol as the default URL when no file is assigned" do
46
- mock_model = MockModel.new
47
- mock_attachment = MockAttachment.new(model: mock_model)
48
- mock_interpolator = MockInterpolator.new
45
+ mock_model = FakeModel.new
49
46
  default_url = :to_s
50
- options = { interpolator: mock_interpolator, default_url: default_url}
51
-
52
- url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
47
+ mock_interpolator = MockInterpolator.new
48
+ options = {
49
+ interpolator: mock_interpolator,
50
+ default_url: default_url,
51
+ model: mock_model,
52
+ }
53
+ mock_attachment = MockAttachment.new(options)
54
+
55
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
53
56
  url_generator.for(:style_name, {})
54
57
 
55
58
  assert mock_interpolator.has_interpolated_pattern?(mock_model.to_s),
@@ -58,10 +61,10 @@ describe Paperclip::UrlGenerator do
58
61
 
59
62
  it "URL-escapes spaces if asked to" do
60
63
  expected = "the expected result"
61
- mock_attachment = MockAttachment.new
62
64
  mock_interpolator = MockInterpolator.new(result: expected)
63
65
  options = { interpolator: mock_interpolator }
64
- url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
66
+ mock_attachment = MockAttachment.new(options)
67
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
65
68
 
66
69
  result = url_generator.for(:style_name, {escape: true})
67
70
 
@@ -74,10 +77,10 @@ describe Paperclip::UrlGenerator do
74
77
  "the escaped result"
75
78
  end
76
79
  end.new
77
- mock_attachment = MockAttachment.new
78
80
  mock_interpolator = MockInterpolator.new(result: expected)
79
81
  options = { interpolator: mock_interpolator }
80
- url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
82
+ mock_attachment = MockAttachment.new(options)
83
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
81
84
 
82
85
  result = url_generator.for(:style_name, {escape: true})
83
86
 
@@ -86,10 +89,10 @@ describe Paperclip::UrlGenerator do
86
89
 
87
90
  it "leaves spaces unescaped as asked to" do
88
91
  expected = "the expected result"
89
- mock_attachment = MockAttachment.new
90
92
  mock_interpolator = MockInterpolator.new(result: expected)
91
93
  options = { interpolator: mock_interpolator }
92
- url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
94
+ mock_attachment = MockAttachment.new(options)
95
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
93
96
 
94
97
  result = url_generator.for(:style_name, {escape: false})
95
98
 
@@ -98,10 +101,10 @@ describe Paperclip::UrlGenerator do
98
101
 
99
102
  it "defaults to leaving spaces unescaped" do
100
103
  expected = "the expected result"
101
- mock_attachment = MockAttachment.new
102
104
  mock_interpolator = MockInterpolator.new(result: expected)
103
105
  options = { interpolator: mock_interpolator }
104
- url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
106
+ mock_attachment = MockAttachment.new(options)
107
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
105
108
 
106
109
  result = url_generator.for(:style_name, {})
107
110
 
@@ -111,9 +114,9 @@ describe Paperclip::UrlGenerator do
111
114
  it "produces URLs without the updated_at value when the object does not respond to updated_at" do
112
115
  expected = "the expected result"
113
116
  mock_interpolator = MockInterpolator.new(result: expected)
114
- mock_attachment = MockAttachment.new(responds_to_updated_at: false)
115
- options = { interpolator: mock_interpolator }
116
- url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
117
+ options = { interpolator: mock_interpolator, responds_to_updated_at: false }
118
+ mock_attachment = MockAttachment.new(options)
119
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
117
120
 
118
121
  result = url_generator.for(:style_name, {timestamp: true})
119
122
 
@@ -123,9 +126,13 @@ describe Paperclip::UrlGenerator do
123
126
  it "produces URLs without the updated_at value when the updated_at value is nil" do
124
127
  expected = "the expected result"
125
128
  mock_interpolator = MockInterpolator.new(result: expected)
126
- mock_attachment = MockAttachment.new(responds_to_updated_at: true, updated_at: nil)
127
- options = { interpolator: mock_interpolator }
128
- url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
129
+ options = {
130
+ responds_to_updated_at: true,
131
+ updated_at: nil,
132
+ interpolator: mock_interpolator,
133
+ }
134
+ mock_attachment = MockAttachment.new(options)
135
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
129
136
 
130
137
  result = url_generator.for(:style_name, {timestamp: true})
131
138
 
@@ -136,9 +143,9 @@ describe Paperclip::UrlGenerator do
136
143
  expected = "the expected result"
137
144
  updated_at = 1231231234
138
145
  mock_interpolator = MockInterpolator.new(result: expected)
139
- mock_attachment = MockAttachment.new(updated_at: updated_at)
140
- options = { interpolator: mock_interpolator }
141
- url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
146
+ options = { interpolator: mock_interpolator, updated_at: updated_at }
147
+ mock_attachment = MockAttachment.new(options)
148
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
142
149
 
143
150
  result = url_generator.for(:style_name, {timestamp: true})
144
151
 
@@ -149,9 +156,9 @@ describe Paperclip::UrlGenerator do
149
156
  expected = "the?expected=result"
150
157
  updated_at = 1231231234
151
158
  mock_interpolator = MockInterpolator.new(result: expected)
152
- mock_attachment = MockAttachment.new(updated_at: updated_at)
153
- options = { interpolator: mock_interpolator }
154
- url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
159
+ options = { interpolator: mock_interpolator, updated_at: updated_at }
160
+ mock_attachment = MockAttachment.new(options)
161
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
155
162
 
156
163
  result = url_generator.for(:style_name, {timestamp: true})
157
164
 
@@ -162,9 +169,9 @@ describe Paperclip::UrlGenerator do
162
169
  expected = "the expected result"
163
170
  updated_at = 1231231234
164
171
  mock_interpolator = MockInterpolator.new(result: expected)
165
- mock_attachment = MockAttachment.new(updated_at: updated_at)
166
- options = { interpolator: mock_interpolator }
167
- url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
172
+ options = { interpolator: mock_interpolator, updated_at: updated_at }
173
+ mock_attachment = MockAttachment.new(options)
174
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
168
175
 
169
176
  result = url_generator.for(:style_name, {timestamp: false})
170
177
 
@@ -173,11 +180,15 @@ describe Paperclip::UrlGenerator do
173
180
 
174
181
  it "produces the correct URL when the instance has a file name" do
175
182
  expected = "the expected result"
176
- mock_attachment = MockAttachment.new(original_filename: 'exists')
177
183
  mock_interpolator = MockInterpolator.new
178
- options = { interpolator: mock_interpolator, url: expected}
179
-
180
- url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
184
+ options = {
185
+ interpolator: mock_interpolator,
186
+ url: expected,
187
+ original_filename: "exists",
188
+ }
189
+ mock_attachment = MockAttachment.new(options)
190
+
191
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
181
192
  url_generator.for(:style_name, {})
182
193
 
183
194
  assert mock_interpolator.has_interpolated_pattern?(expected),
@@ -186,10 +197,10 @@ describe Paperclip::UrlGenerator do
186
197
 
187
198
  describe "should be able to escape (, ), [, and ]." do
188
199
  def generate(expected, updated_at=nil)
189
- mock_attachment = MockAttachment.new(updated_at: updated_at)
190
200
  mock_interpolator = MockInterpolator.new(result: expected)
191
- options = { interpolator: mock_interpolator }
192
- url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
201
+ options = { interpolator: mock_interpolator, updated_at: updated_at }
202
+ mock_attachment = MockAttachment.new(options)
203
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
193
204
  def url_generator.respond_to(params)
194
205
  false if params == :escape
195
206
  end
@@ -12,14 +12,6 @@ describe Paperclip::Validators::AttachmentSizeValidator do
12
12
  ))
13
13
  end
14
14
 
15
- def self.storage_units
16
- if defined?(ActiveSupport::NumberHelper) # Rails 4.0+
17
- { 5120 => '5 KB', 10240 => '10 KB' }
18
- else
19
- { 5120 => '5120 Bytes', 10240 => '10240 Bytes' }
20
- end
21
- end
22
-
23
15
  def self.should_allow_attachment_file_size(size)
24
16
  context "when the attachment size is #{size}" do
25
17
  it "adds error to dummy object" do
@@ -158,8 +150,10 @@ describe Paperclip::Validators::AttachmentSizeValidator do
158
150
  message: "is invalid. (Between %{min} and %{max} please.)"
159
151
  end
160
152
 
161
- should_not_allow_attachment_file_size 11.kilobytes,
162
- message: "is invalid. (Between #{storage_units[5120]} and #{storage_units[10240]} please.)"
153
+ should_not_allow_attachment_file_size(
154
+ 11.kilobytes,
155
+ message: "is invalid. (Between 5 KB and 10 KB please.)"
156
+ )
163
157
  end
164
158
 
165
159
  context "given :less_than and :greater_than" do
@@ -169,8 +163,10 @@ describe Paperclip::Validators::AttachmentSizeValidator do
169
163
  message: "is invalid. (Between %{min} and %{max} please.)"
170
164
  end
171
165
 
172
- should_not_allow_attachment_file_size 11.kilobytes,
173
- message: "is invalid. (Between #{storage_units[5120]} and #{storage_units[10240]} please.)"
166
+ should_not_allow_attachment_file_size(
167
+ 11.kilobytes,
168
+ message: "is invalid. (Between 5 KB and 10 KB please.)"
169
+ )
174
170
  end
175
171
  end
176
172
 
@@ -181,10 +177,15 @@ describe Paperclip::Validators::AttachmentSizeValidator do
181
177
  less_than: 10.kilobytes
182
178
  end
183
179
 
184
- should_not_allow_attachment_file_size 11.kilobytes,
185
- message: "must be less than #{storage_units[10240]}"
186
- should_not_allow_attachment_file_size 4.kilobytes,
187
- message: "must be greater than #{storage_units[5120]}"
180
+ should_not_allow_attachment_file_size(
181
+ 11.kilobytes,
182
+ message: "must be less than 10 KB"
183
+ )
184
+
185
+ should_not_allow_attachment_file_size(
186
+ 4.kilobytes,
187
+ message: "must be greater than 5 KB"
188
+ )
188
189
  end
189
190
 
190
191
  context "given a size range" do
@@ -192,10 +193,15 @@ describe Paperclip::Validators::AttachmentSizeValidator do
192
193
  build_validator in: (5.kilobytes..10.kilobytes)
193
194
  end
194
195
 
195
- should_not_allow_attachment_file_size 11.kilobytes,
196
- message: "must be in between #{storage_units[5120]} and #{storage_units[10240]}"
197
- should_not_allow_attachment_file_size 4.kilobytes,
198
- message: "must be in between #{storage_units[5120]} and #{storage_units[10240]}"
196
+ should_not_allow_attachment_file_size(
197
+ 11.kilobytes,
198
+ message: "must be in between 5 KB and 10 KB"
199
+ )
200
+
201
+ should_not_allow_attachment_file_size(
202
+ 4.kilobytes,
203
+ message: "must be in between 5 KB and 10 KB"
204
+ )
199
205
  end
200
206
  end
201
207
 
@@ -3,6 +3,7 @@ require 'spec_helper'
3
3
  describe Paperclip::Validators do
4
4
  context "using the helper" do
5
5
  before do
6
+ rebuild_class
6
7
  Dummy.validates_attachment :avatar, presence: true, content_type: { content_type: "image/jpeg" }, size: { in: 0..10240 }
7
8
  end
8
9
 
@@ -30,8 +31,8 @@ describe Paperclip::Validators do
30
31
  before do
31
32
  rebuild_class
32
33
  Dummy.validates_attachment :avatar, file_type_ignorance: true, file_name: [
33
- { matches: /\A.*\.jpe?g\Z/i, message: :invalid_extension },
34
- { matches: /\A.{,8}\..+\Z/i, message: [:too_long, count: 8] },
34
+ { matches: /\A.*\.jpe?g\z/i, message: :invalid_extension },
35
+ { matches: /\A.{,8}\..+\z/i, message: [:too_long, count: 8] },
35
36
  ]
36
37
  end
37
38
 
@@ -64,12 +65,11 @@ describe Paperclip::Validators do
64
65
  assert_raises(RuntimeError){ dummy.valid? }
65
66
  end
66
67
 
67
- it 'allows you to attach a file that does not violates these validations' do
68
+ it 'allows you to attach a file that does not violate these validations' do
68
69
  dummy = Dummy.new(avatar: File.new(fixture_file('rotated.jpg')))
69
- expect(dummy.errors.keys).to match_array []
70
+ expect(dummy.errors.full_messages).to be_empty
70
71
  assert dummy.valid?
71
72
  end
72
-
73
73
  end
74
74
 
75
75
  context "using the helper with a conditional" do
data/spec/spec_helper.rb CHANGED
@@ -7,6 +7,8 @@ require 'active_support/core_ext'
7
7
  require 'mocha/api'
8
8
  require 'bourne'
9
9
  require 'ostruct'
10
+ require 'pathname'
11
+ require 'activerecord-import'
10
12
 
11
13
  ROOT = Pathname(File.expand_path(File.join(File.dirname(__FILE__), '..')))
12
14
 
@@ -20,6 +22,10 @@ FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures")
20
22
  config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
21
23
  ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
22
24
  ActiveRecord::Base.establish_connection(config['test'])
25
+ if ActiveRecord::VERSION::STRING >= "4.2" &&
26
+ ActiveRecord::VERSION::STRING < "5.0"
27
+ ActiveRecord::Base.raise_in_transactional_callbacks = true
28
+ end
23
29
  Paperclip.options[:logger] = ActiveRecord::Base.logger
24
30
 
25
31
  Dir[File.join(ROOT, 'spec', 'support', '**', '*.rb')].each{|f| require f }
@@ -31,8 +37,9 @@ RSpec.configure do |config|
31
37
  config.include Assertions
32
38
  config.include ModelReconstruction
33
39
  config.include TestData
40
+ config.include Reporting
34
41
  config.extend VersionHelper
35
- config.extend RailsHelpers::ClassMethods
42
+ config.extend ConditionalFilterHelper
36
43
  config.mock_framework = :mocha
37
44
  config.before(:all) do
38
45
  rebuild_model
@@ -48,19 +48,30 @@ module Assertions
48
48
  end
49
49
 
50
50
  def assert_success_response(url)
51
+ url = "http:#{url}" unless url =~ /http/
51
52
  Net::HTTP.get_response(URI.parse(url)) do |response|
52
- assert_equal "200", response.code,
53
+ assert_equal "200",
54
+ response.code,
53
55
  "Expected HTTP response code 200, got #{response.code}"
54
56
  end
55
57
  end
56
58
 
57
59
  def assert_not_found_response(url)
60
+ url = "http:#{url}" unless url =~ /http/
58
61
  Net::HTTP.get_response(URI.parse(url)) do |response|
59
62
  assert_equal "404", response.code,
60
63
  "Expected HTTP response code 404, got #{response.code}"
61
64
  end
62
65
  end
63
66
 
67
+ def assert_forbidden_response(url)
68
+ url = "http:#{url}" unless url =~ /http/
69
+ Net::HTTP.get_response(URI.parse(url)) do |response|
70
+ assert_equal "403", response.code,
71
+ "Expected HTTP response code 403, got #{response.code}"
72
+ end
73
+ end
74
+
64
75
  def assert_frame_dimensions(range, frames)
65
76
  frames.each_with_index do |frame, frame_index|
66
77
  frame.split('x').each_with_index do |dimension, dimension_index |
@@ -0,0 +1,5 @@
1
+ module ConditionalFilterHelper
2
+ def aws_accelerate_available?
3
+ (Gem::Version.new(Aws::VERSION) >= Gem::Version.new("2.3.0"))
4
+ end
5
+ end
@@ -18,4 +18,8 @@ class FakeModel
18
18
  def valid?
19
19
  errors.empty?
20
20
  end
21
+
22
+ def new_record?
23
+ false
24
+ end
21
25
  end
Binary file
@@ -8,7 +8,16 @@ RSpec::Matchers.define :have_column do |column_name|
8
8
  column && column.default.to_s == @default.to_s
9
9
  end
10
10
 
11
- failure_message_for_should do |columns|
12
- "expected to find '#{column_name}', default '#{@default}' in #{columns.map{|column| [column.name, column.default] }}"
11
+ failure_message_method =
12
+ if RSpec::Version::STRING.to_i >= 3
13
+ :failure_message
14
+ else
15
+ :failure_message_for_should
16
+ end
17
+
18
+ send(failure_message_method) do |columns|
19
+ "expected to find '#{column_name}', " +
20
+ "default '#{@default}' " +
21
+ "in #{columns.map { |column| [column.name, column.default] }}"
13
22
  end
14
23
  end
@@ -1,7 +1,9 @@
1
1
  class MockAttachment
2
2
  attr_accessor :updated_at, :original_filename
3
+ attr_reader :options
3
4
 
4
5
  def initialize(options = {})
6
+ @options = options
5
7
  @model = options[:model]
6
8
  @responds_to_updated_at = options[:responds_to_updated_at]
7
9
  @updated_at = options[:updated_at]
@@ -2,9 +2,9 @@ class MockUrlGeneratorBuilder
2
2
  def initializer
3
3
  end
4
4
 
5
- def new(attachment, attachment_options)
5
+ def new(attachment)
6
6
  @attachment = attachment
7
- @attachment_options = attachment_options
7
+ @attachment_options = @attachment.options
8
8
  self
9
9
  end
10
10
 
@@ -10,7 +10,15 @@ module ModelReconstruction
10
10
 
11
11
  klass.reset_column_information
12
12
  klass.connection_pool.clear_table_cache!(klass.table_name) if klass.connection_pool.respond_to?(:clear_table_cache!)
13
- klass.connection.schema_cache.clear_table_cache!(klass.table_name) if klass.connection.respond_to?(:schema_cache)
13
+
14
+ if klass.connection.respond_to?(:schema_cache)
15
+ if ActiveRecord::VERSION::STRING >= "5.0"
16
+ klass.connection.schema_cache.clear_data_source_cache!(klass.table_name)
17
+ else
18
+ klass.connection.schema_cache.clear_table_cache!(klass.table_name)
19
+ end
20
+ end
21
+
14
22
  klass
15
23
  end
16
24
 
@@ -0,0 +1,11 @@
1
+ module Reporting
2
+ def silence_stream(stream)
3
+ old_stream = stream.dup
4
+ stream.reopen(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? 'NUL:' : '/dev/null')
5
+ stream.sync = true
6
+ yield
7
+ ensure
8
+ stream.reopen(old_stream)
9
+ old_stream.close
10
+ end
11
+ end