kt-paperclip 5.4.0 → 7.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (159) hide show
  1. checksums.yaml +4 -4
  2. data/.github/FUNDING.yml +3 -0
  3. data/.github/ISSUE_TEMPLATE/bug_report.md +38 -0
  4. data/.github/ISSUE_TEMPLATE/custom.md +10 -0
  5. data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  6. data/.hound.yml +3 -1055
  7. data/.rubocop.yml +1061 -1
  8. data/.travis.yml +23 -4
  9. data/Appraisals +23 -0
  10. data/CONTRIBUTING.md +4 -5
  11. data/Gemfile +10 -7
  12. data/NEWS +52 -0
  13. data/README.md +58 -46
  14. data/Rakefile +29 -21
  15. data/UPGRADING +3 -3
  16. data/features/basic_integration.feature +4 -0
  17. data/features/migration.feature +10 -51
  18. data/features/step_definitions/attachment_steps.rb +23 -13
  19. data/features/step_definitions/html_steps.rb +5 -5
  20. data/features/step_definitions/rails_steps.rb +29 -9
  21. data/features/step_definitions/s3_steps.rb +3 -3
  22. data/features/step_definitions/web_steps.rb +5 -6
  23. data/features/support/env.rb +4 -4
  24. data/features/support/fakeweb.rb +3 -5
  25. data/features/support/file_helpers.rb +2 -2
  26. data/features/support/paths.rb +4 -4
  27. data/features/support/rails.rb +7 -7
  28. data/features/support/selectors.rb +1 -1
  29. data/gemfiles/4.2.gemfile +7 -4
  30. data/gemfiles/5.0.gemfile +7 -4
  31. data/gemfiles/5.1.gemfile +20 -0
  32. data/gemfiles/5.2.gemfile +20 -0
  33. data/gemfiles/6.0.gemfile +20 -0
  34. data/gemfiles/6.1.gemfile +21 -0
  35. data/gemfiles/7.0.gemfile +21 -0
  36. data/lib/generators/paperclip/paperclip_generator.rb +6 -8
  37. data/lib/paperclip/attachment.rb +103 -105
  38. data/lib/paperclip/attachment_registry.rb +2 -2
  39. data/lib/paperclip/content_type_detector.rb +10 -5
  40. data/lib/paperclip/file_command_content_type_detector.rb +1 -3
  41. data/lib/paperclip/filename_cleaner.rb +0 -1
  42. data/lib/paperclip/geometry.rb +18 -19
  43. data/lib/paperclip/geometry_detector_factory.rb +13 -16
  44. data/lib/paperclip/geometry_parser_factory.rb +5 -5
  45. data/lib/paperclip/glue.rb +3 -3
  46. data/lib/paperclip/has_attached_file.rb +5 -4
  47. data/lib/paperclip/helpers.rb +3 -3
  48. data/lib/paperclip/interpolations.rb +42 -38
  49. data/lib/paperclip/io_adapters/abstract_adapter.rb +16 -14
  50. data/lib/paperclip/io_adapters/attachment_adapter.rb +12 -6
  51. data/lib/paperclip/io_adapters/data_uri_adapter.rb +1 -1
  52. data/lib/paperclip/io_adapters/file_adapter.rb +1 -3
  53. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +1 -1
  54. data/lib/paperclip/io_adapters/identity_adapter.rb +1 -2
  55. data/lib/paperclip/io_adapters/registry.rb +1 -1
  56. data/lib/paperclip/io_adapters/stringio_adapter.rb +1 -1
  57. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +6 -8
  58. data/lib/paperclip/io_adapters/uri_adapter.rb +21 -9
  59. data/lib/paperclip/logger.rb +1 -1
  60. data/lib/paperclip/matchers/have_attached_file_matcher.rb +4 -4
  61. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +19 -18
  62. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +4 -4
  63. data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +11 -10
  64. data/lib/paperclip/matchers.rb +4 -4
  65. data/lib/paperclip/media_type_spoof_detector.rb +13 -13
  66. data/lib/paperclip/missing_attachment_styles.rb +11 -6
  67. data/lib/paperclip/processor.rb +13 -6
  68. data/lib/paperclip/processor_helpers.rb +3 -1
  69. data/lib/paperclip/rails_environment.rb +1 -5
  70. data/lib/paperclip/railtie.rb +5 -5
  71. data/lib/paperclip/schema.rb +16 -12
  72. data/lib/paperclip/storage/filesystem.rb +6 -8
  73. data/lib/paperclip/storage/fog.rb +36 -32
  74. data/lib/paperclip/storage/s3.rb +68 -76
  75. data/lib/paperclip/style.rb +3 -6
  76. data/lib/paperclip/tempfile.rb +4 -5
  77. data/lib/paperclip/tempfile_factory.rb +0 -1
  78. data/lib/paperclip/thumbnail.rb +11 -11
  79. data/lib/paperclip/url_generator.rb +5 -5
  80. data/lib/paperclip/validators/attachment_content_type_validator.rb +11 -4
  81. data/lib/paperclip/validators/attachment_file_name_validator.rb +14 -12
  82. data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +1 -2
  83. data/lib/paperclip/validators/attachment_presence_validator.rb +3 -5
  84. data/lib/paperclip/validators/attachment_size_validator.rb +28 -11
  85. data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +3 -1
  86. data/lib/paperclip/validators.rb +16 -17
  87. data/lib/paperclip/version.rb +1 -3
  88. data/lib/paperclip.rb +49 -48
  89. data/lib/tasks/paperclip.rake +23 -24
  90. data/paperclip.gemspec +29 -33
  91. data/shoulda_macros/paperclip.rb +16 -16
  92. data/spec/paperclip/attachment_definitions_spec.rb +5 -5
  93. data/spec/paperclip/attachment_processing_spec.rb +22 -23
  94. data/spec/paperclip/attachment_registry_spec.rb +15 -15
  95. data/spec/paperclip/attachment_spec.rb +238 -196
  96. data/spec/paperclip/content_type_detector_spec.rb +18 -12
  97. data/spec/paperclip/file_command_content_type_detector_spec.rb +10 -10
  98. data/spec/paperclip/filename_cleaner_spec.rb +3 -4
  99. data/spec/paperclip/geometry_detector_spec.rb +7 -8
  100. data/spec/paperclip/geometry_parser_spec.rb +31 -31
  101. data/spec/paperclip/geometry_spec.rb +24 -24
  102. data/spec/paperclip/glue_spec.rb +3 -5
  103. data/spec/paperclip/has_attached_file_spec.rb +46 -126
  104. data/spec/paperclip/integration_spec.rb +111 -77
  105. data/spec/paperclip/interpolations_spec.rb +101 -93
  106. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +41 -13
  107. data/spec/paperclip/io_adapters/attachment_adapter_spec.rb +8 -10
  108. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +13 -14
  109. data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +4 -4
  110. data/spec/paperclip/io_adapters/file_adapter_spec.rb +12 -12
  111. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +58 -37
  112. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +1 -1
  113. data/spec/paperclip/io_adapters/nil_adapter_spec.rb +2 -2
  114. data/spec/paperclip/io_adapters/registry_spec.rb +4 -4
  115. data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +10 -10
  116. data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +6 -6
  117. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +90 -31
  118. data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +3 -3
  119. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +4 -5
  120. data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +4 -4
  121. data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +4 -4
  122. data/spec/paperclip/media_type_spoof_detector_spec.rb +50 -24
  123. data/spec/paperclip/meta_class_spec.rb +3 -3
  124. data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +28 -24
  125. data/spec/paperclip/paperclip_spec.rb +15 -11
  126. data/spec/paperclip/plural_cache_spec.rb +8 -8
  127. data/spec/paperclip/processor_helpers_spec.rb +35 -35
  128. data/spec/paperclip/processor_spec.rb +8 -8
  129. data/spec/paperclip/rails_environment_spec.rb +7 -10
  130. data/spec/paperclip/rake_spec.rb +39 -39
  131. data/spec/paperclip/schema_spec.rb +57 -53
  132. data/spec/paperclip/storage/filesystem_spec.rb +29 -6
  133. data/spec/paperclip/storage/fog_spec.rb +122 -82
  134. data/spec/paperclip/storage/s3_live_spec.rb +22 -22
  135. data/spec/paperclip/storage/s3_spec.rb +649 -583
  136. data/spec/paperclip/style_spec.rb +67 -71
  137. data/spec/paperclip/tempfile_factory_spec.rb +5 -5
  138. data/spec/paperclip/thumbnail_spec.rb +68 -67
  139. data/spec/paperclip/url_generator_spec.rb +18 -19
  140. data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +115 -27
  141. data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +105 -16
  142. data/spec/paperclip/validators/attachment_presence_validator_spec.rb +5 -5
  143. data/spec/paperclip/validators/attachment_size_validator_spec.rb +111 -21
  144. data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +9 -13
  145. data/spec/paperclip/validators_spec.rb +61 -46
  146. data/spec/spec_helper.rb +21 -23
  147. data/spec/support/assertions.rb +8 -6
  148. data/spec/support/fake_model.rb +1 -2
  149. data/spec/support/fake_rails.rb +1 -1
  150. data/spec/support/fixtures/aws_s3.yml +13 -0
  151. data/spec/support/fixtures/sample.xlsm +0 -0
  152. data/spec/support/matchers/exist.rb +1 -1
  153. data/spec/support/matchers/have_column.rb +1 -1
  154. data/spec/support/mock_url_generator_builder.rb +2 -3
  155. data/spec/support/model_reconstruction.rb +16 -12
  156. data/spec/support/reporting.rb +1 -1
  157. data/spec/support/test_data.rb +2 -2
  158. metadata +58 -106
  159. data/spec/support/conditional_filter_helper.rb +0 -5
