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::Style do
@@ -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')
@@ -80,7 +48,7 @@ describe Paperclip::Thumbnail do
80
48
  it "lets us know when a command isn't found versus a processing error" do
81
49
  old_path = ENV['PATH']
82
50
  begin
83
- Cocaine::CommandLine.path = ''
51
+ Terrapin::CommandLine.path = ''
84
52
  Paperclip.options[:command_path] = ''
85
53
  ENV['PATH'] = ''
86
54
  assert_raises(Paperclip::Errors::CommandNotFoundError) do
@@ -134,7 +102,7 @@ describe Paperclip::Thumbnail do
134
102
 
135
103
  output_file = thumb.make
136
104
 
137
- command = Cocaine::CommandLine.new("identify", "-format %wx%h :file")
105
+ command = Terrapin::CommandLine.new("identify", "-format %wx%h :file")
138
106
  assert_equal "50x50", command.run(file: output_file.path).strip
139
107
  end
140
108
 
@@ -211,17 +179,19 @@ describe Paperclip::Thumbnail do
211
179
  end
212
180
 
213
181
  it "errors when trying to create the thumbnail" do
214
- assert_raises(Paperclip::Error) do
215
- silence_stream(STDERR) do
182
+ silence_stream(STDERR) do
183
+ expect {
216
184
  @thumb.make
217
- end
185
+ }.to raise_error(
186
+ Paperclip::Error, /unrecognized option `-this-aint-no-option'/
187
+ )
218
188
  end
219
189
  end
220
190
 
221
191
  it "lets us know when a command isn't found versus a processing error" do
222
192
  old_path = ENV['PATH']
223
193
  begin
224
- Cocaine::CommandLine.path = ''
194
+ Terrapin::CommandLine.path = ''
225
195
  Paperclip.options[:command_path] = ''
226
196
  ENV['PATH'] = ''
227
197
  assert_raises(Paperclip::Errors::CommandNotFoundError) do
@@ -480,5 +450,56 @@ describe Paperclip::Thumbnail do
480
450
  assert_equal "50x50", `#{cmd}`.chomp
481
451
  end
482
452
  end
453
+
454
+ context "with a specified frame_index" do
455
+ before do
456
+ @thumb = Paperclip::Thumbnail.new(
457
+ @file,
458
+ geometry: "50x50",
459
+ frame_index: 5,
460
+ format: :jpg,
461
+ )
462
+ end
463
+
464
+ it "creates the thumbnail from the frame index when sent #make" do
465
+ @thumb.make
466
+ assert_equal 5, @thumb.frame_index
467
+ end
468
+ end
469
+
470
+ context "with a specified frame_index out of bounds" do
471
+ before do
472
+ @thumb = Paperclip::Thumbnail.new(
473
+ @file,
474
+ geometry: "50x50",
475
+ frame_index: 20,
476
+ format: :jpg,
477
+ )
478
+ end
479
+
480
+ it "errors when trying to create the thumbnail" do
481
+ assert_raises(Paperclip::Error) do
482
+ silence_stream(STDERR) do
483
+ @thumb.make
484
+ end
485
+ end
486
+ end
487
+ end
488
+ end
489
+
490
+ context "with a really long file name" do
491
+ before do
492
+ tempfile = Tempfile.new("f")
493
+ tempfile_additional_chars = tempfile.path.split("/")[-1].length + 15
494
+ image_file = File.new(fixture_file("5k.png"), "rb")
495
+ @file = Tempfile.new("f" * (255 - tempfile_additional_chars))
496
+ @file.write(image_file.read)
497
+ @file.rewind
498
+ end
499
+
500
+ it "does not throw Errno::ENAMETOOLONG" do
501
+ thumb = Paperclip::Thumbnail.new(@file, geometry: "50x50", format: :gif)
502
+ expect { thumb.make }.to_not raise_error
503
+ end
483
504
  end
484
505
  end
@@ -1,14 +1,12 @@
1
- # encoding: utf-8
2
1
  require 'spec_helper'
3
2
 
4
3
  describe Paperclip::UrlGenerator do
5
4
  it "uses the given interpolator" do
6
5
  expected = "the expected result"
7
- mock_attachment = MockAttachment.new
8
6
  mock_interpolator = MockInterpolator.new(result: expected)
7
+ mock_attachment = MockAttachment.new(interpolator: mock_interpolator)
9
8
 
10
- url_generator = Paperclip::UrlGenerator.new(mock_attachment,
11
- { interpolator: mock_interpolator })
9
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
12
10
  result = url_generator.for(:style_name, {})
13
11
 
14
12
  assert_equal expected, result
@@ -17,12 +15,12 @@ describe Paperclip::UrlGenerator do
17
15
  end
18
16
 
19
17
  it "uses the default URL when no file is assigned" do
20
- mock_attachment = MockAttachment.new
21
18
  mock_interpolator = MockInterpolator.new
22
19
  default_url = "the default url"
23
- options = { interpolator: mock_interpolator, default_url: default_url}
20
+ options = { interpolator: mock_interpolator, default_url: default_url }
21
+ mock_attachment = MockAttachment.new(options)
24
22
 
25
- url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
23
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
26
24
  url_generator.for(:style_name, {})
27
25
 
28
26
  assert mock_interpolator.has_interpolated_pattern?(default_url),
@@ -30,12 +28,12 @@ describe Paperclip::UrlGenerator do
30
28
  end
31
29
 
32
30
  it "executes the default URL lambda when no file is assigned" do
33
- mock_attachment = MockAttachment.new
34
31
  mock_interpolator = MockInterpolator.new
35
32
  default_url = lambda {|attachment| "the #{attachment.class.name} default url" }
36
33
  options = { interpolator: mock_interpolator, default_url: default_url}
34
+ mock_attachment = MockAttachment.new(options)
37
35
 
38
- url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
36
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
39
37
  url_generator.for(:style_name, {})
40
38
 
41
39
  assert mock_interpolator.has_interpolated_pattern?("the MockAttachment default url"),
@@ -43,13 +41,17 @@ describe Paperclip::UrlGenerator do
43
41
  end
44
42
 
45
43
  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
44
+ mock_model = FakeModel.new
49
45
  default_url = :to_s
50
- options = { interpolator: mock_interpolator, default_url: default_url}
51
-
52
- url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
46
+ mock_interpolator = MockInterpolator.new
47
+ options = {
48
+ interpolator: mock_interpolator,
49
+ default_url: default_url,
50
+ model: mock_model,
51
+ }
52
+ mock_attachment = MockAttachment.new(options)
53
+
54
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
53
55
  url_generator.for(:style_name, {})
54
56
 
55
57
  assert mock_interpolator.has_interpolated_pattern?(mock_model.to_s),
@@ -58,10 +60,10 @@ describe Paperclip::UrlGenerator do
58
60
 
59
61
  it "URL-escapes spaces if asked to" do
60
62
  expected = "the expected result"
61
- mock_attachment = MockAttachment.new
62
63
  mock_interpolator = MockInterpolator.new(result: expected)
63
64
  options = { interpolator: mock_interpolator }
64
- url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
65
+ mock_attachment = MockAttachment.new(options)
66
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
65
67
 
66
68
  result = url_generator.for(:style_name, {escape: true})
67
69
 
@@ -74,10 +76,10 @@ describe Paperclip::UrlGenerator do
74
76
  "the escaped result"
75
77
  end
76
78
  end.new
77
- mock_attachment = MockAttachment.new
78
79
  mock_interpolator = MockInterpolator.new(result: expected)
79
80
  options = { interpolator: mock_interpolator }
80
- url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
81
+ mock_attachment = MockAttachment.new(options)
82
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
81
83
 
82
84
  result = url_generator.for(:style_name, {escape: true})
83
85
 
@@ -86,10 +88,10 @@ describe Paperclip::UrlGenerator do
86
88
 
87
89
  it "leaves spaces unescaped as asked to" do
88
90
  expected = "the expected result"
89
- mock_attachment = MockAttachment.new
90
91
  mock_interpolator = MockInterpolator.new(result: expected)
91
92
  options = { interpolator: mock_interpolator }
92
- url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
93
+ mock_attachment = MockAttachment.new(options)
94
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
93
95
 
94
96
  result = url_generator.for(:style_name, {escape: false})
95
97
 
@@ -98,10 +100,10 @@ describe Paperclip::UrlGenerator do
98
100
 
99
101
  it "defaults to leaving spaces unescaped" do
100
102
  expected = "the expected result"
101
- mock_attachment = MockAttachment.new
102
103
  mock_interpolator = MockInterpolator.new(result: expected)
103
104
  options = { interpolator: mock_interpolator }
104
- url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
105
+ mock_attachment = MockAttachment.new(options)
106
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
105
107
 
106
108
  result = url_generator.for(:style_name, {})
107
109
 
@@ -111,9 +113,9 @@ describe Paperclip::UrlGenerator do
111
113
  it "produces URLs without the updated_at value when the object does not respond to updated_at" do
112
114
  expected = "the expected result"
113
115
  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)
