paperclip 3.5.4 → 5.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (212) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +17 -0
  3. data/.gitignore +0 -6
  4. data/.hound.yml +1055 -0
  5. data/.rubocop.yml +1 -0
  6. data/.travis.yml +17 -20
  7. data/Appraisals +4 -16
  8. data/CONTRIBUTING.md +29 -13
  9. data/Gemfile +11 -3
  10. data/LICENSE +1 -3
  11. data/NEWS +241 -49
  12. data/README.md +471 -166
  13. data/RELEASING.md +17 -0
  14. data/Rakefile +6 -8
  15. data/UPGRADING +12 -9
  16. data/features/basic_integration.feature +27 -8
  17. data/features/migration.feature +0 -24
  18. data/features/step_definitions/attachment_steps.rb +36 -28
  19. data/features/step_definitions/html_steps.rb +2 -2
  20. data/features/step_definitions/rails_steps.rb +68 -37
  21. data/features/step_definitions/s3_steps.rb +2 -2
  22. data/features/step_definitions/web_steps.rb +1 -103
  23. data/features/support/env.rb +3 -2
  24. data/features/support/file_helpers.rb +2 -2
  25. data/features/support/fixtures/gemfile.txt +1 -1
  26. data/features/support/paths.rb +1 -1
  27. data/features/support/rails.rb +2 -25
  28. data/gemfiles/4.2.gemfile +17 -0
  29. data/gemfiles/5.0.gemfile +17 -0
  30. data/lib/generators/paperclip/paperclip_generator.rb +9 -3
  31. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +2 -2
  32. data/lib/paperclip/attachment.rb +151 -46
  33. data/lib/paperclip/attachment_registry.rb +3 -2
  34. data/lib/paperclip/callbacks.rb +13 -1
  35. data/lib/paperclip/content_type_detector.rb +26 -22
  36. data/lib/paperclip/errors.rb +8 -1
  37. data/lib/paperclip/file_command_content_type_detector.rb +6 -8
  38. data/lib/paperclip/geometry_detector_factory.rb +5 -3
  39. data/lib/paperclip/geometry_parser_factory.rb +1 -1
  40. data/lib/paperclip/glue.rb +1 -1
  41. data/lib/paperclip/has_attached_file.rb +17 -1
  42. data/lib/paperclip/helpers.rb +15 -11
  43. data/lib/paperclip/interpolations/plural_cache.rb +6 -5
  44. data/lib/paperclip/interpolations.rb +26 -13
  45. data/lib/paperclip/io_adapters/abstract_adapter.rb +32 -4
  46. data/lib/paperclip/io_adapters/attachment_adapter.rb +13 -8
  47. data/lib/paperclip/io_adapters/data_uri_adapter.rb +11 -16
  48. data/lib/paperclip/io_adapters/empty_string_adapter.rb +5 -4
  49. data/lib/paperclip/io_adapters/file_adapter.rb +12 -6
  50. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +7 -7
  51. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
  52. data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
  53. data/lib/paperclip/io_adapters/registry.rb +6 -2
  54. data/lib/paperclip/io_adapters/stringio_adapter.rb +15 -16
  55. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +10 -6
  56. data/lib/paperclip/io_adapters/uri_adapter.rb +41 -19
  57. data/lib/paperclip/locales/en.yml +1 -0
  58. data/lib/paperclip/matchers/have_attached_file_matcher.rb +2 -1
  59. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
  60. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +2 -1
  61. data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +2 -1
  62. data/lib/paperclip/media_type_spoof_detector.rb +90 -0
  63. data/lib/paperclip/processor.rb +5 -41
  64. data/lib/paperclip/processor_helpers.rb +50 -0
  65. data/lib/paperclip/rails_environment.rb +25 -0
  66. data/lib/paperclip/schema.rb +9 -7
  67. data/lib/paperclip/storage/filesystem.rb +14 -3
  68. data/lib/paperclip/storage/fog.rb +37 -19
  69. data/lib/paperclip/storage/s3.rb +129 -69
  70. data/lib/paperclip/style.rb +8 -2
  71. data/lib/paperclip/tempfile_factory.rb +5 -1
  72. data/lib/paperclip/thumbnail.rb +30 -18
  73. data/lib/paperclip/url_generator.rb +26 -14
  74. data/lib/paperclip/validators/attachment_content_type_validator.rb +4 -0
  75. data/lib/paperclip/validators/attachment_file_name_validator.rb +80 -0
  76. data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +29 -0
  77. data/lib/paperclip/validators/attachment_presence_validator.rb +4 -0
  78. data/lib/paperclip/validators/attachment_size_validator.rb +5 -3
  79. data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +27 -0
  80. data/lib/paperclip/validators.rb +11 -4
  81. data/lib/paperclip/version.rb +3 -1
  82. data/lib/paperclip.rb +30 -11
  83. data/lib/tasks/paperclip.rake +34 -5
  84. data/paperclip.gemspec +21 -16
  85. data/shoulda_macros/paperclip.rb +0 -1
  86. data/spec/paperclip/attachment_definitions_spec.rb +13 -0
  87. data/{test/attachment_processing_test.rb → spec/paperclip/attachment_processing_spec.rb} +17 -20
  88. data/spec/paperclip/attachment_registry_spec.rb +158 -0
  89. data/{test/attachment_test.rb → spec/paperclip/attachment_spec.rb} +502 -407
  90. data/{test/content_type_detector_test.rb → spec/paperclip/content_type_detector_spec.rb} +17 -20
  91. data/spec/paperclip/file_command_content_type_detector_spec.rb +40 -0
  92. data/spec/paperclip/filename_cleaner_spec.rb +14 -0
  93. data/spec/paperclip/geometry_detector_spec.rb +39 -0
  94. data/{test/geometry_parser_test.rb → spec/paperclip/geometry_parser_spec.rb} +27 -27
  95. data/{test/geometry_test.rb → spec/paperclip/geometry_spec.rb} +50 -52
  96. data/spec/paperclip/glue_spec.rb +44 -0
  97. data/spec/paperclip/has_attached_file_spec.rb +158 -0
  98. data/{test/integration_test.rb → spec/paperclip/integration_spec.rb} +137 -128
  99. data/{test/interpolations_test.rb → spec/paperclip/interpolations_spec.rb} +70 -46
  100. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +132 -0
  101. data/{test/io_adapters/attachment_adapter_test.rb → spec/paperclip/io_adapters/attachment_adapter_spec.rb} +33 -32
  102. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +89 -0
  103. data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +17 -0
  104. data/{test/io_adapters/file_adapter_test.rb → spec/paperclip/io_adapters/file_adapter_spec.rb} +38 -42
  105. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +121 -0
  106. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +8 -0
  107. data/{test/io_adapters/nil_adapter_test.rb → spec/paperclip/io_adapters/nil_adapter_spec.rb} +7 -7
  108. data/{test/io_adapters/registry_test.rb → spec/paperclip/io_adapters/registry_spec.rb} +12 -9
  109. data/{test/io_adapters/stringio_adapter_test.rb → spec/paperclip/io_adapters/stringio_adapter_spec.rb} +21 -18
  110. data/{test/io_adapters/uploaded_file_adapter_test.rb → spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb} +46 -46
  111. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +172 -0
  112. data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +19 -0
  113. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +109 -0
  114. data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +69 -0
  115. data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +88 -0
  116. data/spec/paperclip/media_type_spoof_detector_spec.rb +94 -0
  117. data/spec/paperclip/meta_class_spec.rb +30 -0
  118. data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +84 -0
  119. data/spec/paperclip/paperclip_spec.rb +192 -0
  120. data/spec/paperclip/plural_cache_spec.rb +37 -0
  121. data/spec/paperclip/processor_helpers_spec.rb +57 -0
  122. data/{test/processor_test.rb → spec/paperclip/processor_spec.rb} +7 -7
  123. data/spec/paperclip/rails_environment_spec.rb +33 -0
  124. data/{test/rake_test.rb → spec/paperclip/rake_spec.rb} +15 -15
  125. data/spec/paperclip/schema_spec.rb +248 -0
  126. data/{test/storage/filesystem_test.rb → spec/paperclip/storage/filesystem_spec.rb} +18 -18
  127. data/spec/paperclip/storage/fog_spec.rb +566 -0
  128. data/spec/paperclip/storage/s3_live_spec.rb +188 -0
  129. data/spec/paperclip/storage/s3_spec.rb +1693 -0
  130. data/spec/paperclip/style_spec.rb +255 -0
  131. data/spec/paperclip/tempfile_factory_spec.rb +33 -0
  132. data/spec/paperclip/tempfile_spec.rb +35 -0
  133. data/{test/thumbnail_test.rb → spec/paperclip/thumbnail_spec.rb} +153 -134
  134. data/spec/paperclip/url_generator_spec.rb +222 -0
  135. data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +322 -0
  136. data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +160 -0
  137. data/{test/validators/attachment_presence_validator_test.rb → spec/paperclip/validators/attachment_presence_validator_spec.rb} +20 -20
  138. data/{test/validators/attachment_size_validator_test.rb → spec/paperclip/validators/attachment_size_validator_spec.rb} +77 -64
  139. data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +52 -0
  140. data/spec/paperclip/validators_spec.rb +164 -0
  141. data/spec/spec_helper.rb +47 -0
  142. data/spec/support/assertions.rb +82 -0
  143. data/spec/support/conditional_filter_helper.rb +5 -0
  144. data/spec/support/fake_model.rb +25 -0
  145. data/spec/support/fake_rails.rb +12 -0
  146. data/spec/support/fixtures/empty.html +1 -0
  147. data/spec/support/fixtures/empty.xlsx +0 -0
  148. data/spec/support/fixtures/spaced file.jpg +0 -0
  149. data/spec/support/matchers/accept.rb +5 -0
  150. data/spec/support/matchers/exist.rb +5 -0
  151. data/spec/support/matchers/have_column.rb +23 -0
  152. data/{test → spec}/support/mock_attachment.rb +2 -0
  153. data/{test → spec}/support/mock_url_generator_builder.rb +2 -2
  154. data/spec/support/model_reconstruction.rb +68 -0
  155. data/spec/support/reporting.rb +11 -0
  156. data/spec/support/test_data.rb +13 -0
  157. data/spec/support/version_helper.rb +9 -0
  158. metadata +348 -225
  159. data/RUNNING_TESTS.md +0 -4
  160. data/cucumber/paperclip_steps.rb +0 -6
  161. data/gemfiles/3.0.gemfile +0 -11
  162. data/gemfiles/3.1.gemfile +0 -11
  163. data/gemfiles/3.2.gemfile +0 -11
  164. data/gemfiles/4.0.gemfile +0 -11
  165. data/test/attachment_definitions_test.rb +0 -12
  166. data/test/attachment_registry_test.rb +0 -88
  167. data/test/file_command_content_type_detector_test.rb +0 -27
  168. data/test/filename_cleaner_test.rb +0 -14
  169. data/test/generator_test.rb +0 -84
  170. data/test/geometry_detector_test.rb +0 -24
  171. data/test/has_attached_file_test.rb +0 -125
  172. data/test/helper.rb +0 -232
  173. data/test/io_adapters/abstract_adapter_test.rb +0 -58
  174. data/test/io_adapters/data_uri_adapter_test.rb +0 -74
  175. data/test/io_adapters/empty_string_adapter_test.rb +0 -18
  176. data/test/io_adapters/http_url_proxy_adapter_test.rb +0 -102
  177. data/test/io_adapters/identity_adapter_test.rb +0 -8
  178. data/test/io_adapters/uri_adapter_test.rb +0 -102
  179. data/test/matchers/have_attached_file_matcher_test.rb +0 -24
  180. data/test/matchers/validate_attachment_content_type_matcher_test.rb +0 -110
  181. data/test/matchers/validate_attachment_presence_matcher_test.rb +0 -69
  182. data/test/matchers/validate_attachment_size_matcher_test.rb +0 -86
  183. data/test/meta_class_test.rb +0 -32
  184. data/test/paperclip_missing_attachment_styles_test.rb +0 -90
  185. data/test/paperclip_test.rb +0 -217
  186. data/test/plural_cache_test.rb +0 -36
  187. data/test/schema_test.rb +0 -200
  188. data/test/storage/fog_test.rb +0 -473
  189. data/test/storage/s3_live_test.rb +0 -179
  190. data/test/storage/s3_test.rb +0 -1356
  191. data/test/style_test.rb +0 -213
  192. data/test/support/mock_model.rb +0 -2
  193. data/test/tempfile_factory_test.rb +0 -17
  194. data/test/url_generator_test.rb +0 -187
  195. data/test/validators/attachment_content_type_validator_test.rb +0 -324
  196. data/test/validators_test.rb +0 -61
  197. /data/{test → spec}/database.yml +0 -0
  198. /data/{test → spec/support}/fixtures/12k.png +0 -0
  199. /data/{test → spec/support}/fixtures/50x50.png +0 -0
  200. /data/{test → spec/support}/fixtures/5k.png +0 -0
  201. /data/{test → spec/support}/fixtures/animated +0 -0
  202. /data/{test → spec/support}/fixtures/animated.gif +0 -0
  203. /data/{test → spec/support}/fixtures/animated.unknown +0 -0
  204. /data/{test → spec/support}/fixtures/bad.png +0 -0
  205. /data/{test → spec/support}/fixtures/fog.yml +0 -0
  206. /data/{test → spec/support}/fixtures/rotated.jpg +0 -0
  207. /data/{test → spec/support}/fixtures/s3.yml +0 -0
  208. /data/{test → spec/support}/fixtures/spaced file.png +0 -0
  209. /data/{test → spec/support}/fixtures/text.txt +0 -0
  210. /data/{test → spec/support}/fixtures/twopage.pdf +0 -0
  211. /data/{test → spec/support}/fixtures/uppercase.PNG +0 -0
  212. /data/{test → spec}/support/mock_interpolator.rb +0 -0