@@ -1,5 +1,4 @@
1
- # encoding: utf-8
2
- require 'spec_helper'
1
+ require "spec_helper"
3
2
 
4
3
  describe Paperclip::UrlGenerator do
5
4
  it "uses the given interpolator" do
@@ -25,20 +24,20 @@ describe Paperclip::UrlGenerator do
25
24
  url_generator.for(:style_name, {})
26
25
 
27
26
  assert mock_interpolator.has_interpolated_pattern?(default_url),
28
- "expected the interpolator to be passed #{default_url.inspect} but it wasn't"
27
+ "expected the interpolator to be passed #{default_url.inspect} but it wasn't"
29
28
  end
30
29
 
31
30
  it "executes the default URL lambda when no file is assigned" do
32
31
  mock_interpolator = MockInterpolator.new
33
- default_url = lambda {|attachment| "the #{attachment.class.name} default url" }
34
- options = { interpolator: mock_interpolator, default_url: default_url}
32
+ default_url = lambda { |attachment| "the #{attachment.class.name} default url" }
33
+ options = { interpolator: mock_interpolator, default_url: default_url }
35
34
  mock_attachment = MockAttachment.new(options)
36
35
 
37
36
  url_generator = Paperclip::UrlGenerator.new(mock_attachment)
38
37
  url_generator.for(:style_name, {})
