paperclip 3.5.4 → 6.1.0

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