@@ -1,205 +1,204 @@
1
1
  # encoding: utf-8
2
- require './test/helper'
3
- require 'paperclip/attachment'
2
+ require 'spec_helper'
4
3
 
5
- class Dummy; end
4
+ describe Paperclip::Attachment do
6
5
 
7
- class AttachmentTest < Test::Unit::TestCase
8
-
9
- context "presence" do
10
- setup do
11
- rebuild_class
12
- @dummy = Dummy.new
13
- end
14
-
15
- context "when file not set" do
16
- should "not be present" do
17
- assert @dummy.avatar.blank?
18
- refute @dummy.avatar.present?
19
- end
20
- end
21
-
22
- context "when file set" do
23
- setup { @dummy.avatar = File.new(fixture_file("50x50.png"), "rb") }
6
+ it "is not present when file not set" do
7
+ rebuild_class
8
+ dummy = Dummy.new
9
+ expect(dummy.avatar).to be_blank
10
+ expect(dummy.avatar).to_not be_present
11
+ end
24
12
 
25
- should "be present" do
26
- refute @dummy.avatar.blank?
27
- assert @dummy.avatar.present?
28
- end
29
- end
13
+ it "is present when the file is set" do
14
+ rebuild_class
15
+ dummy = Dummy.new
16
+ dummy.avatar = File.new(fixture_file("50x50.png"), "rb")
17
+ expect(dummy.avatar).to_not be_blank
18
+ expect(dummy.avatar).to be_present
30
19
  end
31
20
 
32
- should "process :original style first" do
21
+ it "processes :original style first" do
33
22
  file = File.new(fixture_file("50x50.png"), 'rb')
34
- rebuild_class :styles => { :small => '100x>', :original => '42x42#' }
23
+ rebuild_class styles: { small: '100x>', original: '42x42#' }
35
24
  dummy = Dummy.new
36
25
  dummy.avatar = file
37
26
  dummy.save
38
27
 
39
28
  # :small avatar should be 42px wide (processed original), not 50px (preprocessed original)
40
- assert_equal `identify -format "%w" "#{dummy.avatar.path(:small)}"`.strip, "42"
29
+ expect(`identify -format "%w" "#{dummy.avatar.path(:small)}"`.strip).to eq "42"
41
30
 
42
31
  file.close
43
32
  end
44
33
 
45
- should "not delete styles that don't get reprocessed" do
34
+ it "does not delete styles that don't get reprocessed" do
46
35
  file = File.new(fixture_file("50x50.png"), 'rb')
47
- rebuild_class :styles => { :small => '100x>',
48
- :large => '500x>',
49
- :original => '42x42#' }
36
+ rebuild_class styles: {
37
+ small: "100x>",
38
+ large: "500x>",
39
+ original: "42x42#"
40
+ }
41
+
50
42
  dummy = Dummy.new
51
43
  dummy.avatar = file
52
44
  dummy.save
53
45
 
54
- assert_file_exists(dummy.avatar.path(:small))
55
- assert_file_exists(dummy.avatar.path(:large))
56
- assert_file_exists(dummy.avatar.path(:original))
46
+ expect(dummy.avatar.path(:small)).to exist
47
+ expect(dummy.avatar.path(:large)).to exist
48
+ expect(dummy.avatar.path(:original)).to exist
57
49
 
58
50
  dummy.avatar.reprocess!(:small)
59
51
 
60
- assert_file_exists(dummy.avatar.path(:small))
61
- assert_file_exists(dummy.avatar.path(:large))
62
- assert_file_exists(dummy.avatar.path(:original))
52
+ expect(dummy.avatar.path(:small)).to exist
53
+ expect(dummy.avatar.path(:large)).to exist
54
+ expect(dummy.avatar.path(:original)).to exist
63
55
  end
64
56
 
65
57
  context "having a not empty hash as a default option" do
66
- setup do
58
+ before do
67
59
  @old_default_options = Paperclip::Attachment.default_options.dup
68
- @new_default_options = { :convert_options => { :all => "-background white" } }
60
+ @new_default_options = { convert_options: { all: "-background white" } }
69
61
  Paperclip::Attachment.default_options.merge!(@new_default_options)
70
62
  end
71
63
 
72
- teardown do
64
+ after do
73
65
  Paperclip::Attachment.default_options.merge!(@old_default_options)
74
66
  end
75
67
 
76
- should "deep merge when it is overridden" do
77
- new_options = { :convert_options => { :thumb => "-thumbnailize" } }
68
+ it "deep merges when it is overridden" do
69
+ new_options = { convert_options: { thumb: "-thumbnailize" } }
78
70
  attachment = Paperclip::Attachment.new(:name, :instance, new_options)
79
71
 
80
- assert_equal Paperclip::Attachment.default_options.deep_merge(new_options),
81
- attachment.instance_variable_get("@options")
72
+ expect(Paperclip::Attachment.default_options.deep_merge(new_options)).to eq attachment.instance_variable_get("@options")
82
73
  end
83
74
  end
84
75
 
85
- should "handle a boolean second argument to #url" do
76
+ it "handles a boolean second argument to #url" do
86
77
  mock_url_generator_builder = MockUrlGeneratorBuilder.new
87
- attachment = Paperclip::Attachment.new(:name, :instance, :url_generator => mock_url_generator_builder)
78
+ attachment = Paperclip::Attachment.new(
79
+ :name,
80
+ FakeModel.new,
81
+ url_generator: mock_url_generator_builder
82
+ )
88
83
 
89
84
  attachment.url(:style_name, true)
90
- assert mock_url_generator_builder.has_generated_url_with_options?(:timestamp => true, :escape => true)
85
+ expect(mock_url_generator_builder.has_generated_url_with_options?(timestamp: true, escape: true)).to eq true
91
86
 
92
87
  attachment.url(:style_name, false)
93
- assert mock_url_generator_builder.has_generated_url_with_options?(:timestamp => false, :escape => true)
88
+ expect(mock_url_generator_builder.has_generated_url_with_options?(timestamp: false, escape: true)).to eq true
94
89
  end
95
90
 
96
- should "pass the style and options through to the URL generator on #url" do
91
+ it "passes the style and options through to the URL generator on #url" do
97
92
  mock_url_generator_builder = MockUrlGeneratorBuilder.new
98
- attachment = Paperclip::Attachment.new(:name, :instance, :url_generator => mock_url_generator_builder)
99
-
100
- attachment.url(:style_name, :options => :values)
101
- assert mock_url_generator_builder.has_generated_url_with_options?(:options => :values)
93
+ attachment = Paperclip::Attachment.new(
94
+ :name,
95
+ FakeModel.new,
96
+ url_generator: mock_url_generator_builder
97
+ )
98
+
99
+ attachment.url(:style_name, options: :values)
100
+ expect(mock_url_generator_builder.has_generated_url_with_options?(options: :values)).to eq true
102
101
  end
103
102
 
104
- should "pass default options through when #url is given one argument" do
103
+ it "passes default options through when #url is given one argument" do
105
104
  mock_url_generator_builder = MockUrlGeneratorBuilder.new
106
105
  attachment = Paperclip::Attachment.new(:name,
107
- :instance,
108
- :url_generator => mock_url_generator_builder,
109
- :use_timestamp => true)
106
+ FakeModel.new,
107
+ url_generator: mock_url_generator_builder,
108
+ use_timestamp: true)
110
109
 
111
110
  attachment.url(:style_name)
112
- assert mock_url_generator_builder.has_generated_url_with_options?(:escape => true, :timestamp => true)
111
+ assert mock_url_generator_builder.has_generated_url_with_options?(escape: true, timestamp: true)
113
112
  end
114
113
 
115
- should "pass default style and options through when #url is given no arguments" do
114
+ it "passes default style and options through when #url is given no arguments" do
116
115
  mock_url_generator_builder = MockUrlGeneratorBuilder.new
117
116
  attachment = Paperclip::Attachment.new(:name,
118
- :instance,
119
- :default_style => 'default style',
120
- :url_generator => mock_url_generator_builder,
121
- :use_timestamp => true)
117
+ FakeModel.new,
118
+ default_style: 'default style',
119
+ url_generator: mock_url_generator_builder,
120
+ use_timestamp: true)
122
121
 
123
122
  attachment.url
124
- assert mock_url_generator_builder.has_generated_url_with_options?(:escape => true, :timestamp => true)
123
+ assert mock_url_generator_builder.has_generated_url_with_options?(escape: true, timestamp: true)
125
124
  assert mock_url_generator_builder.has_generated_url_with_style_name?('default style')
126
125
  end
127
126
 
128
- should "pass the option :timestamp => true if :use_timestamp is true and :timestamp is not passed" do
127
+ it "passes the option timestamp: true if :use_timestamp is true and :timestamp is not passed" do
129
128
  mock_url_generator_builder = MockUrlGeneratorBuilder.new
130
129
  attachment = Paperclip::Attachment.new(:name,
131
- :instance,
132
- :url_generator => mock_url_generator_builder,
133
- :use_timestamp => true)
130
+ FakeModel.new,
131
+ url_generator: mock_url_generator_builder,
132
+ use_timestamp: true)
134
133
 
135
134
  attachment.url(:style_name)
136
- assert mock_url_generator_builder.has_generated_url_with_options?(:escape => true, :timestamp => true)
135
+ assert mock_url_generator_builder.has_generated_url_with_options?(escape: true, timestamp: true)
137
136
  end
138
137
 
139
- should "pass the option :timestamp => false if :use_timestamp is false and :timestamp is not passed" do
138
+ it "passes the option timestamp: false if :use_timestamp is false and :timestamp is not passed" do
140
139
  mock_url_generator_builder = MockUrlGeneratorBuilder.new
141
140
  attachment = Paperclip::Attachment.new(:name,
142
- :instance,
143
- :url_generator => mock_url_generator_builder,
144
- :use_timestamp => false)
141
+ FakeModel.new,
142
+ url_generator: mock_url_generator_builder,
143
+ use_timestamp: false)
145
144
 