39
38
 
40
39
  assert mock_interpolator.has_interpolated_pattern?("the MockAttachment default url"),
41
- %{expected the interpolator to be passed "the MockAttachment default url", but it wasn't}
40
+ %{expected the interpolator to be passed "the MockAttachment default url", but it wasn't}
42
41
  end
43
42
 
44
43
  it "executes the method named by the symbol as the default URL when no file is assigned" do
@@ -56,7 +55,7 @@ describe Paperclip::UrlGenerator do
56
55
  url_generator.for(:style_name, {})
57
56
 
58
57
  assert mock_interpolator.has_interpolated_pattern?(mock_model.to_s),
59
- %{expected the interpolator to be passed #{mock_model.to_s}, but it wasn't}
58
+ %{expected the interpolator to be passed #{mock_model}, but it wasn't}
60
59
  end
61
60
 
62
61
  it "URL-escapes spaces if asked to" do
@@ -66,7 +65,7 @@ describe Paperclip::UrlGenerator do
66
65
  mock_attachment = MockAttachment.new(options)
67
66
  url_generator = Paperclip::UrlGenerator.new(mock_attachment)
68
67
 
69
- result = url_generator.for(:style_name, {escape: true})
68
+ result = url_generator.for(:style_name, escape: true)
70
69
 
71
70
  assert_equal "the%20expected%20result", result
72
71
  end
@@ -82,7 +81,7 @@ describe Paperclip::UrlGenerator do
82
81
  mock_attachment = MockAttachment.new(options)
83
82
  url_generator = Paperclip::UrlGenerator.new(mock_attachment)
84
83
 
85
- result = url_generator.for(:style_name, {escape: true})
84
+ result = url_generator.for(:style_name, escape: true)
86
85
 
87
86
  assert_equal "the escaped result", result
88
87
  end
@@ -94,7 +93,7 @@ describe Paperclip::UrlGenerator do
94
93
  mock_attachment = MockAttachment.new(options)
95
94
  url_generator = Paperclip::UrlGenerator.new(mock_attachment)
96
95
 
97
- result = url_generator.for(:style_name, {escape: false})
96
+ result = url_generator.for(:style_name, escape: false)
98
97
 
99
98
  assert_equal "the expected result", result
100
99
  end
@@ -118,7 +117,7 @@ describe Paperclip::UrlGenerator do
118
117
  mock_attachment = MockAttachment.new(options)
119
118
  url_generator = Paperclip::UrlGenerator.new(mock_attachment)
120
119
 
121
- result = url_generator.for(:style_name, {timestamp: true})
120
+ result = url_generator.for(:style_name, timestamp: true)
122
121
 
123
122
  assert_equal expected, result
124
123
  end
@@ -134,7 +133,7 @@ describe Paperclip::UrlGenerator do
134
133
  mock_attachment = MockAttachment.new(options)
135
134
  url_generator = Paperclip::UrlGenerator.new(mock_attachment)
136
135
 
137
- result = url_generator.for(:style_name, {timestamp: true})
136
+ result = url_generator.for(:style_name, timestamp: true)
138
137
 
139
138
  assert_equal expected, result
140
139
  end
@@ -147,7 +146,7 @@ describe Paperclip::UrlGenerator do
147
146
  mock_attachment = MockAttachment.new(options)
148
147
  url_generator = Paperclip::UrlGenerator.new(mock_attachment)
149
148
 
150
- result = url_generator.for(:style_name, {timestamp: true})
149
+ result = url_generator.for(:style_name, timestamp: true)
151
150
 
152
151
  assert_equal "#{expected}?#{updated_at}", result
153
152
  end
@@ -160,7 +159,7 @@ describe Paperclip::UrlGenerator do
160
159
  mock_attachment = MockAttachment.new(options)
161
160
  url_generator = Paperclip::UrlGenerator.new(mock_attachment)
162
161
 
163
- result = url_generator.for(:style_name, {timestamp: true})
162
+ result = url_generator.for(:style_name, timestamp: true)
164
163
 
165
164
  assert_equal "#{expected}&#{updated_at}", result
166
165
  end
@@ -173,7 +172,7 @@ describe Paperclip::UrlGenerator do
173
172
  mock_attachment = MockAttachment.new(options)
174
173
  url_generator = Paperclip::UrlGenerator.new(mock_attachment)
175
174
 
176
- result = url_generator.for(:style_name, {timestamp: false})
175
+ result = url_generator.for(:style_name, timestamp: false)
177
176
 
178
177
  assert_equal expected, result
179
178
  end
@@ -192,7 +191,7 @@ describe Paperclip::UrlGenerator do
192
191
  url_generator.for(:style_name, {})
193
192
 
194
193
  assert mock_interpolator.has_interpolated_pattern?(expected),
195
- "expected the interpolator to be passed #{expected.inspect} but it wasn't"
194
+ "expected the interpolator to be passed #{expected.inspect} but it wasn't"
196
195
  end
197
196
 
198
197
  it "doesn't emit deprecation warnings" do
@@ -206,7 +205,7 @@ describe Paperclip::UrlGenerator do
206
205
  end
207
206
 
208
207
  describe "should be able to escape (, ), [, and ]." do
209
- def generate(expected, updated_at=nil)
208
+ def generate(expected, updated_at = nil)
210
209
  mock_interpolator = MockInterpolator.new(result: expected)
211
210
  options = { interpolator: mock_interpolator, updated_at: updated_at }
212
211
  mock_attachment = MockAttachment.new(options)
@@ -214,7 +213,7 @@ describe Paperclip::UrlGenerator do
214
213
  def url_generator.respond_to(params)
215
214
  false if params == :escape
216
215
  end
217
- url_generator.for(:style_name, {escape: true, timestamp: !!updated_at})
216
+ url_generator.for(:style_name, escape: true, timestamp: !!updated_at)
218
217
  end
219
218
 
220
219
  it "not timestamp" do
@@ -226,7 +225,7 @@ describe Paperclip::UrlGenerator do
226
225
  expected = "the(expected)result[]"
227
226
  updated_at = 1231231234
228
227
  assert_equal "the%28expected%29result%5B%5D?#{updated_at}",
229
- generate(expected, updated_at)
228
+ generate(expected, updated_at)
230
229
  end
231
230
  end
232
231
  end
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  describe Paperclip::Validators::AttachmentContentTypeValidator do
4
4
  before do
@@ -8,14 +8,14 @@ describe Paperclip::Validators::AttachmentContentTypeValidator do
8
8
 
9
9
  def build_validator(options)
10
10
  @validator = Paperclip::Validators::AttachmentContentTypeValidator.new(options.merge(
11
- attributes: :avatar
12
- ))
11
+ attributes: :avatar
12
+ ))
13
13
  end
14
14
 
15
15
  context "with a nil content type" do
16
16
  before do
17
17
  build_validator content_type: "image/jpg"
18
- @dummy.stubs(avatar_content_type: nil)
18
+ allow(@dummy).to receive_messages(avatar_content_type: nil)
19
19
  @validator.validate(@dummy)
20
20
  end
21
21
 
@@ -28,7 +28,7 @@ describe Paperclip::Validators::AttachmentContentTypeValidator do
28
28
  context "as true" do
29
29
  before do
30
30
  build_validator content_type: "image/png", allow_nil: true
31
- @dummy.stubs(avatar_content_type: nil)
31
+ allow(@dummy).to receive_messages(avatar_content_type: nil)
32
32
  @validator.validate(@dummy)
33
33
  end
34
34
 
@@ -40,7 +40,7 @@ describe Paperclip::Validators::AttachmentContentTypeValidator do
40
40
  context "as false" do
41
41
  before do
42
42
  build_validator content_type: "image/png", allow_nil: false
43
- @dummy.stubs(avatar_content_type: nil)
43
+ allow(@dummy).to receive_messages(avatar_content_type: nil)
44
44
  @validator.validate(@dummy)
45
45
  end
46
46
 
@@ -53,13 +53,13 @@ describe Paperclip::Validators::AttachmentContentTypeValidator do
53
53
  context "with a failing validation" do
54
54
  before do
55
55
  build_validator content_type: "image/png", allow_nil: false
56
- @dummy.stubs(avatar_content_type: nil)
56
+ allow(@dummy).to receive_messages(avatar_content_type: nil)
57
57
  @validator.validate(@dummy)
58
58
  end
59
59
 
60
60
  it "adds error to the base object" do
61
61
  assert @dummy.errors[:avatar].present?,
62
- "Error not added to base attribute"
62
+ "Error not added to base attribute"
63
63
  end
64
64
 
65
65
  it "adds error to base object as a string" do
@@ -67,16 +67,104 @@ describe Paperclip::Validators::AttachmentContentTypeValidator do
67
67
  end
68
68
  end
69
69
 
70
+ context "with add_validation_errors_to not set (implicitly :both)" do
71
+ it "adds error to both attribute and base" do
72
+ build_validator content_type: "image/png", allow_nil: false
73
+ allow(@dummy).to receive_messages(avatar_content_type: nil)
74
+ @validator.validate(@dummy)
75
+
76
+ assert @dummy.errors[:avatar_content_type].present?,
77
+ "Error not added to attribute"
78
+
79
+ assert @dummy.errors[:avatar].present?,
80
+ "Error not added to base attribute"
81
+ end
82
+ end
83
+
84
+ context "with add_validation_errors_to set to :attribute globally" do
85
+ before do
86
+ Paperclip.options[:add_validation_errors_to] = :attribute
87
+ end
88
+
89
+ after do
90
+ Paperclip.options[:add_validation_errors_to] = :both
91
+ end
92
+
93
+ it "only adds error to attribute not base" do
94
+ build_validator content_type: "image/png", allow_nil: false
95
+ allow(@dummy).to receive_messages(avatar_content_type: nil)
96
+ @validator.validate(@dummy)
97
+
98
+ assert @dummy.errors[:avatar_content_type].present?,
99
+ "Error not added to attribute"
100
+
101
+ assert @dummy.errors[:avatar].blank?,
102
+ "Error added to base attribute"
103
+ end
104
+ end
105
+
106
+ context "with add_validation_errors_to set to :base globally" do
107
+ before do
108
+ Paperclip.options[:add_validation_errors_to] = :base
109
+ end
110
+
111
+ after do
112
+ Paperclip.options[:add_validation_errors_to] = :both
113
+ end
114
+
115
+ it "only adds error to base not attribute" do
116
+ build_validator content_type: "image/png", allow_nil: false
117
+ allow(@dummy).to receive_messages(avatar_content_type: nil)
118
+ @validator.validate(@dummy)
119
+
120
+ assert @dummy.errors[:avatar].present?,
121
+ "Error not added to base attribute"
122
+
123
+ assert @dummy.errors[:avatar_content_type].blank?,
124
+ "Error added to attribute"
125
+ end
126
+ end
127
+
128
+ context "with add_validation_errors_to set to :attribute" do
129
+ it "only adds error to attribute not base" do
130
+ build_validator content_type: "image/png", allow_nil: false,
131
+ add_validation_errors_to: :attribute
132
+ allow(@dummy).to receive_messages(avatar_content_type: nil)
133
+ @validator.validate(@dummy)
134
+
135
+ assert @dummy.errors[:avatar_content_type].present?,
136
+ "Error not added to attribute"
137
+
138
+ assert @dummy.errors[:avatar].blank?,
139
+ "Error added to base attribute"
140
+ end
141
+ end
142
+
143
+ context "with add_validation_errors_to set to :base" do
144
+ it "only adds error to base not attribute" do
145
+ build_validator content_type: "image/png", allow_nil: false,
146
+ add_validation_errors_to: :base
147
+ allow(@dummy).to receive_messages(avatar_content_type: nil)
148
+ @validator.validate(@dummy)
149
+
150
+ assert @dummy.errors[:avatar].present?,
151
+ "Error not added to base attribute"
152
+
153
+ assert @dummy.errors[:avatar_content_type].blank?,
154
+ "Error added to attribute"
155
+ end
156
+ end
157
+
70
158
  context "with a successful validation" do
71
159
  before do
72
160
  build_validator content_type: "image/png", allow_nil: false
73
- @dummy.stubs(avatar_content_type: "image/png")
161
+ allow(@dummy).to receive_messages(avatar_content_type: "image/png")
74
162
  @validator.validate(@dummy)
75
163
  end
76
164
 
77
165
  it "does not add error to the base object" do
78
166
  assert @dummy.errors[:avatar].blank?,
79
- "Error was added to base attribute"
167
+ "Error was added to base attribute"
80
168
  end
81
169
  end
82
170
 
@@ -84,7 +172,7 @@ describe Paperclip::Validators::AttachmentContentTypeValidator do
84
172
  context "as true" do
85
173
  before do
86
174
  build_validator content_type: "image/png", allow_blank: true
87
- @dummy.stubs(avatar_content_type: "")
175
+ allow(@dummy).to receive_messages(avatar_content_type: "")
88
176
  @validator.validate(@dummy)
89
177
  end
90
178
 
@@ -96,7 +184,7 @@ describe Paperclip::Validators::AttachmentContentTypeValidator do
96
184
  context "as false" do
97
185
  before do
98
186
  build_validator content_type: "image/png", allow_blank: false
99
- @dummy.stubs(avatar_content_type: "")
187
+ allow(@dummy).to receive_messages(avatar_content_type: "")
100
188
  @validator.validate(@dummy)
101
189
  end
102
190
 
@@ -111,7 +199,7 @@ describe Paperclip::Validators::AttachmentContentTypeValidator do
111
199
  context "as a string" do
112
200
  before do
113
201
  build_validator content_type: "image/jpg"
114
- @dummy.stubs(avatar_content_type: "image/jpg")
202
+ allow(@dummy).to receive_messages(avatar_content_type: "image/jpg")
115
203
  @validator.validate(@dummy)
116
204
  end
117
205
 
@@ -123,7 +211,7 @@ describe Paperclip::Validators::AttachmentContentTypeValidator do
123
211
  context "as an regexp" do
124
212
  before do
125
213
  build_validator content_type: /^image\/.*/
126
- @dummy.stubs(avatar_content_type: "image/jpg")
214
+ allow(@dummy).to receive_messages(avatar_content_type: "image/jpg")
127
215
  @validator.validate(@dummy)
128
216
  end
129
217
 
@@ -135,7 +223,7 @@ describe Paperclip::Validators::AttachmentContentTypeValidator do
135
223
  context "as a list" do
136
224
  before do
137
225
  build_validator content_type: ["image/png", "image/jpg", "image/jpeg"]
138
- @dummy.stubs(avatar_content_type: "image/jpg")
226
+ allow(@dummy).to receive_messages(avatar_content_type: "image/jpg")
139
227
  @validator.validate(@dummy)
140
228
  end
141
229
 
@@ -149,7 +237,7 @@ describe Paperclip::Validators::AttachmentContentTypeValidator do
149
237
  context "as a string" do
150
238
  before do
151
239
  build_validator content_type: "image/png"
152
- @dummy.stubs(avatar_content_type: "image/jpg")
240
+ allow(@dummy).to receive_messages(avatar_content_type: "image/jpg")
153
241
  @validator.validate(@dummy)
154
242
  end
155
243
 
@@ -162,7 +250,7 @@ describe Paperclip::Validators::AttachmentContentTypeValidator do
162
250
  context "as a regexp" do
163
251
  before do
164
252
  build_validator content_type: /^text\/.*/
165
- @dummy.stubs(avatar_content_type: "image/jpg")
253
+ allow(@dummy).to receive_messages(avatar_content_type: "image/jpg")
166
254
  @validator.validate(@dummy)
167
255
  end
168
256
 
@@ -176,7 +264,7 @@ describe Paperclip::Validators::AttachmentContentTypeValidator do
176
264
  context "without interpolation" do
177
265
  before do
178
266
  build_validator content_type: "image/png", message: "should be a PNG image"
179
- @dummy.stubs(avatar_content_type: "image/jpg")
267
+ allow(@dummy).to receive_messages(avatar_content_type: "image/jpg")
180
268
  @validator.validate(@dummy)
181
269
  end
182
270
 
@@ -188,7 +276,7 @@ describe Paperclip::Validators::AttachmentContentTypeValidator do
188
276
  context "with interpolation" do
189
277
  before do
190
278
  build_validator content_type: "image/png", message: "should have content type %{types}"
191
- @dummy.stubs(avatar_content_type: "image/jpg")
279
+ allow(@dummy).to receive_messages(avatar_content_type: "image/jpg")
192
280
  @validator.validate(@dummy)
193
281
  end
194
282
 
@@ -205,7 +293,7 @@ describe Paperclip::Validators::AttachmentContentTypeValidator do
205
293
  context "as a string" do
206
294
  before do
207
295
  build_validator not: "image/gif"
208
- @dummy.stubs(avatar_content_type: "image/jpg")
296
+ allow(@dummy).to receive_messages(avatar_content_type: "image/jpg")
209
297
  @validator.validate(@dummy)
210
298
  end
211
299
 
@@ -217,7 +305,7 @@ describe Paperclip::Validators::AttachmentContentTypeValidator do
217
305
  context "as an regexp" do
218
306
  before do
219
307
  build_validator not: /^text\/.*/
220
- @dummy.stubs(avatar_content_type: "image/jpg")
308
+ allow(@dummy).to receive_messages(avatar_content_type: "image/jpg")
221
309
  @validator.validate(@dummy)
222
310
  end
223
311
 
@@ -229,7 +317,7 @@ describe Paperclip::Validators::AttachmentContentTypeValidator do
229
317
  context "as a list" do
230
318
  before do
231
319
  build_validator not: ["image/png", "image/jpg", "image/jpeg"]
232
- @dummy.stubs(avatar_content_type: "image/gif")
320
+ allow(@dummy).to receive_messages(avatar_content_type: "image/gif")
233
321
  @validator.validate(@dummy)
234
322
  end
235
323
 
@@ -243,7 +331,7 @@ describe Paperclip::Validators::AttachmentContentTypeValidator do
243
331
  context "as a string" do
244
332
  before do
245
333
  build_validator not: "image/png"
246
- @dummy.stubs(avatar_content_type: "image/png")
334
+ allow(@dummy).to receive_messages(avatar_content_type: "image/png")
247
335
  @validator.validate(@dummy)
248
336
  end
249
337
 
@@ -256,7 +344,7 @@ describe Paperclip::Validators::AttachmentContentTypeValidator do
256
344
  context "as a regexp" do
257
345
  before do
258
346
  build_validator not: /^text\/.*/
259
- @dummy.stubs(avatar_content_type: "text/plain")
347
+ allow(@dummy).to receive_messages(avatar_content_type: "text/plain")
260
348
  @validator.validate(@dummy)
261
349
  end
262
350
 
@@ -270,7 +358,7 @@ describe Paperclip::Validators::AttachmentContentTypeValidator do
270
358
  context "without interpolation" do
271
359
  before do
272
360
  build_validator not: "image/png", message: "should not be a PNG image"
273
- @dummy.stubs(avatar_content_type: "image/png")
361
+ allow(@dummy).to receive_messages(avatar_content_type: "image/png")
274
362
  @validator.validate(@dummy)
275
363
  end
276
364
 
@@ -282,7 +370,7 @@ describe Paperclip::Validators::AttachmentContentTypeValidator do
282
370
  context "with interpolation" do
283
371
  before do
284
372
  build_validator not: "image/png", message: "should not have content type %{types}"
285
- @dummy.stubs(avatar_content_type: "image/png")
373
+ allow(@dummy).to receive_messages(avatar_content_type: "image/png")
286
374
  @validator.validate(@dummy)
287
375
  end
288
376
 
@@ -300,7 +388,7 @@ describe Paperclip::Validators::AttachmentContentTypeValidator do
300
388
  end
301
389
 
302
390
  it "adds the validator to the class" do
303
- assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_content_type }
391
+ assert Dummy.validators_on(:avatar).any? { |validator| validator.kind == :attachment_content_type }
304
392
  end
305
393
  end
306
394
 
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  describe Paperclip::Validators::AttachmentFileNameValidator do
4
4
  before do
@@ -8,20 +8,20 @@ describe Paperclip::Validators::AttachmentFileNameValidator do
8
8
 
9
9
  def build_validator(options)
10
10
  @validator = Paperclip::Validators::AttachmentFileNameValidator.new(options.merge(
11
- attributes: :avatar
12
- ))
11
+ attributes: :avatar
12
+ ))
13
13
  end
