kt-paperclip 6.2.0 → 7.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) 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 -1050
  7. data/.rubocop.yml +1061 -1
  8. data/.travis.yml +12 -14
  9. data/Appraisals +6 -0
  10. data/CONTRIBUTING.md +4 -5
  11. data/Gemfile +5 -4
  12. data/NEWS +31 -0
  13. data/README.md +41 -19
  14. data/features/step_definitions/attachment_steps.rb +11 -1
  15. data/gemfiles/4.2.gemfile +1 -1
  16. data/gemfiles/5.0.gemfile +1 -1
  17. data/gemfiles/5.1.gemfile +1 -1
  18. data/gemfiles/5.2.gemfile +1 -1
  19. data/gemfiles/6.0.gemfile +1 -1
  20. data/gemfiles/6.1.gemfile +21 -0
  21. data/gemfiles/7.0.gemfile +21 -0
  22. data/lib/kt-paperclip.rb +1 -0
  23. data/lib/paperclip/attachment.rb +1 -1
  24. data/lib/paperclip/content_type_detector.rb +10 -5
  25. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +2 -2
  26. data/lib/paperclip/io_adapters/uri_adapter.rb +13 -3
  27. data/lib/paperclip/schema.rb +2 -2
  28. data/lib/paperclip/storage/filesystem.rb +1 -1
  29. data/lib/paperclip/storage/fog.rb +1 -1
  30. data/lib/paperclip/storage/s3.rb +3 -3
  31. data/lib/paperclip/url_generator.rb +8 -1
  32. data/lib/paperclip/validators/attachment_content_type_validator.rb +9 -2
  33. data/lib/paperclip/validators/attachment_file_name_validator.rb +10 -3
  34. data/lib/paperclip/validators/attachment_presence_validator.rb +1 -1
  35. data/lib/paperclip/validators/attachment_size_validator.rb +21 -4
  36. data/lib/paperclip/validators.rb +4 -4
  37. data/lib/paperclip/version.rb +1 -1
  38. data/lib/paperclip.rb +3 -3
  39. data/paperclip.gemspec +4 -4
  40. data/spec/paperclip/content_type_detector_spec.rb +7 -0
  41. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +1 -1
  42. data/spec/paperclip/io_adapters/file_adapter_spec.rb +1 -1
  43. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +20 -15
  44. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +13 -3
  45. data/spec/paperclip/storage/filesystem_spec.rb +23 -0
  46. data/spec/paperclip/storage/fog_spec.rb +46 -0
  47. data/spec/paperclip/storage/s3_spec.rb +67 -3
  48. data/spec/paperclip/url_generator_spec.rb +10 -0
  49. data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +88 -0
  50. data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +90 -0
  51. data/spec/paperclip/validators/attachment_size_validator_spec.rb +90 -0
  52. data/spec/paperclip/validators_spec.rb +21 -6
  53. data/spec/support/fixtures/aws_s3.yml +13 -0
  54. data/spec/support/fixtures/sample.xlsm +0 -0
  55. data/spec/support/model_reconstruction.rb +1 -1
  56. metadata +20 -12
  57. data/.github/issue_template.md +0 -3
@@ -4,7 +4,7 @@ module Paperclip
4
4
  module Validators
5
5
  class AttachmentPresenceValidator < ActiveModel::EachValidator
6
6
  def validate_each(record, attribute, _value)
7
- record.errors.add(attribute, :blank, options) if record.send("#{attribute}_file_name").blank?
7
+ record.errors.add(attribute, :blank, **options) if record.send("#{attribute}_file_name").blank?
8
8
  end
9
9
 
10
10
  def self.helper_method_name
@@ -17,17 +17,30 @@ module Paperclip
17
17
  def validate_each(record, attr_name, value)
18
18
  base_attr_name = attr_name
19
19
  attr_name = "#{attr_name}_file_size".to_sym
20
+
21
+ error_attrs = []
22
+ case options[:add_validation_errors_to]
23
+ when :base
24
+ error_attrs << base_attr_name
25
+ when :attribute
26
+ error_attrs << attr_name
27
+ else
28
+ error_attrs << base_attr_name
29
+ error_attrs << attr_name
30
+ end
31
+
20
32
  value = record.send(:read_attribute_for_validation, attr_name)