146
145
  attachment.url(:style_name)
147
- assert mock_url_generator_builder.has_generated_url_with_options?(:escape => true, :timestamp => false)
146
+ assert mock_url_generator_builder.has_generated_url_with_options?(escape: true, timestamp: false)
148
147
  end
149
148
 
150
- should "not change the :timestamp if :timestamp is passed" do
149
+ it "does not change the :timestamp if :timestamp is passed" do
151
150
  mock_url_generator_builder = MockUrlGeneratorBuilder.new
152
151
  attachment = Paperclip::Attachment.new(:name,
153
- :instance,
154
- :url_generator => mock_url_generator_builder,
155
- :use_timestamp => false)
152
+ FakeModel.new,
153
+ url_generator: mock_url_generator_builder,
154
+ use_timestamp: false)
156
155
 
157
- attachment.url(:style_name, :timestamp => true)
158
- assert mock_url_generator_builder.has_generated_url_with_options?(:escape => true, :timestamp => true)
156
+ attachment.url(:style_name, timestamp: true)
157
+ assert mock_url_generator_builder.has_generated_url_with_options?(escape: true, timestamp: true)
159
158
  end
160
159
 
161
- should "render JSON as default style" do
160
+ it "renders JSON as default style" do
162
161
  mock_url_generator_builder = MockUrlGeneratorBuilder.new
163
162
  attachment = Paperclip::Attachment.new(:name,
164
- :instance,
165
- :default_style => 'default style',
166
- :url_generator => mock_url_generator_builder)
163
+ FakeModel.new,
164
+ default_style: 'default style',
165
+ url_generator: mock_url_generator_builder)
167
166
 
168
- attachment_json = attachment.as_json
167
+ attachment.as_json
169
168
  assert mock_url_generator_builder.has_generated_url_with_style_name?('default style')
170
169
  end
171
170
 
172
- should "pass the option :escape => true if :escape_url is true and :escape is not passed" do
171
+ it "passes the option escape: true if :escape_url is true and :escape is not passed" do
173
172
  mock_url_generator_builder = MockUrlGeneratorBuilder.new
174
173
  attachment = Paperclip::Attachment.new(:name,
175
- :instance,
176
- :url_generator => mock_url_generator_builder,
177
- :escape_url => true)
174
+ FakeModel.new,
175
+ url_generator: mock_url_generator_builder,
176
+ escape_url: true)
178
177
 
179
178
  attachment.url(:style_name)
180
- assert mock_url_generator_builder.has_generated_url_with_options?(:escape => true)
179
+ assert mock_url_generator_builder.has_generated_url_with_options?(escape: true)
181
180
  end
182
181
 
183
- should "pass the option :escape => false if :escape_url is false and :escape is not passed" do
182
+ it "passes the option escape: false if :escape_url is false and :escape is not passed" do
184
183
  mock_url_generator_builder = MockUrlGeneratorBuilder.new
185
184
  attachment = Paperclip::Attachment.new(:name,
186
- :instance,
187
- :url_generator => mock_url_generator_builder,
188
- :escape_url => false)
185
+ FakeModel.new,
186
+ url_generator: mock_url_generator_builder,
187
+ escape_url: false)
189
188
 
190
189
  attachment.url(:style_name)
191
- assert mock_url_generator_builder.has_generated_url_with_options?(:escape => false)
190
+ assert mock_url_generator_builder.has_generated_url_with_options?(escape: false)
192
191
  end
193
192
 
194
- should "return the path based on the url by default" do
195
- @attachment = attachment :url => "/:class/:id/:basename"
193
+ it "returns the path based on the url by default" do
194
+ @attachment = attachment url: "/:class/:id/:basename"
196
195
  @model = @attachment.instance
197
196
  @model.id = 1234
198
197
  @model.avatar_file_name = "fake.jpg"
199
198
  assert_equal "#{Rails.root}/public/fake_models/1234/fake", @attachment.path
200
199
  end
201
200
 
202
- should "default to a path that scales" do
201
+ it "defaults to a path that scales" do
203
202
  avatar_attachment = attachment
204
203
  model = avatar_attachment.instance
205
204
  model.id = 1234
@@ -208,7 +207,7 @@ class AttachmentTest < Test::Unit::TestCase
208
207
  assert_equal expected_path, avatar_attachment.path
209
208
  end
210
209
 
211
- should "render JSON as the URL to the attachment" do
210
+ it "renders JSON as the URL to the attachment" do
212
211
  avatar_attachment = attachment
213
212
  model = avatar_attachment.instance
214
213
  model.id = 1234
@@ -216,35 +215,33 @@ class AttachmentTest < Test::Unit::TestCase
216
215
  assert_equal attachment.url, attachment.as_json
217
216
  end
218
217
 
219
- should "render JSON from the model when requested by :methods" do
218
+ it "renders JSON from the model when requested by :methods" do
220
219
  rebuild_model
221
220
  dummy = Dummy.new
222
221
  dummy.id = 1234
223
222
  dummy.avatar_file_name = "fake.jpg"
223
+ dummy.stubs(:new_record?).returns(false)
224
224
  expected_string = '{"avatar":"/system/dummies/avatars/000/001/234/original/fake.jpg"}'