14
14
 
15
15
  context "with a failing validation" do
16
16
  before do
17
17
  build_validator matches: /.*\.png$/, allow_nil: false
18
- @dummy.stubs(avatar_file_name: "data.txt")
18
+ allow(@dummy).to receive_messages(avatar_file_name: "data.txt")
19
19
  @validator.validate(@dummy)
20
20
  end
21
21
 
22
22
  it "adds error to the base object" do
23
23
  assert @dummy.errors[:avatar].present?,
24
- "Error not added to base attribute"
24
+ "Error not added to base attribute"
25
25
  end
26
26
 
27
27
  it "adds error to base object as a string" do
@@ -29,9 +29,99 @@ describe Paperclip::Validators::AttachmentFileNameValidator do
29
29
  end
30
30
  end
31
31
 
32
+ context "with add_validation_errors_to not set (implicitly :both)" do
33
+ it "adds error to both attribute and base" do
34
+ build_validator matches: /.*\.png$/, allow_nil: false
35
+ allow(@dummy).to receive_messages(avatar_file_name: "data.txt")
36
+ @validator.validate(@dummy)
37
+
38
+ assert @dummy.errors[:avatar_file_name].present?,
39
+ "Error not added to attribute"
40
+
41
+ assert @dummy.errors[:avatar].present?,
42
+ "Error not added to base attribute"
43
+ end
44
+ end
45
+
46
+ context "with add_validation_errors_to set to :attribute globally" do
47
+ before do
48
+ Paperclip.options[:add_validation_errors_to] = :attribute
49
+ end
50
+
51
+ after do
52
+ Paperclip.options[:add_validation_errors_to] = :both
53
+ end
54
+
55
+ it "only adds error to attribute not base" do
56
+ build_validator matches: /.*\.png$/, allow_nil: false
57
+ allow(@dummy).to receive_messages(avatar_file_name: "data.txt")
58
+ @validator.validate(@dummy)
59
+
60
+ assert @dummy.errors[:avatar_file_name].present?,
61
+ "Error not added to attribute"
62
+
63
+ assert @dummy.errors[:avatar].blank?,
64
+ "Error added to base attribute"
65
+ end
66
+ end
67
+
68
+ context "with add_validation_errors_to set to :base globally" do
69
+ before do
70
+ Paperclip.options[:add_validation_errors_to] = :base
71
+ end
72
+
73
+ after do
74
+ Paperclip.options[:add_validation_errors_to] = :both
75
+ end
76
+
77
+ it "only adds error to base not attribute" do
78
+ build_validator matches: /.*\.png$/, allow_nil: false
79
+ allow(@dummy).to receive_messages(avatar_file_name: "data.txt")
80
+ @validator.validate(@dummy)
81
+
82
+ assert @dummy.errors[:avatar].present?,
83
+ "Error not added to base attribute"
84
+
85
+ assert @dummy.errors[:avatar_file_name].blank?,
86
+ "Error added to attribute"
87
+ end
88
+ end
89
+
90
+ context "with add_validation_errors_to set to :attribute" do
91
+ it "only adds error to attribute not base" do
92
+ build_validator matches: /.*\.png$/, allow_nil: false,
93
+ add_validation_errors_to: :attribute
94
+
95
+ allow(@dummy).to receive_messages(avatar_file_name: "data.txt")
96
+ @validator.validate(@dummy)
97
+
98
+ assert @dummy.errors[:avatar_file_name].present?,
99
+ "Error not added to attribute"
100
+
101
+ assert @dummy.errors[:avatar].blank?,
102
+ "Error added to base attribute"
103
+ end
104
+ end
105
+
106
+ context "with add_validation_errors_to set to :base" do
107
+ it "only adds error to base not attribute" do
108
+ build_validator matches: /.*\.png$/, allow_nil: false,
109
+ add_validation_errors_to: :base
110
+
111
+ allow(@dummy).to receive_messages(avatar_file_name: "data.txt")
112
+ @validator.validate(@dummy)
113
+
114
+ assert @dummy.errors[:avatar].present?,
115
+ "Error not added to base attribute"
116
+
117
+ assert @dummy.errors[:avatar_file_name].blank?,
118
+ "Error added to attribute"
119
+ end
120
+ end
121
+
32
122
  it "does not add error to the base object with a successful validation" do
