paperclip 3.4.0 → 6.1.0

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