225
- if ActiveRecord::Base.include_root_in_json # This is true by default in Rails 3, and false in 4
226
- expected_string = %({"dummy":#{expected_string}})
227
- end
228
225
  # active_model pre-3.2 checks only by calling any? on it, thus it doesn't work if it is empty
229
- assert_equal expected_string, dummy.to_json(:only => [:dummy_key_for_old_active_model], :methods => [:avatar])
226
+ assert_equal expected_string, dummy.to_json(only: [:dummy_key_for_old_active_model], methods: [:avatar])
230
227
  end
231
228
 
232
229
  context "Attachment default_options" do
233
- setup do
230
+ before do
234
231
  rebuild_model
235
232
  @old_default_options = Paperclip::Attachment.default_options.dup
236
233
  @new_default_options = @old_default_options.merge({
237
- :path => "argle/bargle",
238
- :url => "fooferon",
239
- :default_url => "not here.png"
234
+ path: "argle/bargle",
235
+ url: "fooferon",
236
+ default_url: "not here.png"
240
237
  })
241
238
  end
242
239
 
243
- teardown do
240
+ after do
244
241
  Paperclip::Attachment.default_options.merge! @old_default_options
245
242
  end
246
243
 
247
- should "be overrideable" do
244
+ it "is overrideable" do
248
245
  Paperclip::Attachment.default_options.merge!(@new_default_options)
249
246
  @new_default_options.keys.each do |key|
250
247
  assert_equal @new_default_options[key],
@@ -253,41 +250,46 @@ class AttachmentTest < Test::Unit::TestCase
253
250
  end
254
251
 
255
252
  context "without an Attachment" do
256
- setup do
253
+ before do
254
+ rebuild_model default_url: "default.url"
257
255
  @dummy = Dummy.new
258
256
  end
259
257
 
260
- should "return false when asked exists?" do
258
+ it "returns false when asked exists?" do
261
259
  assert !@dummy.avatar.exists?
262
260
  end
261
+
262
+ it "#url returns the default_url" do
263
+ expect(@dummy.avatar.url).to eq "default.url"
264
+ end
263
265
  end
264
266
 
265
267
  context "on an Attachment" do
266
- setup do
268
+ before do
267
269
  @dummy = Dummy.new
268
270
  @attachment = @dummy.avatar
269
271
  end
270
272
 
271
273
  Paperclip::Attachment.default_options.keys.each do |key|
272
- should "be the default_options for #{key}" do
274
+ it "is the default_options for #{key}" do
273
275
  assert_equal @old_default_options[key],
274
276
  @attachment.instance_variable_get("@options")[key],
275
- key
277
+ key.to_s
276
278
  end
277
279
  end
278
280
 
279
281
  context "when redefined" do
280
- setup do
282
+ before do
281
283
  Paperclip::Attachment.default_options.merge!(@new_default_options)
282
284
  @dummy = Dummy.new
283
285
  @attachment = @dummy.avatar
284
286
  end
285
287
 
286
288
  Paperclip::Attachment.default_options.keys.each do |key|
287
- should "be the new default_options for #{key}" do
289
+ it "is the new default_options for #{key}" do
288
290
  assert_equal @new_default_options[key],
289
291
  @attachment.instance_variable_get("@options")[key],
290
- key
292
+ key.to_s
291
293
  end
292
294
  end
293
295
  end
@@ -295,23 +297,23 @@ class AttachmentTest < Test::Unit::TestCase
295
297
  end
296
298
 
297
299
  context "An attachment with similarly named interpolations" do
298
- setup do
299
- rebuild_model :path => ":id.omg/:id-bbq/:idwhat/:id_partition.wtf"
300
+ before do
301
+ rebuild_model path: ":id.omg/:id-bbq/:idwhat/:id_partition.wtf"
300
302
  @dummy = Dummy.new
301
303
  @dummy.stubs(:id).returns(1024)
302
304
  @file = File.new(fixture_file("5k.png"), 'rb')
303
305
  @dummy.avatar = @file
304
306
  end
305
307
 
306
- teardown { @file.close }
308
+ after { @file.close }
307
309
 
308
- should "make sure that they are interpolated correctly" do
310
+ it "makes sure that they are interpolated correctly" do
309
311
  assert_equal "1024.omg/1024-bbq/1024what/000/001/024.wtf", @dummy.avatar.path
310
312
  end
311
313
  end
312
314
 
313
315
  context "An attachment with :timestamp interpolations" do
314
- setup do
316
+ before do
315
317
  @file = StringIO.new("...")
316
318
  @zone = 'UTC'
317
319
  Time.stubs(:zone).returns(@zone)
@@ -320,78 +322,77 @@ class AttachmentTest < Test::Unit::TestCase
320
322
  end
321
323
 
322
324
  context "using default time zone" do
323
- setup do
324
- rebuild_model :path => ":timestamp", :use_default_time_zone => true
325
+ before do
326
+ rebuild_model path: ":timestamp", use_default_time_zone: true
325
327
  @dummy = Dummy.new
326
328
  @dummy.avatar = @file
327
329
  end
328
330
 
329
- should "return a time in the default zone" do
331
+ it "returns a time in the default zone" do
330
332
  assert_equal @dummy.avatar_updated_at.in_time_zone(@zone_default).to_s, @dummy.avatar.path
331
333
  end
332
334
  end
333
335
 
334
336
  context "using per-thread time zone" do
335
- setup do
336
- rebuild_model :path => ":timestamp", :use_default_time_zone => false
337
+ before do
338
+ rebuild_model path: ":timestamp", use_default_time_zone: false
337
339
  @dummy = Dummy.new
338
340
  @dummy.avatar = @file
339
341
  end
340
342
 
341
- should "return a time in the per-thread zone" do
343
+ it "returns a time in the per-thread zone" do
342
344
  assert_equal @dummy.avatar_updated_at.in_time_zone(@zone).to_s, @dummy.avatar.path
343
345
  end
344
346
  end
345
347
  end
346
348
 
347
349
  context "An attachment with :hash interpolations" do
348
- setup do
349
- @file = StringIO.new("...")
350
+ before do
351
+ @file = File.open(fixture_file("5k.png"))
352
+ end
353
+
354
+ after do
355
+ @file.close
350
356
  end
351
357
 
352
- should "raise if no secret is provided" do
353
- @attachment = attachment :path => ":hash"
358
+ it "raises if no secret is provided" do
359
+ rebuild_model path: ":hash"
360
+ @attachment = Dummy.new.avatar
354
361
  @attachment.assign @file
355
362
 
356
- assert_raise ArgumentError do
363
+ assert_raises ArgumentError do
357
364
  @attachment.path
358
365
  end
359
366
  end
360
367
 
361
368
  context "when secret is set" do
362
- setup do
363
- @attachment = attachment :path => ":hash", :hash_secret => "w00t"
364
- @attachment.stubs(:instance_read).with(:updated_at).returns(Time.at(1234567890))
365
- @attachment.stubs(:instance_read).with(:file_name).returns("bla.txt")
366
- @attachment.instance.id = 1234
369
+ before do
370
+ rebuild_model path: ":hash",
371
+ hash_secret: "w00t",
372
+ hash_data: ":class/:attachment/:style/:filename"
373
+ @attachment = Dummy.new.avatar
367
374
  @attachment.assign @file
368
375
  end
369
376
 
370
- should "interpolate the hash data" do
371
- @attachment.expects(:interpolate).with(@attachment.options[:hash_data],anything).returns("interpolated_stuff")
372
- @attachment.hash_key
377
+ it "results in the correct interpolation" do
378
+ assert_equal "dummies/avatars/original/5k.png",
379
+ @attachment.send(:interpolate, @attachment.options[:hash_data])
380
+ assert_equal "dummies/avatars/thumb/5k.png",
381
+ @attachment.send(:interpolate, @attachment.options[:hash_data], :thumb)
373
382
  end
374
383
 
375
- should "result in the correct interpolation" do
376
- assert_equal "fake_models/avatars/1234/original/1234567890", @attachment.send(:interpolate,@attachment.options[:hash_data])
377
- end
378
-
379
- should "result in a correct hash" do
380
- assert_equal "d22b617d1bf10016aa7d046d16427ae203f39fce", @attachment.path
381
- end
382
-
383
- should "generate a hash digest with the correct style" do
384
- OpenSSL::HMAC.expects(:hexdigest).with(anything, anything, "fake_models/avatars/1234/medium/1234567890")
385
- @attachment.path("medium")
384
+ it "results in a correct hash" do
385
+ assert_equal "0a59e9142bba11576de1d353d8747b1acad5ad34", @attachment.path
386
+ assert_equal "b39a062c1e62e85a6c785ed00cf3bebf5f850e2b", @attachment.path(:thumb)
386
387
  end
387
388
  end
388
389
  end
389
390
 
390
391
  context "An attachment with a :rails_env interpolation" do
391
- setup do
392
+ before do
392
393
  @rails_env = "blah"
393
394
  @id = 1024
394
- rebuild_model :path => ":rails_env/:id.png"
395
+ rebuild_model path: ":rails_env/:id.png"
395
396
  @dummy = Dummy.new
396
397
  @dummy.stubs(:id).returns(@id)
397
398
  @file = StringIO.new(".")
@@ -399,83 +400,84 @@ class AttachmentTest < Test::Unit::TestCase
399
400
  Rails.stubs(:env).returns(@rails_env)
400
401
  end
401
402
 
402
- should "return the proper path" do
403
+ it "returns the proper path" do
403
404
  assert_equal "#{@rails_env}/#{@id}.png", @dummy.avatar.path
404
405
  end
405
406
  end
406
407
 
407
408
  context "An attachment with a default style and an extension interpolation" do
408
- setup do
409
- @attachment = attachment :path => ":basename.:extension",
410
- :styles => { :default => ["100x100", :png] },
411
- :default_style => :default
412
- @file = StringIO.new("...")
413
- @file.stubs(:original_filename).returns("file.jpg")
409
+ before do
410
+ rebuild_model path: ":basename.:extension",
411
+ styles: { default: ["100x100", :jpg] },
412
+ default_style: :default
413
+ @attachment = Dummy.new.avatar
414
+ @file = File.open(fixture_file("5k.png"))
415
+ @file.stubs(:original_filename).returns("file.png")
414
416
  end
415
- should "return the right extension for the path" do
417
+ it "returns the right extension for the path" do
416
418
  @attachment.assign(@file)
417
- assert_equal "file.png", @attachment.path
419
+ assert_equal "file.jpg", @attachment.path
418
420
  end
419
421
  end
420
422
 
421
423
  context "An attachment with :convert_options" do
422
- setup do
423
- rebuild_model :styles => {
424
- :thumb => "100x100",
425
- :large => "400x400"
424
+ before do
425
+ rebuild_model styles: {
426
+ thumb: "100x100",
427
+ large: "400x400"
426
428
  },
427
- :convert_options => {
428
- :all => "-do_stuff",
429
- :thumb => "-thumbnailize"
429
+ convert_options: {
430
+ all: "-do_stuff",
431
+ thumb: "-thumbnailize"
430
432
  }
431
433
  @dummy = Dummy.new
432
434
  @dummy.avatar
433
435
  end
434
436
 
435
- should "report the correct options when sent #extra_options_for(:thumb)" do
437
+ it "reports the correct options when sent #extra_options_for(:thumb)" do
436
438
  assert_equal "-thumbnailize -do_stuff", @dummy.avatar.send(:extra_options_for, :thumb), @dummy.avatar.convert_options.inspect
437
439
  end
438
440
 
439
- should "report the correct options when sent #extra_options_for(:large)" do
441
+ it "reports the correct options when sent #extra_options_for(:large)" do
440
442
  assert_equal "-do_stuff", @dummy.avatar.send(:extra_options_for, :large)
441
443
  end
442
444
  end
443
445
 
444
446
  context "An attachment with :source_file_options" do
445
- setup do
446
- rebuild_model :styles => {
447
- :thumb => "100x100",
448
- :large => "400x400"
447
+ before do
448
+ rebuild_model styles: {
449
+ thumb: "100x100",
450
+ large: "400x400"
449
451
  },
450
- :source_file_options => {
451
- :all => "-density 400",
452
- :thumb => "-depth 8"
452
+ source_file_options: {
453
+ all: "-density 400",
454
+ thumb: "-depth 8"
453
455
  }
454
456
  @dummy = Dummy.new
455
457
  @dummy.avatar
456
458
  end
457
459
 
458
- should "report the correct options when sent #extra_source_file_options_for(:thumb)" do
460
+ it "reports the correct options when sent #extra_source_file_options_for(:thumb)" do
459
461
  assert_equal "-depth 8 -density 400", @dummy.avatar.send(:extra_source_file_options_for, :thumb), @dummy.avatar.source_file_options.inspect
460
462
  end
461
463
 
462
- should "report the correct options when sent #extra_source_file_options_for(:large)" do
464
+ it "reports the correct options when sent #extra_source_file_options_for(:large)" do
463
465
  assert_equal "-density 400", @dummy.avatar.send(:extra_source_file_options_for, :large)
464
466
  end
465
467
  end
466
468
 
467
469
  context "An attachment with :only_process" do
468
- setup do
469
- rebuild_model :styles => {
470
- :thumb => "100x100",
471
- :large => "400x400"
470
+ before do
471
+ rebuild_model styles: {
472
+ thumb: "100x100",
473
+ large: "400x400"
472
474
  },
473
- :only_process => [:thumb]
475
+ only_process: [:thumb]
474
476
  @file = StringIO.new("...")
475
477
  @attachment = Dummy.new.avatar
476
478
  end
477
479
 
478
- should "only process the provided style" do
480
+ it "only processes the provided style" do
479
481
  @attachment.expects(:post_process).with(:thumb)
480
482
  @attachment.expects(:post_process).with(:large).never
481
483
  @attachment.assign(@file)
@@ -483,33 +485,34 @@ class AttachmentTest < Test::Unit::TestCase
483
485
  end
484
486
 
485
487
  context "An attachment with :only_process that is a proc" do
486
- setup do
487
- rebuild_model :styles => {
488
- :thumb => "100x100",
489
- :large => "400x400"
488
+ before do
489
+ rebuild_model styles: {
490
+ thumb: "100x100",
491
+ large: "400x400"
490
492
  },
491
- :only_process => lambda { |attachment| [:thumb] }
493
+ only_process: lambda { |attachment| [:thumb] }
492
494
 
493
495
  @file = StringIO.new("...")
494
496
  @attachment = Dummy.new.avatar
495
497
  end
496
498
 
497
- should "only process the provided style" do
499
+ it "only processes the provided style" do
498
500
  @attachment.expects(:post_process).with(:thumb)
499
501
  @attachment.expects(:post_process).with(:large).never
500
502
  @attachment.assign(@file)
503
+ @attachment.save
501
504
  end
502
505
  end
503
506
 
504
507
  context "An attachment with :convert_options that is a proc" do
505
- setup do
506
- rebuild_model :styles => {
507
- :thumb => "100x100",
508
- :large => "400x400"
508
+ before do
509
+ rebuild_model styles: {
510
+ thumb: "100x100",
511
+ large: "400x400"
509
512
  },
510
- :convert_options => {
511
- :all => lambda{|i| i.all },
512
- :thumb => lambda{|i| i.thumb }
513
+ convert_options: {
514
+ all: lambda{|i| i.all },
515
+ thumb: lambda{|i| i.thumb }
513
516
  }
514
517
  Dummy.class_eval do
515
518
  def all; "-all"; end
@@ -519,54 +522,54 @@ class AttachmentTest < Test::Unit::TestCase
519
522
  @dummy.avatar
520
523
  end
521
524
 
522
- should "report the correct options when sent #extra_options_for(:thumb)" do
525
+ it "reports the correct options when sent #extra_options_for(:thumb)" do
523
526
  assert_equal "-thumb -all", @dummy.avatar.send(:extra_options_for, :thumb), @dummy.avatar.convert_options.inspect
524
527
  end
525
528
 
526
- should "report the correct options when sent #extra_options_for(:large)" do
529
+ it "reports the correct options when sent #extra_options_for(:large)" do
527
530
  assert_equal "-all", @dummy.avatar.send(:extra_options_for, :large)
528
531
  end
529
532
  end
530
533
 
531
534
  context "An attachment with :path that is a proc" do
532
- setup do
533
- rebuild_model :path => lambda{ |attachment| "path/#{attachment.instance.other}.:extension" }
535
+ before do
536
+ rebuild_model path: lambda{ |attachment| "path/#{attachment.instance.other}.:extension" }
534
537
 
535
538
  @file = File.new(fixture_file("5k.png"), 'rb')
536
- @dummyA = Dummy.new(:other => 'a')
539
+ @dummyA = Dummy.new(other: 'a')
537
540
  @dummyA.avatar = @file
538
- @dummyB = Dummy.new(:other => 'b')
541
+ @dummyB = Dummy.new(other: 'b')
539
542
  @dummyB.avatar = @file
540
543
  end
541
544
 
542
- teardown { @file.close }
545
+ after { @file.close }
543
546
 
544
- should "return correct path" do
547
+ it "returns correct path" do
545
548
  assert_equal "path/a.png", @dummyA.avatar.path
546
549
  assert_equal "path/b.png", @dummyB.avatar.path
547
550
  end
548
551
  end
549
552
 
550
553
  context "An attachment with :styles that is a proc" do
551
- setup do
552
- rebuild_model :styles => lambda{ |attachment| {:thumb => "50x50#", :large => "400x400"} }
554
+ before do
555
+ rebuild_model styles: lambda{ |attachment| {thumb: "50x50#", large: "400x400"} }
553
556
 
554
557
  @attachment = Dummy.new.avatar
555
558
  end
556
559
 
557
- should "have the correct geometry" do
560
+ it "has the correct geometry" do
558
561
  assert_equal "50x50#", @attachment.styles[:thumb][:geometry]
559
562
  end
560
563
  end
561
564
 
562
565
  context "An attachment with conditional :styles that is a proc" do
563
- setup do
564
- rebuild_model :styles => lambda{ |attachment| attachment.instance.other == 'a' ? {:thumb => "50x50#"} : {:large => "400x400"} }
566
+ before do
567
+ rebuild_model styles: lambda{ |attachment| attachment.instance.other == 'a' ? {thumb: "50x50#"} : {large: "400x400"} }
565
568
 
566
- @dummy = Dummy.new(:other => 'a')
569
+ @dummy = Dummy.new(other: 'a')
567
570
  end
568
571
 
569
- should "have the correct styles for the assigned instance values" do
572
+ it "has the correct styles for the assigned instance values" do
570
573
  assert_equal "50x50#", @dummy.avatar.styles[:thumb][:geometry]
571
574
  assert_nil @dummy.avatar.styles[:large]
572
575
 
@@ -580,22 +583,22 @@ class AttachmentTest < Test::Unit::TestCase
580
583
  geometry_specs = [
581
584
  [ lambda{|z| "50x50#" }, :png ],
582
585
  lambda{|z| "50x50#" },
583
- { :geometry => lambda{|z| "50x50#" } }
586
+ { geometry: lambda{|z| "50x50#" } }
584
587
  ]
585
588
  geometry_specs.each do |geometry_spec|
586
589
  context "An attachment geometry like #{geometry_spec}" do
587
- setup do
588
- rebuild_model :styles => { :normal => geometry_spec }
590
+ before do
591
+ rebuild_model styles: { normal: geometry_spec }
589
592
  @attachment = Dummy.new.avatar
590
593
  end
591
594
 
592
595
  context "when assigned" do
593
- setup do
596
+ before do
594
597
  @file = StringIO.new(".")
595
598
  @attachment.assign(@file)
596
599
  end
597
600
 
598
- should "have the correct geometry" do
601
+ it "has the correct geometry" do
599
602
  assert_equal "50x50#", @attachment.styles[:normal][:geometry]
600
603
  end
601
604
  end
@@ -603,64 +606,89 @@ class AttachmentTest < Test::Unit::TestCase
603
606
  end
604
607
 
605
608
  context "An attachment with both 'normal' and hash-style styles" do
606
- setup do
607
- rebuild_model :styles => {
608
- :normal => ["50x50#", :png],
609
- :hash => { :geometry => "50x50#", :format => :png }
609
+ before do
610
+ rebuild_model styles: {
611
+ normal: ["50x50#", :png],
612
+ hash: { geometry: "50x50#", format: :png }
610
613
  }
611
614
  @dummy = Dummy.new
612
615
  @attachment = @dummy.avatar
613
616
  end
614
617
 
615
618
  [:processors, :whiny, :convert_options, :geometry, :format].each do |field|
616
- should "have the same #{field} field" do
619
+ it "has the same #{field} field" do
617
620
  assert_equal @attachment.styles[:normal][field], @attachment.styles[:hash][field]
618
621
  end
619
622
  end
620
623
  end
621
624
 
622
625
  context "An attachment with :processors that is a proc" do
623
- setup do
626
+ before do
624
627
  class Paperclip::Test < Paperclip::Processor; end
625
628
  @file = StringIO.new("...")
626
629
  Paperclip::Test.stubs(:make).returns(@file)
627
630
 
628
- rebuild_model :styles => { :normal => '' }, :processors => lambda { |a| [ :test ] }
631
+ rebuild_model styles: { normal: '' }, processors: lambda { |a| [ :test ] }
629
632
  @attachment = Dummy.new.avatar
630
633
  end
631
634
 
632
635
  context "when assigned" do
633
- setup do
636
+ before do
634
637
  @attachment.assign(StringIO.new("."))
635
638
  end
636
639
 
637
- should "have the correct processors" do
640
+ it "has the correct processors" do
638
641
  assert_equal [ :test ], @attachment.styles[:normal][:processors]
639
642
  end
640
643
  end
641
644
  end
642
645
 
643
646
  context "An attachment with erroring processor" do
644
- setup do
645
- rebuild_model :processor => [:thumbnail], :styles => { :small => '' }, :whiny_thumbnails => true
647
+ before do
648
+ rebuild_model processor: [:thumbnail], styles: { small: '' }, whiny_thumbnails: true
646
649
  @dummy = Dummy.new
647
- Paperclip::Thumbnail.expects(:make).raises(Paperclip::Error, "cannot be processed.")
648
650
  @file = StringIO.new("...")
649
651
  @file.stubs(:to_tempfile).returns(@file)
650
- @dummy.avatar = @file
651
652
  end
652
653
 
653
- should "correctly forward processing error message to the instance" do
654
- @dummy.valid?
655
- assert_contains @dummy.errors.full_messages, "Avatar cannot be processed."
654
+ context "when error is meaningful for the end user" do
655
+ before do
656
+ Paperclip::Thumbnail.expects(:make).raises(
657
+ Paperclip::Errors::NotIdentifiedByImageMagickError,
658
+ "cannot be processed."
659
+ )
660
+ end
661
+
662
+ it "correctly forwards processing error message to the instance" do
663
+ @dummy.avatar = @file
664
+ @dummy.valid?
665
+ assert_contains(
666
+ @dummy.errors.full_messages,
667
+ "Avatar cannot be processed."
668
+ )
669
+ end
670
+ end
671
+
672
+ context "when error is intended for the developer" do
673
+ before do
674
+ Paperclip::Thumbnail.expects(:make).raises(
675
+ Paperclip::Errors::CommandNotFoundError
676
+ )
677
+ end
678
+
679
+ it "propagates the error" do
680
+ assert_raises(Paperclip::Errors::CommandNotFoundError) do
681
+ @dummy.avatar = @file
682
+ end
683
+ end
656
684
  end
657
685
  end
658
686
 
659
687
  context "An attachment with multiple processors" do
660
- setup do
688
+ before do
661
689
  class Paperclip::Test < Paperclip::Processor; end
662
- @style_params = { :once => {:one => 1, :two => 2} }
663
- rebuild_model :processors => [:thumbnail, :test], :styles => @style_params
690
+ @style_params = { once: {one: 1, two: 2} }
691
+ rebuild_model processors: [:thumbnail, :test], styles: @style_params
664
692
  @dummy = Dummy.new
665
693
  @file = StringIO.new("...")
666
694
  @file.stubs(:close)
@@ -669,56 +697,87 @@ class AttachmentTest < Test::Unit::TestCase
669
697
  end
670
698
 
671
699
  context "when assigned" do
672
- setup { @dummy.avatar = @file }
700
+ it "calls #make on all specified processors" do
701
+ @dummy.avatar = @file
673
702
 
674
- before_should "call #make on all specified processors" do
675
- Paperclip::Thumbnail.expects(:make).with(any_parameters).returns(@file)
676
- Paperclip::Test.expects(:make).with(any_parameters).returns(@file)
703
+ expect(Paperclip::Thumbnail).to have_received(:make)
704
+ expect(Paperclip::Test).to have_received(:make)
677
705
  end
678
706
 
679
- before_should "call #make with the right parameters passed as second argument" do
707
+ it "calls #make with the right parameters passed as second argument" do
680
708
  expected_params = @style_params[:once].merge({
681
- :processors => [:thumbnail, :test],
682
- :whiny => true,
683
- :convert_options => "",
684
- :source_file_options => ""
709
+ style: :once,
710
+ processors: [:thumbnail, :test],
711
+ whiny: true,
712
+ convert_options: "",
713
+ source_file_options: ""
685
714
  })
686
- Paperclip::Thumbnail.expects(:make).with(anything, expected_params, anything).returns(@file)
715
+
716
+ @dummy.avatar = @file
717
+
718
+ expect(Paperclip::Thumbnail).to have_received(:make).with(anything, expected_params, anything)
687
719
  end
688
720
 
689
- before_should "call #make with attachment passed as third argument" do
690
- Paperclip::Test.expects(:make).with(anything, anything, @dummy.avatar).returns(@file)
721
+ it "calls #make with attachment passed as third argument" do
722
+ @dummy.avatar = @file
723
+
724
+ expect(Paperclip::Test).to have_received(:make).with(anything, anything, @dummy.avatar)
725
+ end
726
+
727
+ it "calls #make and unlinks intermediary files afterward" do
728
+ @dummy.avatar.expects(:unlink_files).with([@file, @file])
729
+
730
+ @dummy.avatar = @file
731
+ end
732
+ end
733
+ end
734
+
735
+ context "An attachment with a processor that returns original file" do
736
+ before do
737
+ class Paperclip::Test < Paperclip::Processor
738
+ def make; @file; end
739
+ end
740
+ rebuild_model processors: [:test], styles: { once: "100x100" }
741
+ @file = StringIO.new("...")
742
+ @file.stubs(:close)
743
+ @dummy = Dummy.new
744
+ end
745
+
746
+ context "when assigned" do
747
+ it "#calls #make and doesn't unlink the original file" do
748
+ @dummy.avatar.expects(:unlink_files).with([])
749
+
750
+ @dummy.avatar = @file
691
751
  end
692
752
  end
693
753
  end
694
754
 
695
- should "include the filesystem module when loading the filesystem storage" do
696
- rebuild_model :storage => :filesystem
755
+ it "includes the filesystem module when loading the filesystem storage" do
756
+ rebuild_model storage: :filesystem
697
757
  @dummy = Dummy.new
698
758
  assert @dummy.avatar.is_a?(Paperclip::Storage::Filesystem)
699
759
  end
700
760
 
701
- should "include the filesystem module even if capitalization is wrong" do
702
- rebuild_model :storage => :FileSystem
761
+ it "includes the filesystem module even if capitalization is wrong" do
762
+ rebuild_model storage: :FileSystem
703
763
  @dummy = Dummy.new
704
764
  assert @dummy.avatar.is_a?(Paperclip::Storage::Filesystem)
705
765
 
706
- rebuild_model :storage => :Filesystem
766
+ rebuild_model storage: :Filesystem
707
767
  @dummy = Dummy.new
708
768
  assert @dummy.avatar.is_a?(Paperclip::Storage::Filesystem)
709
769
  end
710
770
 
711
- should "convert underscored storage name to camelcase" do
712
- rebuild_model :storage => :not_here
771
+ it "converts underscored storage name to camelcase" do
772
+ rebuild_model storage: :not_here
713
773
  @dummy = Dummy.new
714
- exception = assert_raises(Paperclip::Errors::StorageMethodNotFound) do
774
+ exception = assert_raises(Paperclip::Errors::StorageMethodNotFound, /NotHere/) do
715
775
  @dummy.avatar
716
776
  end
717
- assert exception.message.include?("NotHere")
718
777
  end
719
778
 
720
- should "raise an error if you try to include a storage module that doesn't exist" do
721
- rebuild_model :storage => :not_here
779
+ it "raises an error if you try to include a storage module that doesn't exist" do
780
+ rebuild_model storage: :not_here
722
781
  @dummy = Dummy.new
723
782
  assert_raises(Paperclip::Errors::StorageMethodNotFound) do
724
783
  @dummy.avatar
@@ -726,30 +785,30 @@ class AttachmentTest < Test::Unit::TestCase
726
785
  end
727
786
 
728
787
  context "An attachment with styles but no processors defined" do
729
- setup do
730
- rebuild_model :processors => [], :styles => {:something => '1'}
788
+ before do
789
+ rebuild_model processors: [], styles: {something: '1'}
731
790
  @dummy = Dummy.new
732
791
  @file = StringIO.new("...")
733
792
  end
734
- should "raise when assigned to" do
793
+ it "raises when assigned to" do
735
794
  assert_raises(RuntimeError){ @dummy.avatar = @file }
736
795
  end
737
796
  end
738
797
 
739
798
  context "An attachment without styles and with no processors defined" do
740
- setup do
741
- rebuild_model :processors => [], :styles => {}
799
+ before do
800
+ rebuild_model processors: [], styles: {}
742
801
  @dummy = Dummy.new
743
802
  @file = StringIO.new("...")
744
803
  end
745
- should "not raise when assigned to" do
804
+ it "does not raise when assigned to" do
746
805
  @dummy.avatar = @file
747
806
  end
748
807
  end
749
808
 
750
809
  context "Assigning an attachment with post_process hooks" do
751
- setup do
752
- rebuild_class :styles => { :something => "100x100#" }
810
+ before do
811
+ rebuild_class styles: { something: "100x100#" }
753
812
  Dummy.class_eval do
754
813
  before_avatar_post_process :do_before_avatar
755
814
  after_avatar_post_process :do_after_avatar
@@ -767,7 +826,7 @@ class AttachmentTest < Test::Unit::TestCase
767
826
  @attachment = @dummy.avatar
768
827
  end
769
828
 
770
- should "call the defined callbacks when assigned" do
829
+ it "calls the defined callbacks when assigned" do
771
830
  @dummy.expects(:do_before_avatar).with()
772
831
  @dummy.expects(:do_after_avatar).with()
773
832
  @dummy.expects(:do_before_all).with()
@@ -776,7 +835,7 @@ class AttachmentTest < Test::Unit::TestCase
776
835
  @dummy.avatar = @file
777
836
  end
778
837
 
779
- should "not cancel the processing if a before_post_process returns nil" do
838
+ it "does not cancel the processing if a before_post_process returns nil" do
780
839
  @dummy.expects(:do_before_avatar).with().returns(nil)
781
840
  @dummy.expects(:do_after_avatar).with()
782
841
  @dummy.expects(:do_before_all).with().returns(nil)
@@ -785,7 +844,7 @@ class AttachmentTest < Test::Unit::TestCase
785
844
  @dummy.avatar = @file
786
845
  end
787
846
 
788
- should "cancel the processing if a before_post_process returns false" do
847
+ it "cancels the processing if a before_post_process returns false" do
789
848
  @dummy.expects(:do_before_avatar).never
790
849
  @dummy.expects(:do_after_avatar).never
791
850
  @dummy.expects(:do_before_all).with().returns(false)
@@ -794,7 +853,7 @@ class AttachmentTest < Test::Unit::TestCase
794
853
  @dummy.avatar = @file
795
854
  end
796
855
 
797
- should "cancel the processing if a before_avatar_post_process returns false" do
856
+ it "cancels the processing if a before_avatar_post_process returns false" do
798
857
  @dummy.expects(:do_before_avatar).with().returns(false)
799
858
  @dummy.expects(:do_after_avatar)
800
859
  @dummy.expects(:do_before_all).with().returns(true)
@@ -805,37 +864,37 @@ class AttachmentTest < Test::Unit::TestCase
805
864
  end
806
865
 
807
866
  context "Assigning an attachment" do
808
- setup do
809
- rebuild_model :styles => { :something => "100x100#" }
867
+ before do
868
+ rebuild_model styles: { something: "100x100#" }
810
869
  @file = File.new(fixture_file("5k.png"), "rb")
811
870
  @dummy = Dummy.new
812
871
  @dummy.avatar = @file
813
872
  end
814
873
 
815
- should "strip whitespace from original_filename field" do
874
+ it "strips whitespace from original_filename field" do
816
875
  assert_equal "5k.png", @dummy.avatar.original_filename
817
876
  end
818
877
 
819
- should "strip whitespace from content_type field" do
878
+ it "strips whitespace from content_type field" do
820
879
  assert_equal "image/png", @dummy.avatar.instance.avatar_content_type
821
880
  end
822
881
  end
823
882
 
824
883
  context "Assigning an attachment" do
825
- setup do
826
- rebuild_model :styles => { :something => "100x100#" }
884
+ before do
885
+ rebuild_model styles: { something: "100x100#" }
827
886
  @file = File.new(fixture_file("5k.png"), "rb")
828
887
  @dummy = Dummy.new
829
888
  @dummy.avatar = @file
830
889
  end
831
890
 
832
- should "make sure the content_type is a string" do
891
+ it "makes sure the content_type is a string" do
833
892
  assert_equal "image/png", @dummy.avatar.instance.avatar_content_type
834
893
  end
835
894
  end
836
895
 
837
896
  context "Attachment with strange letters" do
838
- setup do
897
+ before do
839
898
  rebuild_model
840
899
  @file = File.new(fixture_file("5k.png"), "rb")
841
900
  @file.stubs(:original_filename).returns("sheep_say_bæ.png")
@@ -843,18 +902,18 @@ class AttachmentTest < Test::Unit::TestCase
843
902
  @dummy.avatar = @file
844
903
  end
845
904
 
846
- should "not remove strange letters" do
905
+ it "does not remove strange letters" do
847
906
  assert_equal "sheep_say_bæ.png", @dummy.avatar.original_filename
848
907
  end
849
908
  end
850
909
 
851
910
  context "Attachment with reserved filename" do
852
- setup do
911
+ before do
853
912
  rebuild_model
854
913
  @file = Tempfile.new(["filename","png"])
855
914
  end
856
915
 
857
- teardown do
916
+ after do
858
917
  @file.unlink
859
918
  end
860
919
 
@@ -863,37 +922,37 @@ class AttachmentTest < Test::Unit::TestCase
863
922
  context "with character #{character}" do
864
923
 
865
924
  context "at beginning of filename" do
866
- setup do
925
+ before do
867
926
  @file.stubs(:original_filename).returns("#{character}filename.png")
868
927
  @dummy = Dummy.new
869
928
  @dummy.avatar = @file
870
929
  end
871
930
 
872
- should "convert special character into underscore" do
931
+ it "converts special character into underscore" do
873
932
  assert_equal "_filename.png", @dummy.avatar.original_filename
874
933
  end
875
934
  end
876
935
 
877
936
  context "at end of filename" do
878
- setup do
937
+ before do
879
938
  @file.stubs(:original_filename).returns("filename.png#{character}")
880
939
  @dummy = Dummy.new
881
940
  @dummy.avatar = @file
882
941
  end
883
942
 
884
- should "convert special character into underscore" do
943
+ it "converts special character into underscore" do
885
944
  assert_equal "filename.png_", @dummy.avatar.original_filename
886
945
  end
887
946
  end
888
947
 
889
948
  context "in the middle of filename" do
890
- setup do
949
+ before do
891
950
  @file.stubs(:original_filename).returns("file#{character}name.png")
892
951
  @dummy = Dummy.new
893
952
  @dummy.avatar = @file
894
953
  end
895
954
 
896
- should "convert special character into underscore" do
955
+ it "converts special character into underscore" do
897
956
  assert_equal "file_name.png", @dummy.avatar.original_filename
898
957
  end
899
958
  end
@@ -903,53 +962,53 @@ class AttachmentTest < Test::Unit::TestCase
903
962
  end
904
963
 
905
964
  context "with specified regexp replacement" do
906
- setup do
965
+ before do
907
966
  @old_defaults = Paperclip::Attachment.default_options.dup
908
967
  end
909
968
 
910
- teardown do
969
+ after do
911
970
  Paperclip::Attachment.default_options.merge! @old_defaults
912
971
  end
913
972
 
914
973
  context 'as another regexp' do
915
- setup do
916
- Paperclip::Attachment.default_options.merge! :restricted_characters => /o/
974
+ before do
975
+ Paperclip::Attachment.default_options.merge! restricted_characters: /o/
917
976
 
918
977
  @file.stubs(:original_filename).returns("goood.png")
919
978
  @dummy = Dummy.new
920
979
  @dummy.avatar = @file
921
980
  end
922
981
 
923
- should "match and convert that character" do
982
+ it "matches and converts that character" do
924
983
  assert_equal "g___d.png", @dummy.avatar.original_filename
925
984
  end
926
985
  end
927
986
 
928
987
  context 'as nil' do
929
- setup do
930
- Paperclip::Attachment.default_options.merge! :restricted_characters => nil
988
+ before do
989
+ Paperclip::Attachment.default_options.merge! restricted_characters: nil
931
990
 
932
991
  @file.stubs(:original_filename).returns("goood.png")
933
992
  @dummy = Dummy.new
934
993
  @dummy.avatar = @file
935
994
  end
936
995
 
937
- should "ignore and return the original file name" do
996
+ it "ignores and returns the original file name" do
938
997
  assert_equal "goood.png", @dummy.avatar.original_filename
939
998
  end
940
999
  end
941
1000
  end
942
1001
 
943
1002
  context 'with specified cleaner' do
944
- setup do
1003
+ before do
945
1004
  @old_defaults = Paperclip::Attachment.default_options.dup
946
1005
  end
947
1006
 
948
- teardown do
1007
+ after do
949
1008
  Paperclip::Attachment.default_options.merge! @old_defaults
950
1009
  end
951
1010
 
952
- should 'call the given proc and take the result as cleaned filename' do
1011
+ it 'calls the given proc and take the result as cleaned filename' do
953
1012
  Paperclip::Attachment.default_options[:filename_cleaner] = lambda do |str|
954
1013
  "from_proc_#{str}"
955
1014
  end
@@ -960,7 +1019,7 @@ class AttachmentTest < Test::Unit::TestCase
960
1019
  assert_equal "from_proc_goood.png", @dummy.avatar.original_filename
961
1020
  end
962
1021
 
963
- should 'call the given object and take the result as the cleaned filename' do
1022
+ it 'calls the given object and take the result as the cleaned filename' do
964
1023
  class MyCleaner
965
1024
  def call(filename)
966
1025
  "foo"
@@ -977,102 +1036,100 @@ class AttachmentTest < Test::Unit::TestCase
977
1036
  end
978
1037
 
979
1038
  context "Attachment with uppercase extension and a default style" do
980
- setup do
1039
+ before do
981
1040
  @old_defaults = Paperclip::Attachment.default_options.dup
982
1041
  Paperclip::Attachment.default_options.merge!({
983
- :path => ":rails_root/:attachment/:class/:style/:id/:basename.:extension"
1042
+ path: ":rails_root/:attachment/:class/:style/:id/:basename.:extension"
984
1043
  })
985
1044
  FileUtils.rm_rf("tmp")
986
- rebuild_model
1045
+ rebuild_model styles: { large: ["400x400", :jpg],
1046
+ medium: ["100x100", :jpg],
1047
+ small: ["32x32#", :jpg]},
1048
+ default_style: :small
987
1049
  @instance = Dummy.new
988
1050
  @instance.stubs(:id).returns 123
989
-
990
1051
  @file = File.new(fixture_file("uppercase.PNG"), 'rb')
991
1052
 
992
- styles = {:styles => { :large => ["400x400", :jpg],
993
- :medium => ["100x100", :jpg],
994
- :small => ["32x32#", :jpg]},
995
- :default_style => :small}
996
- @attachment = Paperclip::Attachment.new(:avatar,
997
- @instance,
998
- styles)
1053
+ @attachment = @instance.avatar
1054
+
999
1055
  now = Time.now
1000
1056
  Time.stubs(:now).returns(now)
1001
1057
  @attachment.assign(@file)
1002
1058
  @attachment.save
1003
1059
  end
1004
1060
 
1005
- teardown do
1061
+ after do
1006
1062
  @file.close
1007
1063
  Paperclip::Attachment.default_options.merge!(@old_defaults)
1008
1064
  end
1009
1065
 
1010
- should "should have matching to_s and url methods" do
1011
- assert_match @attachment.to_s, @attachment.url
1012
- assert_match @attachment.to_s(:small), @attachment.url(:small)
1066
+ it "has matching to_s and url methods" do
1067
+ assert_equal @attachment.to_s, @attachment.url
1068
+ assert_equal @attachment.to_s(:small), @attachment.url(:small)
1013
1069
  end
1014
1070
 
1015
- should "have matching expiring_url and url methods when using the filesystem storage" do
1016
- assert_match @attachment.expiring_url, @attachment.url
1071
+ it "has matching expiring_url and url methods when using the filesystem storage" do
1072
+ assert_equal @attachment.expiring_url, @attachment.url
1017
1073
  end
1018
1074
  end
1019
1075
 
1020
1076
  context "An attachment" do
1021
- setup do
1077
+ before do
1022
1078
  @old_defaults = Paperclip::Attachment.default_options.dup
1023
1079
  Paperclip::Attachment.default_options.merge!({
1024
- :path => ":rails_root/:attachment/:class/:style/:id/:basename.:extension"
1080
+ path: ":rails_root/:attachment/:class/:style/:id/:basename.:extension"
1025
1081
  })
1026
1082
  FileUtils.rm_rf("tmp")
1027
1083
  rebuild_model
1028
1084
  @instance = Dummy.new
1029
1085
  @instance.stubs(:id).returns 123
1030
- @attachment = Paperclip::Attachment.new(:avatar, @instance)
1086
+ # @attachment = Paperclip::Attachment.new(:avatar, @instance)
1087
+ @attachment = @instance.avatar
1031
1088
  @file = File.new(fixture_file("5k.png"), 'rb')
1032
1089
  end
1033
1090
 
1034
- teardown do
1091
+ after do
1035
1092
  @file.close
1036
1093
  Paperclip::Attachment.default_options.merge!(@old_defaults)
1037
1094
  end
1038
1095
 
1039
- should "raise if there are not the correct columns when you try to assign" do
1096
+ it "raises if there are not the correct columns when you try to assign" do
1040
1097
  @other_attachment = Paperclip::Attachment.new(:not_here, @instance)
1041
1098
  assert_raises(Paperclip::Error) do
1042
1099
  @other_attachment.assign(@file)
1043
1100
  end
1044
1101
  end
1045
1102
 
1046
- should 'clear out the previous assignment when assigned nil' do
1103
+ it 'clears out the previous assignment when assigned nil' do
1047
1104
  @attachment.assign(@file)
1048
- original_file = @attachment.queued_for_write[:original]
1105
+ @attachment.queued_for_write[:original]
1049
1106
  @attachment.assign(nil)
1050
1107
  assert_nil @attachment.queued_for_write[:original]
1051
1108
  end
1052
1109
 
1053
- should 'not do anything when it is assigned an empty string' do
1110
+ it 'does not do anything when it is assigned an empty string' do
1054
1111
  @attachment.assign(@file)
1055
1112
  original_file = @attachment.queued_for_write[:original]
1056
1113
  @attachment.assign("")
1057
1114
  assert_equal original_file, @attachment.queued_for_write[:original]
1058
1115
  end
1059
1116
 
1060
- should "return nil as path when no file assigned" do
1117
+ it "returns nil as path when no file assigned" do
1061
1118
  assert_equal nil, @attachment.path
1062
1119
  assert_equal nil, @attachment.path(:blah)
1063
1120
  end
1064
1121
 
1065
1122
  context "with a file assigned but not saved yet" do
1066
- should "clear out any attached files" do
1123
+ it "clears out any attached files" do
1067
1124
  @attachment.assign(@file)
1068
- assert !@attachment.queued_for_write.blank?
1125
+ assert @attachment.queued_for_write.present?
1069
1126
  @attachment.clear
1070
1127
  assert @attachment.queued_for_write.blank?
1071
1128
  end
1072
1129
  end
1073
1130
 
1074
1131
  context "with a file assigned in the database" do
1075
- setup do
1132
+ before do
1076
1133
  @attachment.stubs(:instance_read).with(:file_name).returns("5k.png")
1077
1134
  @attachment.stubs(:instance_read).with(:content_type).returns("image/png")
1078
1135
  @attachment.stubs(:instance_read).with(:file_size).returns(12345)
@@ -1082,54 +1139,57 @@ class AttachmentTest < Test::Unit::TestCase
1082
1139
  @attachment.stubs(:instance_read).with(:updated_at).returns(dtnow)
1083
1140
  end
1084
1141
 
1085
- should "return the proper path when filename has a single .'s" do
1142
+ it "returns the proper path when filename has a single .'s" do
1086
1143
  assert_equal File.expand_path("tmp/avatars/dummies/original/#{@instance.id}/5k.png"), File.expand_path(@attachment.path)
1087
1144
  end
1088
1145
 
1089
- should "return the proper path when filename has multiple .'s" do
1146
+ it "returns the proper path when filename has multiple .'s" do
1090
1147
  @attachment.stubs(:instance_read).with(:file_name).returns("5k.old.png")
1091
1148
  assert_equal File.expand_path("tmp/avatars/dummies/original/#{@instance.id}/5k.old.png"), File.expand_path(@attachment.path)
1092
1149
  end
1093
1150
 
1094
1151
  context "when expecting three styles" do
1095
- setup do
1096
- styles = {:styles => { :large => ["400x400", :png],
1097
- :medium => ["100x100", :gif],
1098
- :small => ["32x32#", :jpg]}}
1099
- @attachment = Paperclip::Attachment.new(:avatar,
1100
- @instance,
1101
- styles)
1152
+ before do
1153
+ rebuild_class styles: {
1154
+ large: ["400x400", :png],
1155
+ medium: ["100x100", :gif],
1156
+ small: ["32x32#", :jpg]
1157
+ }
1158
+ @instance = Dummy.new
1159
+ @instance.stubs(:id).returns 123
1160
+ @file = File.new(fixture_file("5k.png"), 'rb')
1161
+ @attachment = @instance.avatar
1102
1162
  end
1103
1163
 
1104
1164
  context "and assigned a file" do
1105
- setup do
1165
+ before do
1106
1166
  now = Time.now
1107
1167
  Time.stubs(:now).returns(now)
1108
1168
  @attachment.assign(@file)
1109
1169
  end
1110
1170
 
1111
- should "be dirty" do
1171
+ it "is dirty" do
1112
1172
  assert @attachment.dirty?
1113
1173
  end
1114
1174
 
1115
1175
  context "and saved" do
1116
- setup do
1176
+ before do
1117
1177
  @attachment.save
1118
1178
  end
1119
1179
 
1120
- should "commit the files to disk" do
1180
+ it "commits the files to disk" do
1121
1181
  [:large, :medium, :small].each do |style|
1122
- assert_file_exists(@attachment.path(style))
1182
+ expect(@attachment.path(style)).to exist
1123
1183
  end
1124
1184
  end
1125
1185
 
1126
- should "save the files as the right formats and sizes" do
1186
+ it "saves the files as the right formats and sizes" do
1127
1187
  [[:large, 400, 61, "PNG"],
1128
1188
  [:medium, 100, 15, "GIF"],
1129
1189
  [:small, 32, 32, "JPEG"]].each do |style|
1130
1190
  cmd = %Q[identify -format "%w %h %b %m" "#{@attachment.path(style.first)}"]
1131
1191
  out = `#{cmd}`
1132
- width, height, size, format = out.split(" ")
1192
+ width, height, _size, format = out.split(" ")
1133
1193
  assert_equal style[1].to_s, width.to_s
1134
1194
  assert_equal style[2].to_s, height.to_s
1135
1195
  assert_equal style[3].to_s, format.to_s
@@ -1137,13 +1197,13 @@ class AttachmentTest < Test::Unit::TestCase
1137
1197
  end
1138
1198
 
1139
1199
  context "and trying to delete" do
1140
- setup do
1200
+ before do
1141
1201
  @existing_names = @attachment.styles.keys.collect do |style|
1142
1202
  @attachment.path(style)
1143
1203
  end
1144
1204
  end
1145
1205
 
1146
- should "delete the files after assigning nil" do
1206
+ it "deletes the files after assigning nil" do
1147
1207
  @attachment.expects(:instance_write).with(:file_name, nil)
1148
1208
  @attachment.expects(:instance_write).with(:content_type, nil)
1149
1209
  @attachment.expects(:instance_write).with(:file_size, nil)
@@ -1154,7 +1214,7 @@ class AttachmentTest < Test::Unit::TestCase
1154
1214
  @existing_names.each{|f| assert_file_not_exists(f) }
1155
1215
  end
1156
1216
 
1157
- should "delete the files when you call #clear and #save" do
1217
+ it "deletes the files when you call #clear and #save" do
1158
1218
  @attachment.expects(:instance_write).with(:file_name, nil)
1159
1219
  @attachment.expects(:instance_write).with(:content_type, nil)
1160
1220
  @attachment.expects(:instance_write).with(:file_size, nil)
@@ -1165,7 +1225,7 @@ class AttachmentTest < Test::Unit::TestCase
1165
1225
  @existing_names.each{|f| assert_file_not_exists(f) }
1166
1226
  end
1167
1227
 
1168
- should "delete the files when you call #delete" do
1228
+ it "deletes the files when you call #delete" do
1169
1229
  @attachment.expects(:instance_write).with(:file_name, nil)
1170
1230
  @attachment.expects(:instance_write).with(:content_type, nil)
1171
1231
  @attachment.expects(:instance_write).with(:file_size, nil)
@@ -1176,11 +1236,11 @@ class AttachmentTest < Test::Unit::TestCase
1176
1236
  end
1177
1237
 
1178
1238
  context "when keeping old files" do
1179
- setup do
1239
+ before do
1180
1240
  @attachment.options[:keep_old_files] = true
1181
1241
  end
1182
1242
 
1183
- should "keep the files after assigning nil" do
1243
+ it "keeps the files after assigning nil" do
1184
1244
  @attachment.expects(:instance_write).with(:file_name, nil)
1185
1245
  @attachment.expects(:instance_write).with(:content_type, nil)
1186
1246
  @attachment.expects(:instance_write).with(:file_size, nil)
@@ -1191,7 +1251,7 @@ class AttachmentTest < Test::Unit::TestCase
1191
1251
  @existing_names.each{|f| assert_file_exists(f) }
1192
1252
  end
1193
1253
 
1194
- should "keep the files when you call #clear and #save" do
1254
+ it "keeps the files when you call #clear and #save" do
1195
1255
  @attachment.expects(:instance_write).with(:file_name, nil)
1196
1256
  @attachment.expects(:instance_write).with(:content_type, nil)
1197
1257
  @attachment.expects(:instance_write).with(:file_size, nil)
@@ -1202,7 +1262,7 @@ class AttachmentTest < Test::Unit::TestCase
1202
1262
  @existing_names.each{|f| assert_file_exists(f) }
1203
1263
  end
1204
1264
 
1205
- should "keep the files when you call #delete" do
1265
+ it "keeps the files when you call #delete" do
1206
1266
  @attachment.expects(:instance_write).with(:file_name, nil)
1207
1267
  @attachment.expects(:instance_write).with(:content_type, nil)
1208
1268
  @attachment.expects(:instance_write).with(:file_size, nil)
@@ -1219,19 +1279,19 @@ class AttachmentTest < Test::Unit::TestCase
1219
1279
  end
1220
1280
 
1221
1281
  context "when trying a nonexistant storage type" do
1222
- setup do
1223
- rebuild_model :storage => :not_here
1282
+ before do
1283
+ rebuild_model storage: :not_here
1224
1284
  end
1225
1285
 
1226
- should "not be able to find the module" do
1227
- assert_raise(Paperclip::Errors::StorageMethodNotFound){ Dummy.new.avatar }
1286
+ it "is not able to find the module" do
1287
+ assert_raises(Paperclip::Errors::StorageMethodNotFound){ Dummy.new.avatar }
1228
1288
  end
1229
1289
  end
1230
1290
  end
1231
1291
 
1232
1292
  context "An attachment with only a avatar_file_name column" do
1233
- setup do
1234
- ActiveRecord::Base.connection.create_table :dummies, :force => true do |table|
1293
+ before do
1294
+ ActiveRecord::Base.connection.create_table :dummies, force: true do |table|
1235
1295
  table.column :avatar_file_name, :string
1236
1296
  end
1237
1297
  rebuild_class
@@ -1239,41 +1299,41 @@ class AttachmentTest < Test::Unit::TestCase
1239
1299
  @file = File.new(fixture_file("5k.png"), 'rb')
1240
1300
  end
1241
1301
 
1242
- teardown { @file.close }
1302
+ after { @file.close }
1243
1303
 
1244
- should "not error when assigned an attachment" do
1304
+ it "does not error when assigned an attachment" do
1245
1305
  assert_nothing_raised { @dummy.avatar = @file }
1246
1306
  end
1247
1307
 
1248
- should "not return the time when sent #avatar_updated_at" do
1308
+ it "does not return the time when sent #avatar_updated_at" do
1249
1309
  @dummy.avatar = @file
1250
1310
  assert_nil @dummy.avatar.updated_at
1251
1311
  end
1252
1312
 
1253
- should "return the right value when sent #avatar_file_size" do
1313
+ it "returns the right value when sent #avatar_file_size" do
1254
1314
  @dummy.avatar = @file
1255
1315
  assert_equal File.size(@file), @dummy.avatar.size
1256
1316
  end
1257
1317
 
1258
1318
  context "and avatar_created_at column" do
1259
- setup do
1319
+ before do
1260
1320
  ActiveRecord::Base.connection.add_column :dummies, :avatar_created_at, :timestamp
1261
1321
  rebuild_class
1262
1322
  @dummy = Dummy.new
1263
1323
  end
1264
1324
 
1265
- should "not error when assigned an attachment" do
1325
+ it "does not error when assigned an attachment" do
1266
1326
  assert_nothing_raised { @dummy.avatar = @file }
1267
1327
  end
1268
1328
 
1269
- should "return the creation time when sent #avatar_created_at" do
1329
+ it "returns the creation time when sent #avatar_created_at" do
1270
1330
  now = Time.now
1271
1331
  Time.stubs(:now).returns(now)
1272
1332
  @dummy.avatar = @file
1273
1333
  assert_equal now.to_i, @dummy.avatar.created_at
1274
1334
  end
1275
1335
 
1276
- should "return the creation time when sent #avatar_created_at and the entry has been updated" do
1336
+ it "returns the creation time when sent #avatar_created_at and the entry has been updated" do
1277
1337
  creation = 2.hours.ago
1278
1338
  now = Time.now
1279
1339
  Time.stubs(:now).returns(creation)
@@ -1284,7 +1344,7 @@ class AttachmentTest < Test::Unit::TestCase
1284
1344
  assert_not_equal now.to_i, @dummy.avatar.created_at
1285
1345
  end
1286
1346
 
1287
- should "set changed? to true on attachment assignment" do
1347
+ it "sets changed? to true on attachment assignment" do
1288
1348
  @dummy.avatar = @file
1289
1349
  @dummy.save!
1290
1350
  @dummy.avatar = @file
@@ -1293,17 +1353,17 @@ class AttachmentTest < Test::Unit::TestCase
1293
1353
  end
1294
1354
 
1295
1355
  context "and avatar_updated_at column" do
1296
- setup do
1356
+ before do
1297
1357
  ActiveRecord::Base.connection.add_column :dummies, :avatar_updated_at, :timestamp
1298
1358
  rebuild_class
1299
1359
  @dummy = Dummy.new
1300
1360
  end
1301
1361
 
1302
- should "not error when assigned an attachment" do
1362
+ it "does not error when assigned an attachment" do
1303
1363
  assert_nothing_raised { @dummy.avatar = @file }
1304
1364
  end
1305
1365
 
1306
- should "return the right value when sent #avatar_updated_at" do
1366
+ it "returns the right value when sent #avatar_updated_at" do
1307
1367
  now = Time.now
1308
1368
  Time.stubs(:now).returns(now)
1309
1369
  @dummy.avatar = @file
@@ -1311,45 +1371,51 @@ class AttachmentTest < Test::Unit::TestCase
1311
1371
  end
1312
1372
  end
1313
1373
 
1314
- should "not calculate fingerprint" do
1374
+ it "does not calculate fingerprint" do
1375
+ Digest::MD5.stubs(:file)
1376
+ @dummy.avatar = @file
1377
+ expect(Digest::MD5).to have_received(:file).never
1378
+ end
1379
+
1380
+ it "does not assign fingerprint" do
1315
1381
  @dummy.avatar = @file
1316
1382
  assert_nil @dummy.avatar.fingerprint
1317
1383
  end
1318
1384
 
1319
1385
  context "and avatar_content_type column" do
1320
- setup do
1386
+ before do
1321
1387
  ActiveRecord::Base.connection.add_column :dummies, :avatar_content_type, :string
1322
1388
  rebuild_class
1323
1389
  @dummy = Dummy.new
1324
1390
  end
1325
1391
 
1326
- should "not error when assigned an attachment" do
1392
+ it "does not error when assigned an attachment" do
1327
1393
  assert_nothing_raised { @dummy.avatar = @file }
1328
1394
  end
1329
1395
 
1330
- should "return the right value when sent #avatar_content_type" do
1396
+ it "returns the right value when sent #avatar_content_type" do
1331
1397
  @dummy.avatar = @file
1332
1398
  assert_equal "image/png", @dummy.avatar.content_type
1333
1399
  end
1334
1400
  end
1335
1401
 
1336
1402
  context "and avatar_file_size column" do
1337
- setup do
1403
+ before do
1338
1404
  ActiveRecord::Base.connection.add_column :dummies, :avatar_file_size, :integer
1339
1405
  rebuild_class
1340
1406
  @dummy = Dummy.new
1341
1407
  end
1342
1408
 
1343
- should "not error when assigned an attachment" do
1409
+ it "does not error when assigned an attachment" do
1344
1410
  assert_nothing_raised { @dummy.avatar = @file }
1345
1411
  end
1346
1412
 
1347
- should "return the right value when sent #avatar_file_size" do
1413
+ it "returns the right value when sent #avatar_file_size" do
1348
1414
  @dummy.avatar = @file
1349
1415
  assert_equal File.size(@file), @dummy.avatar.size
1350
1416
  end
1351
1417
 
1352
- should "return the right value when saved, reloaded, and sent #avatar_file_size" do
1418
+ it "returns the right value when saved, reloaded, and sent #avatar_file_size" do
1353
1419
  @dummy.avatar = @file
1354
1420
  @dummy.save
1355
1421
  @dummy = Dummy.find(@dummy.id)
@@ -1358,33 +1424,63 @@ class AttachmentTest < Test::Unit::TestCase
1358
1424
  end
1359
1425
 
1360
1426
  context "and avatar_fingerprint column" do
1361
- setup do
1427
+ before do
1362
1428
  ActiveRecord::Base.connection.add_column :dummies, :avatar_fingerprint, :string
1363
1429
  rebuild_class
1364
1430
  @dummy = Dummy.new
1365
1431
  end
1366
1432
 
1367
- should "not error when assigned an attachment" do
1433
+ it "does not error when assigned an attachment" do
1368
1434
  assert_nothing_raised { @dummy.avatar = @file }
1369
1435
  end
1370
1436
 
1371
- should "return the right value when sent #avatar_fingerprint" do
1372
- @dummy.avatar = @file
1373
- assert_equal 'aec488126c3b33c08a10c3fa303acf27', @dummy.avatar_fingerprint
1437
+ context "with explicitly set digest" do
1438
+ before do
1439
+ rebuild_class adapter_options: { hash_digest: Digest::SHA256 }
1440
+ @dummy = Dummy.new
1441
+ end
1442
+
1443
+ it "returns the right value when sent #avatar_fingerprint" do
1444
+ @dummy.avatar = @file
1445
+ assert_equal "734016d801a497f5579cdd4ef2ae1d020088c1db754dc434482d76dd5486520a",
1446
+ @dummy.avatar_fingerprint
1447
+ end
1448
+
1449
+ it "returns the right value when saved, reloaded, and sent #avatar_fingerprint" do
1450
+ @dummy.avatar = @file
1451
+ @dummy.save
1452
+ @dummy = Dummy.find(@dummy.id)
1453
+ assert_equal "734016d801a497f5579cdd4ef2ae1d020088c1db754dc434482d76dd5486520a",
1454
+ @dummy.avatar_fingerprint
1455
+ end
1374
1456
  end
1375
1457
 
1376
- should "return the right value when saved, reloaded, and sent #avatar_fingerprint" do
1377
- @dummy.avatar = @file
1378
- @dummy.save
1379
- @dummy = Dummy.find(@dummy.id)
1380
- assert_equal 'aec488126c3b33c08a10c3fa303acf27', @dummy.avatar_fingerprint
1458
+ context "with the default digest" do
1459
+ before do
1460
+ rebuild_class # MD5 is the default
1461
+ @dummy = Dummy.new
1462
+ end
1463
+
1464
+ it "returns the right value when sent #avatar_fingerprint" do
1465
+ @dummy.avatar = @file
1466
+ assert_equal "aec488126c3b33c08a10c3fa303acf27",
1467
+ @dummy.avatar_fingerprint
1468
+ end
1469
+
1470
+ it "returns the right value when saved, reloaded, and sent #avatar_fingerprint" do
1471
+ @dummy.avatar = @file
1472
+ @dummy.save
1473
+ @dummy = Dummy.find(@dummy.id)
1474
+ assert_equal "aec488126c3b33c08a10c3fa303acf27",
1475
+ @dummy.avatar_fingerprint
1476
+ end
1381
1477
  end
1382
1478
  end
1383
1479
  end
1384
1480
 
1385
1481
  context "an attachment with delete_file option set to false" do
1386
- setup do
1387
- rebuild_model :preserve_files => true
1482
+ before do
1483
+ rebuild_model preserve_files: true
1388
1484
  @dummy = Dummy.new
1389
1485
  @file = File.new(fixture_file("5k.png"), 'rb')
1390
1486
  @dummy.avatar = @file
@@ -1393,27 +1489,27 @@ class AttachmentTest < Test::Unit::TestCase
1393
1489
  @path = @attachment.path
1394
1490
  end
1395
1491
 
1396
- teardown { @file.close }
1492
+ after { @file.close }
1397
1493
 
1398
- should "not delete the files from storage when attachment is destroyed" do
1494
+ it "does not delete the files from storage when attachment is destroyed" do
1399
1495
  @attachment.destroy
1400
1496
  assert_file_exists(@path)
1401
1497
  end
1402
1498
 
1403
- should "clear out attachment data when attachment is destroyed" do
1499
+ it "clears out attachment data when attachment is destroyed" do
1404
1500
  @attachment.destroy
1405
1501
  assert !@attachment.exists?
1406
1502
  assert_nil @dummy.avatar_file_name
1407
1503
  end
1408
1504
 
1409
- should "not delete the file when model is destroyed" do
1505
+ it "does not delete the file when model is destroyed" do
1410
1506
  @dummy.destroy
1411
1507
  assert_file_exists(@path)
1412
1508
  end
1413
1509
  end
1414
1510
 
1415
1511
  context "An attached file" do
1416
- setup do
1512
+ before do
1417
1513
  rebuild_model
1418
1514
  @dummy = Dummy.new
1419
1515
  @file = File.new(fixture_file("5k.png"), 'rb')
@@ -1423,24 +1519,24 @@ class AttachmentTest < Test::Unit::TestCase
1423
1519
  @path = @attachment.path
1424
1520
  end
1425
1521
 
1426
- teardown { @file.close }
1522
+ after { @file.close }
1427
1523
 
1428
- should "not be deleted when the model fails to destroy" do
1524
+ it "is not deleted when the model fails to destroy" do
1429
1525
  @dummy.stubs(:destroy).raises(Exception)
1430
1526
 
1431
- assert_raise Exception do
1527
+ assert_raises Exception do
1432
1528
  @dummy.destroy
1433
1529
  end
1434
1530
 
1435
1531
  assert_file_exists(@path)
1436
1532
  end
1437
1533
 
1438
- should "be deleted when the model is destroyed" do
1534
+ it "is deleted when the model is destroyed" do
1439
1535
  @dummy.destroy
1440
1536
  assert_file_not_exists(@path)
1441
1537
  end
1442
1538
 
1443
- should "not be deleted when transaction rollbacks after model is destroyed" do
1539
+ it "is not deleted when transaction rollbacks after model is destroyed" do
1444
1540
  ActiveRecord::Base.transaction do
1445
1541
  @dummy.destroy
1446
1542
  raise ActiveRecord::Rollback
@@ -1449,5 +1545,4 @@ class AttachmentTest < Test::Unit::TestCase
1449
1545
  assert_file_exists(@path)
1450
1546
  end
1451
1547
  end
1452
-
1453
1548
  end