33
123
  build_validator matches: /.*\.png$/, allow_nil: false
34
- @dummy.stubs(avatar_file_name: "image.png")
124
+ allow(@dummy).to receive_messages(avatar_file_name: "image.png")
35
125
  @validator.validate(@dummy)
36
126
 
37
127
  assert @dummy.errors[:avatar].blank?, "Error was added to base attribute"
@@ -42,7 +132,7 @@ describe Paperclip::Validators::AttachmentFileNameValidator do
42
132
  context "as a single regexp" do
43
133
  before do
44
134
  build_validator matches: /.*\.jpg$/
45
- @dummy.stubs(avatar_file_name: "image.jpg")
135
+ allow(@dummy).to receive_messages(avatar_file_name: "image.jpg")
46
136
  @validator.validate(@dummy)
47
137
  end
48
138
 
@@ -54,7 +144,7 @@ describe Paperclip::Validators::AttachmentFileNameValidator do
54
144
  context "as a list" do
55
145
  before do
56
146
  build_validator matches: [/.*\.png$/, /.*\.jpe?g$/]
57
- @dummy.stubs(avatar_file_name: "image.jpg")
147
+ allow(@dummy).to receive_messages(avatar_file_name: "image.jpg")
58
148
  @validator.validate(@dummy)
59
149
  end