21
33
 
22
34
  unless value.blank?
23
35
  options.slice(*AVAILABLE_CHECKS).each do |option, option_value|
24
36
  option_value = option_value.call(record) if option_value.is_a?(Proc)
25
37
  option_value = extract_option_value(option, option_value)
26
-
27
- unless value.send(CHECKS[option], option_value)
38
+ operator = Rails::VERSION::MAJOR >= 7 ? COMPARE_CHECKS[option] : CHECKS[option]
39
+
40
+ unless value.send(operator, option_value)
28
41
  error_message_key = options[:in] ? :in_between : option
29
- [attr_name, base_attr_name].each do |error_attr_name|
30
- record.errors.add(error_attr_name, error_message_key, filtered_options(value).merge(
42
+ error_attrs.each do |error_attr_name|
43
+ record.errors.add(error_attr_name, error_message_key, **filtered_options(value).merge(
31
44
  min: min_value_in_human_size(record),
32
45
  max: max_value_in_human_size(record),
33
46
  count: human_size(option_value)
@@ -56,6 +69,10 @@ module Paperclip
56
69
  options[:greater_than_or_equal_to] = range
57
70
  end
58
71
  end
72
+
73
+ unless options.key?(:add_validation_errors_to)
74
+ options[:add_validation_errors_to] = Paperclip.options[:add_validation_errors_to]
75
+ end
59
76
  end
60
77
 
61
78
  def extract_option_value(option, option_value)
@@ -20,10 +20,10 @@ module Paperclip
20
20
  ::Paperclip::REQUIRED_VALIDATORS = [AttachmentFileNameValidator, AttachmentContentTypeValidator, AttachmentFileTypeIgnoranceValidator].freeze
21
21
 
22
22
  module ClassMethods
23
- # This method is a shortcut to validator classes that is in
24
- # "Attachment...Validator" format. It is almost the same thing as the
25
- # +validates+ method that shipped with Rails, but this is customized to
26
- # be using with attachment validators. This is helpful when you're using
23
+ # This method is a shortcut to the validator classes that are in
24
+ # "Attachment...Validator" format. It is almost the same as the
25
+ # +validates+ method that ships with Rails, but is customized for
26
+ # use with attachment validators. This is helpful when you're using
27
27
  # multiple attachment validators on a single attachment.
28
28
  #
29
29
  # Example of using the validator:
@@ -1,3 +1,3 @@
1
1
  module Paperclip
2
- VERSION = "6.2.0" unless defined?(Paperclip::VERSION)
2
+ VERSION = "7.1.1" unless defined?(Paperclip::VERSION)
3
3
  end
data/lib/paperclip.rb CHANGED
@@ -64,8 +64,7 @@ rescue LoadError
64
64
  require "mime/types"
65
65
  end
66
66
 
67
- require "mimemagic"
68
- require "mimemagic/overlay"
67
+ require "marcel"
69
68
  require "logger"
70
69
  require "terrapin"
71
70
 
@@ -98,7 +97,8 @@ module Paperclip
98
97
  swallow_stderr: true,
99
98
  use_exif_orientation: true,
100
99
  whiny: true,
101
- is_windows: Gem.win_platform?
100
+ is_windows: Gem.win_platform?,
101
+ add_validation_errors_to: :both
102
102
  }
103
103
  end
104
104
 
data/paperclip.gemspec CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.author = "Surendra Singhi"
9
9
  s.email = ["ssinghi@kreeti.com"]
10
- s.homepage = "https://github.com/kreeti/paperclip"
10
+ s.homepage = "https://github.com/kreeti/kt-paperclip"
11
11
  s.summary = "File attachments as attributes for ActiveRecord"
12
12
  s.description = "Easy upload management for ActiveRecord"
13
13
  s.license = "MIT"
@@ -20,12 +20,12 @@ Gem::Specification.new do |s|
20
20
  s.post_install_message = File.read("UPGRADING") if File.exist?("UPGRADING")
21
21
 
22
22
  s.requirements << "ImageMagick"
23
- s.required_ruby_version = ">= 2.1.0"
23
+ s.required_ruby_version = ">= 2.3.0"
24
24
 
25
25
  s.add_dependency("activemodel", ">= 4.2.0")
26
26
  s.add_dependency("activesupport", ">= 4.2.0")
27
27
  s.add_dependency("mime-types")
28
- s.add_dependency("mimemagic", "~> 0.3.0")
28
+ s.add_dependency("marcel", "~> 1.0.1")
29
29
  s.add_dependency("terrapin", "~> 0.6.0")
30
30
 
31
31
  s.add_development_dependency("activerecord", ">= 4.2.0")
@@ -34,7 +34,7 @@ Gem::Specification.new do |s|
34
34
  s.add_development_dependency("aws-sdk-s3")
35
35
  s.add_development_dependency("bundler")
36
36
  s.add_development_dependency("capybara")
37
- s.add_development_dependency("cucumber-expressions", "4.0.3") # TODO: investigate failures on 4.0.4
37
+ s.add_development_dependency("cucumber-expressions")
38
38
  s.add_development_dependency("cucumber-rails")
39
39
  s.add_development_dependency("fakeweb")
40
40
  s.add_development_dependency("fog-aws")
@@ -7,6 +7,13 @@ describe Paperclip::ContentTypeDetector do
7
7
  Paperclip::ContentTypeDetector.new(file.path).detect
8
8
  end
9
9
 
10
+ it 'returns a more specific content type based on the filename if it matches
11
+ multiple content types' do
12
+ file = File.new(fixture_file('sample.xlsm'))
13
+ assert_equal 'application/vnd.ms-excel.sheet.macroenabled.12',
14
+ Paperclip::ContentTypeDetector.new(file.path).detect
15
+ end
16
+
10
17
  it "gives a sensible default when the name is empty" do
11
18
  assert_equal "application/octet-stream", Paperclip::ContentTypeDetector.new("").detect
12
19
  end
@@ -15,7 +15,7 @@ describe Paperclip::AbstractAdapter do
15
15
  before do
16
16
  allow(subject).to receive(:path).and_return("image.png")
17
17
  allow(Paperclip).to receive(:run).and_return("image/png\n")
18
- allow_any_instance_of(Paperclip::ContentTypeDetector).to receive(:type_from_mime_magic).and_return("image/png")
18
+ allow_any_instance_of(Paperclip::ContentTypeDetector).to receive(:type_from_marcel).and_return("image/png")
19
19
  end
20
20
 
21
21
  it "returns the content type without newline" do
@@ -78,7 +78,7 @@ describe Paperclip::FileAdapter do
78
78
  allow(MIME::Types).to receive(:type_for).and_return([])
79
79
  allow(Paperclip).to receive(:run).and_return("application/vnd.ms-office\n")
80
80
  allow_any_instance_of(Paperclip::ContentTypeDetector).
81
- to receive(:type_from_mime_magic).and_return("application/vnd.ms-office")
81
+ to receive(:type_from_marcel).and_return("application/vnd.ms-office")
82
82
 
83
83
  @subject = Paperclip.io_adapters.for(@file)
84
84
  end
@@ -16,54 +16,59 @@ describe Paperclip::HttpUrlProxyAdapter do
16
16
  context "a new instance" do
17
17
  before do
18
18
  @url = "http://thoughtbot.com/images/thoughtbot-logo.png"
19
- @subject = Paperclip.io_adapters.for(@url, hash_digest: Digest::MD5)
20
19
  end
21
20
 
21
+ subject { Paperclip.io_adapters.for(@url, hash_digest: Digest::MD5) }
22
+
22
23
  after do
23
- @subject.close
24
+ subject.close
24
25
  end
25
26
 
26
27
  it "returns a file name" do
27
- assert_equal "thoughtbot-logo.png", @subject.original_filename
28
+ expect(subject.original_filename).to(eq("thoughtbot-logo.png"))
28
29
  end
29
30
 
30
31
  it "closes open handle after reading" do
31
- assert_equal true, @open_return.closed?
32
+ expect { subject }.to(change { @open_return.closed? }.from(false).to(true))
32
33
  end
33
34
 
34
35
  it "returns a content type" do
35
- assert_equal "image/png", @subject.content_type
36
+ expect(subject.content_type).to(eq("image/png"))
36
37
  end
37
38
 
38
39
  it "returns the size of the data" do
39
- assert_equal @open_return.size, @subject.size
40
+ expect(subject.size).to(eq(@open_return.size))
40
41
  end
41
42
 
42
43
  it "generates an MD5 hash of the contents" do
43
- assert_equal Digest::MD5.hexdigest("xxx"), @subject.fingerprint
44
+ expect(subject.fingerprint).to(eq(Digest::MD5.hexdigest("xxx")))
44
45
  end
45
46
 
46
47
  it "generates correct fingerprint after read" do
47
- fingerprint = Digest::MD5.hexdigest(@subject.read)
48
- assert_equal fingerprint, @subject.fingerprint
48
+ fingerprint = Digest::MD5.hexdigest(subject.read)
49
+ expect(subject.fingerprint).to(eq(fingerprint))
49
50
  end
50
51
 
51
52
  it "generates same fingerprint" do
52
- assert_equal @subject.fingerprint, @subject.fingerprint
53
+ expect(subject.fingerprint).to(eq(subject.fingerprint))
53
54
  end
54
55
 
55
56
  it "returns the data contained in the StringIO" do
56
- assert_equal "xxx", @subject.read
57
+ expect(subject.read).to(eq("xxx"))
57
58
  end
58
59
 
59
60
  it "accepts a content_type" do
60
- @subject.content_type = "image/png"
61
- assert_equal "image/png", @subject.content_type
61
+ subject.content_type = "image/png"
62
+ expect(subject.content_type).to(eq("image/png"))
62
63
  end
63
64
 
64
65
  it "accepts an original_filename" do
65
- @subject.original_filename = "image.png"
66
- assert_equal "image.png", @subject.original_filename
66
+ subject.original_filename = "image.png"
67
+ expect(subject.original_filename).to(eq("image.png"))
68
+ end
69
+
70
+ it "doesn't emit deprecation warnings" do
71
+ expect { subject }.to_not(output(/URI\.(un)?escape is obsolete/).to_stderr)
67
72
  end
68
73
  end
69
74
 
@@ -193,9 +193,18 @@ describe Paperclip::UriAdapter do
193
193
 
194
194
  describe "#download_content" do
195
195
  before do
196
- allow_any_instance_of(Paperclip::UriAdapter).to receive(:open).and_return(@open_return)
196
+ allowed_mock =
197
+ if RUBY_VERSION < '2.5'
198
+ allow_any_instance_of(Paperclip::UriAdapter)
199
+ else
200
+ allow(URI)
201
+ end
202
+
203
+ allowed_mock.to receive(:open).and_return(@open_return)
204
+
197
205
  @uri = URI.parse("https://github.com/thoughtbot/paper:clip.jpg")
198
206
  @subject = Paperclip.io_adapters.for(@uri)
207
+ @uri_opener = RUBY_VERSION < '2.5' ? @subject : URI
199
208
  end
200
209
 
201
210
  after do
@@ -204,7 +213,7 @@ describe Paperclip::UriAdapter do
204
213
 
205
214
  context "with default read_timeout" do
206
215
  it "calls open without options" do
207
- expect(@subject).to receive(:open).with(@uri, {}).at_least(1).times
216
+ expect(@uri_opener).to receive(:open).with(@uri, {}).at_least(1).times
208
217
  end
209
218
  end
210
219
 
@@ -214,7 +223,8 @@ describe Paperclip::UriAdapter do
214
223
  end
215
224
 
216
225
  it "calls open with read_timeout option" do
217
- expect(@subject).to receive(:open).with(@uri, read_timeout: 120).at_least(1).times
226
+ expect(@uri_opener)
227
+ .to receive(:open).with(@uri, read_timeout: 120).at_least(1).times
218
228
  end
219
229
  end
220
230
  end
@@ -49,6 +49,29 @@ describe Paperclip::Storage::Filesystem do
49
49
  assert_equal @file.read, tempfile.read
50
50
  tempfile.close
51
51
  end
52
+
53
+ it "only issues a delete call once for each unique attachment style when nullifying attachment" do
54
+ @dummy.save
55
+ @dummy.avatar.clear(:thumbnail)
56
+ @dummy.avatar = nil
57
+ assert_equal 3, @dummy.avatar.queued_for_delete.size
58
+
59
+ expect(FileUtils).to receive(:rm).twice
60
+ @dummy.save
61
+
62
+ FileUtils.rm_rf("tmp")
63
+ end
64
+
65
+ it "only issues a delete call once for each unique attachment style when destroying model" do
66
+ @dummy.save
67
+ @dummy.avatar.clear(:thumbnail)
68
+ assert_equal 1, @dummy.avatar.queued_for_delete.size
69
+
70
+ expect(FileUtils).to receive(:rm).twice
71
+ @dummy.destroy
72
+
73
+ FileUtils.rm_rf("tmp")
74
+ end
52
75
  end
53
76
 
54
77
  context "with file that has space in file name" do
@@ -7,6 +7,52 @@ describe Paperclip::Storage::Fog do
7
7
  context "" do
8
8
  before { Fog.mock! }
9
9
 
10
+ context "deleting attachment styles" do
11
+ before do
12
+ rebuild_model styles: { medium: "300x300>", thumb: "100x100>" },
13
+ storage: :fog,
14
+ url: "/:attachment/:style/:filename",
15
+ fog_directory: "paperclip",
16
+ fog_credentials: fixture_file("fog.yml")
17
+ @file = File.open(fixture_file("5k.png"))
18
+ @dummy = Dummy.new
19
+ @dummy.avatar = @file
20
+ @dummy.save
21
+ end
22
+
23
+ after do
24
+ @file.close
25
+ FileUtils.rm_rf("tmp")
26
+ end
27
+
28
+ it "only issues a delete call once for each unique attachment style when nullifying attachment" do
29
+ @dummy.avatar.clear(:thumb)
30
+ @dummy.avatar = nil
31
+ assert_equal 4, @dummy.avatar.queued_for_delete.size
32
+
33
+ original = double("original")
34
+ medium = double("medium")
35
+ thumb = double("thumb")
36
+
37
+ allow(Fog::AWS::Storage::File).to receive(:new).and_return(original, medium, thumb)
38
+
39
+ expect(original).to receive(:destroy).once
40
+ expect(medium).to receive(:destroy).once
41
+ expect(thumb).to receive(:destroy).once
42
+ @dummy.save
43
+ end
44
+
45
+ it "only issues a delete call once for each unique attachment style when destroying model" do
46
+ @dummy.avatar.clear(:thumb)
47
+ assert_equal 1, @dummy.avatar.queued_for_delete.size
48
+
49
+ file = double("file")
50
+ allow(Fog::AWS::Storage::File).to receive(:new).and_return(file)
51
+ expect(file).to receive(:destroy).exactly(3).times
52
+ @dummy.destroy
53
+ end
54
+ end
55
+
10
56
  context "with credentials provided in a path string" do
11
57
  before do
12
58
  rebuild_model styles: { medium: "300x300>", thumb: "100x100>" },
@@ -805,9 +805,7 @@ describe Paperclip::Storage::S3 do
805
805
  s3_region: "ap-northeast-1",
806
806
  s3_host_name: "s3-ap-northeast-1.amazonaws.com"
807
807
  },
808
- test: {
809
- s3_region: ""
810
- }
808
+ test: {}
811
809
  }
812
810
  @dummy = Dummy.new
813
811
  end
@@ -942,6 +940,19 @@ describe Paperclip::Storage::S3 do
942
940
  end
943
941
  end
944
942
 
943
+ context "and remove, calling S3 Object destroy once per unique style" do
944
+ before do
945
+ allow_any_instance_of(Aws::S3::Object).to receive(:exists?).and_return(true)
946
+ expect_any_instance_of(Aws::S3::Object).to receive(:delete).once
947
+ @dummy.avatar.clear(:original)
948
+ @dummy.destroy
949
+ end
950
+
951
+ it "succeeds" do
952
+ assert true
953
+ end
954
+ end
955
+
945
956
  context "that the file were missing" do
946
957
  before do
947
958
  allow_any_instance_of(Aws::S3::Object).to receive(:exists?).
@@ -1439,6 +1450,32 @@ describe Paperclip::Storage::S3 do
1439
1450
  end
1440
1451
  end
1441
1452
 
1453
+ context "with S3 credentials supplied as Pathname and aliases being set" do
1454
+ before do
1455
+ ENV["S3_KEY"] = "pathname_key"
1456
+ ENV["S3_BUCKET"] = "pathname_bucket"
1457
+ ENV["S3_SECRET"] = "pathname_secret"
1458
+
1459
+ rails_env("test") do
1460
+ rebuild_model aws2_add_region.merge storage: :s3,
1461
+ s3_credentials: Pathname.new(fixture_file("aws_s3.yml"))
1462
+
1463
+ Dummy.delete_all
1464
+ @dummy = Dummy.new
1465
+ end
1466
+ end
1467
+
1468
+ it "parses the credentials" do
1469
+ assert_equal "pathname_bucket", @dummy.avatar.bucket_name
1470
+
1471
+ assert_equal "pathname_key",
1472
+ @dummy.avatar.s3_bucket.client.config.access_key_id
1473
+
1474
+ assert_equal "pathname_secret",
1475
+ @dummy.avatar.s3_bucket.client.config.secret_access_key
1476
+ end
1477
+ end
1478
+
1442
1479
  context "with S3 credentials in a YAML file" do
1443
1480
  before do
1444
1481
  ENV["S3_KEY"] = "env_key"
@@ -1466,6 +1503,33 @@ describe Paperclip::Storage::S3 do
1466
1503
  end
1467
1504
  end
1468
1505
 
1506
+ context "with S3 credentials in a YAML file and aliases being set" do
1507
+ before do
1508
+ ENV["S3_KEY"] = "env_key"
1509
+ ENV["S3_BUCKET"] = "env_bucket"
1510
+ ENV["S3_SECRET"] = "env_secret"
1511
+
1512
+ rails_env("test") do
1513
+ rebuild_model aws2_add_region.merge storage: :s3,
1514
+ s3_credentials: File.new(fixture_file("aws_s3.yml"))
1515
+
1516
+ Dummy.delete_all
1517
+
1518
+ @dummy = Dummy.new
1519
+ end
1520
+ end
1521
+
1522
+ it "runs the file through ERB" do
1523
+ assert_equal "env_bucket", @dummy.avatar.bucket_name
1524
+
1525
+ assert_equal "env_key",
1526
+ @dummy.avatar.s3_bucket.client.config.access_key_id
1527
+
1528
+ assert_equal "env_secret",
1529
+ @dummy.avatar.s3_bucket.client.config.secret_access_key
1530
+ end
1531
+ end
1532
+
1469
1533
  context "S3 Permissions" do
1470
1534
  context "defaults to :public_read" do
1471
1535
  before do
@@ -194,6 +194,16 @@ describe Paperclip::UrlGenerator do
194
194
  "expected the interpolator to be passed #{expected.inspect} but it wasn't"
195
195
  end
196
196
 
197
+ it "doesn't emit deprecation warnings" do
198
+ expected = "the expected result"
199
+ mock_interpolator = MockInterpolator.new(result: expected)
200
+ options = { interpolator: mock_interpolator }
201
+ mock_attachment = MockAttachment.new(options)
202
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment)
203
+
204
+ expect { url_generator.for(:style_name, escape: true) }.to_not(output(/URI\.(un)?escape is obsolete/).to_stderr)
205
+ end
206
+
197
207
  describe "should be able to escape (, ), [, and ]." do
198
208
  def generate(expected, updated_at = nil)
199
209
  mock_interpolator = MockInterpolator.new(result: expected)
@@ -67,6 +67,94 @@ 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
@@ -29,6 +29,96 @@ 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
124
  allow(@dummy).to receive_messages(avatar_file_name: "image.png")