116
+ options = { interpolator: mock_interpolator, responds_to_updated_at: false }
117
+ mock_attachment = MockAttachment.new(options)
118
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
117
119
 
118
120
  result = url_generator.for(:style_name, {timestamp: true})
119
121
 
@@ -123,9 +125,13 @@ describe Paperclip::UrlGenerator do
123
125
  it "produces URLs without the updated_at value when the updated_at value is nil" do
124
126
  expected = "the expected result"
125
127
  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)
128
+ options = {
129
+ responds_to_updated_at: true,
130
+ updated_at: nil,
131
+ interpolator: mock_interpolator,
132
+ }
133
+ mock_attachment = MockAttachment.new(options)
134
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
129
135
 
130
136
  result = url_generator.for(:style_name, {timestamp: true})
131
137
 
@@ -136,9 +142,9 @@ describe Paperclip::UrlGenerator do
136
142
  expected = "the expected result"
137
143
  updated_at = 1231231234
138
144
  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)
145
+ options = { interpolator: mock_interpolator, updated_at: updated_at }
146
+ mock_attachment = MockAttachment.new(options)
147
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
142
148
 
143
149
  result = url_generator.for(:style_name, {timestamp: true})
144
150
 
@@ -149,9 +155,9 @@ describe Paperclip::UrlGenerator do
149
155
  expected = "the?expected=result"