60
150
 
@@ -67,7 +157,7 @@ describe Paperclip::Validators::AttachmentFileNameValidator do
67
157
  context "with a disallowed type" do
68
158
  it "sets a correct default error message" do
69
159
  build_validator matches: /^text\/.*/
70
- @dummy.stubs(avatar_file_name: "image.jpg")
160
+ allow(@dummy).to receive_messages(avatar_file_name: "image.jpg")
71
161
  @validator.validate(@dummy)
72
162
 
73
163
  assert @dummy.errors[:avatar_file_name].present?
@@ -76,7 +166,7 @@ describe Paperclip::Validators::AttachmentFileNameValidator do
76
166
 
77
167
  it "sets a correct custom error message" do
78
168
  build_validator matches: /.*\.png$/, message: "should be a PNG image"
79
- @dummy.stubs(avatar_file_name: "image.jpg")
169
+ allow(@dummy).to receive_messages(avatar_file_name: "image.jpg")
80
170
  @validator.validate(@dummy)
81
171
 
82
172
  expect(@dummy.errors[:avatar_file_name]).to include "should be a PNG image"
@@ -89,7 +179,7 @@ describe Paperclip::Validators::AttachmentFileNameValidator do
89
179
  context "as a single regexp" do
90
180
  before do
91
181
  build_validator not: /^text\/.*/