150
156
  updated_at = 1231231234
151
157
  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)
158
+ options = { interpolator: mock_interpolator, updated_at: updated_at }
159
+ mock_attachment = MockAttachment.new(options)
160
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
155
161
 
156
162
  result = url_generator.for(:style_name, {timestamp: true})
157
163
 
@@ -162,9 +168,9 @@ describe Paperclip::UrlGenerator do
162
168
  expected = "the expected result"
163
169
  updated_at = 1231231234
164
170
  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)
171
+ options = { interpolator: mock_interpolator, updated_at: updated_at }
172
+ mock_attachment = MockAttachment.new(options)
173
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
168
174
 
169
175
  result = url_generator.for(:style_name, {timestamp: false})
170
176
 
@@ -173,11 +179,15 @@ describe Paperclip::UrlGenerator do
173
179
 
174
180
  it "produces the correct URL when the instance has a file name" do
175
181
  expected = "the expected result"
176
- mock_attachment = MockAttachment.new(original_filename: 'exists')
177
182
  mock_interpolator = MockInterpolator.new
178
- options = { interpolator: mock_interpolator, url: expected}
179
-
180
- url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
183
+ options = {
184
+ interpolator: mock_interpolator,
185
+ url: expected,
186
+ original_filename: "exists",
187
+ }
188
+ mock_attachment = MockAttachment.new(options)
189
+
190
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
181
191
  url_generator.for(:style_name, {})
182
192
 
183
193
  assert mock_interpolator.has_interpolated_pattern?(expected),
@@ -186,10 +196,10 @@ describe Paperclip::UrlGenerator do
186
196
 
187
197
  describe "should be able to escape (, ), [, and ]." do
188
198
  def generate(expected, updated_at=nil)
189
- mock_attachment = MockAttachment.new(updated_at: updated_at)
190
199
  mock_interpolator = MockInterpolator.new(result: expected)
191
- options = { interpolator: mock_interpolator }
192
- url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
200
+ options = { interpolator: mock_interpolator, updated_at: updated_at }
201
+ mock_attachment = MockAttachment.new(options)
202
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
193
203
  def url_generator.respond_to(params)
194
204
  false if params == :escape
195
205
  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,8 @@ 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
36
42
  config.mock_framework = :mocha
37
43
  config.before(:all) do
38
44
  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 |
@@ -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
 
@@ -19,7 +27,7 @@ module ModelReconstruction
19
27
  ActiveRecord::Base.connection.create_table :dummies, {force: true}, &block
20
28
  end
21
29
 
22
- def modify_table table_name, &block
30
+ def modify_table &block
23
31
  ActiveRecord::Base.connection.change_table :dummies, &block
24
32
  end
25
33
 
@@ -29,7 +37,7 @@ module ModelReconstruction
29
37
  table.column :other, :string
30
38
  table.column :avatar_file_name, :string
31
39
  table.column :avatar_content_type, :string
32
- table.column :avatar_file_size, :integer
40
+ table.column :avatar_file_size, :bigint
33
41
  table.column :avatar_updated_at, :datetime
34
42
  table.column :avatar_fingerprint, :string
35
43
  end