92
- @dummy.stubs(avatar_file_name: "image.jpg")
182
+ allow(@dummy).to receive_messages(avatar_file_name: "image.jpg")
93
183
  @validator.validate(@dummy)
94
184
  end
95
185
 
@@ -101,7 +191,7 @@ describe Paperclip::Validators::AttachmentFileNameValidator do
101
191
  context "as a list" do
102
192
  before do
103
193
  build_validator not: [/.*\.png$/, /.*\.jpe?g$/]
104
- @dummy.stubs(avatar_file_name: "image.gif")
194
+ allow(@dummy).to receive_messages(avatar_file_name: "image.gif")
105
195
  @validator.validate(@dummy)
106
196
  end
107
197
 
@@ -114,7 +204,7 @@ describe Paperclip::Validators::AttachmentFileNameValidator do
114
204
  context "with a disallowed type" do
115
205
  it "sets a correct default error message" do
116
206
  build_validator not: /data.*/
117
- @dummy.stubs(avatar_file_name: "data.txt")
207
+ allow(@dummy).to receive_messages(avatar_file_name: "data.txt")
118
208
  @validator.validate(@dummy)
119
209
 
120
210
  assert @dummy.errors[:avatar_file_name].present?
@@ -123,7 +213,7 @@ describe Paperclip::Validators::AttachmentFileNameValidator do
123
213
 
124
214
  it "sets a correct custom error message" do
125
215
  build_validator not: /.*\.png$/, message: "should not be a PNG image"
126
- @dummy.stubs(avatar_file_name: "image.png")
216
+ allow(@dummy).to receive_messages(avatar_file_name: "image.png")
127
217
  @validator.validate(@dummy)
128
218
 
129
219
  expect(@dummy.errors[:avatar_file_name]).to include "should not be a PNG image"
@@ -137,7 +227,7 @@ describe Paperclip::Validators::AttachmentFileNameValidator do
137
227
  end
138
228
 
139
229
  it "adds the validator to the class" do
140
- assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_file_name }
230
+ assert Dummy.validators_on(:avatar).any? { |validator| validator.kind == :attachment_file_name }
141
231
  end
142
232
  end
143
233
 
@@ -157,4 +247,3 @@ describe Paperclip::Validators::AttachmentFileNameValidator do
157
247
  end
158
248
  end
159
249
  end
160
-