Skip to content

Commit

Permalink
Fix Ruby 2.7 & Rails 6.1 deprecation messages (kreeti#59)
Browse files Browse the repository at this point in the history
* Fix Ruby 2.7 & Rails 6.1 deprecation messages

These can be seen in https://travis-ci.com/github/kreeti/kt-paperclip/jobs/508937905

- ActiveModel::Errors#keys is deprecated and will be removed in Rails 6.2
- warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call

* Fix Hound violations
  • Loading branch information
reedstonefood authored Jun 8, 2021
1 parent 94a0f39 commit 379d3a8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/paperclip/validators/attachment_file_name_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def validate_each(record, attribute, value)
[:both, :base].include?(options[:add_validation_errors_to])

record.errors[attribute].each do |error|
record.errors.add base_attribute, error
record.errors.add(base_attribute, error)
end

record.errors.delete(attribute) if options[:add_validation_errors_to] == :base
Expand Down
27 changes: 21 additions & 6 deletions spec/paperclip/validators_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
require "spec_helper"

describe Paperclip::Validators do
# required to support a range of rubies
def error_attribute_names(error)
error.try(:attribute_names) || error.keys
end

context "using the helper" do
before do
rebuild_class
Expand All @@ -22,7 +27,9 @@
it "prevents you from attaching a file that violates that validation" do
Dummy.class_eval { validate(:name) { raise "DO NOT RUN THIS" } }
dummy = Dummy.new(avatar: File.new(fixture_file("12k.png")))
expect(dummy.errors.keys).to match_array [:avatar_content_type, :avatar, :avatar_file_size]
expect(error_attribute_names(dummy.errors)).to match_array(
%i[avatar_content_type avatar avatar_file_size]
)
assert_raises(RuntimeError) { dummy.valid? }
end
end
Expand All @@ -47,21 +54,27 @@
it "prevents you from attaching a file that violates all of these validations" do
Dummy.class_eval { validate(:name) { raise "DO NOT RUN THIS" } }
dummy = Dummy.new(avatar: File.new(fixture_file("spaced file.png")))
expect(dummy.errors.keys).to match_array [:avatar, :avatar_file_name]
expect(error_attribute_names(dummy.errors)).to match_array(
%i[avatar avatar_file_name]
)
assert_raises(RuntimeError) { dummy.valid? }
end

it "prevents you from attaching a file that violates only first of these validations" do
Dummy.class_eval { validate(:name) { raise "DO NOT RUN THIS" } }
dummy = Dummy.new(avatar: File.new(fixture_file("5k.png")))
expect(dummy.errors.keys).to match_array [:avatar, :avatar_file_name]
expect(error_attribute_names(dummy.errors)).to match_array(
%i[avatar avatar_file_name]
)
assert_raises(RuntimeError) { dummy.valid? }
end

it "prevents you from attaching a file that violates only second of these validations" do
Dummy.class_eval { validate(:name) { raise "DO NOT RUN THIS" } }
dummy = Dummy.new(avatar: File.new(fixture_file("spaced file.jpg")))
expect(dummy.errors.keys).to match_array [:avatar, :avatar_file_name]
expect(error_attribute_names(dummy.errors)).to match_array(
%i[avatar avatar_file_name]
)
assert_raises(RuntimeError) { dummy.valid? }
end

Expand All @@ -88,7 +101,9 @@ def title_present?
end
end
dummy = Dummy.new(avatar: File.new(fixture_file("12k.png")))
expect(dummy.errors.keys).to match_array [:avatar_content_type, :avatar, :avatar_file_size]
expect(error_attribute_names(dummy.errors)).to match_array(
%i[avatar_content_type avatar avatar_file_size]
)
end

it "does not validate attachment if title is not present" do
Expand All @@ -98,7 +113,7 @@ def title_present?
end
end
dummy = Dummy.new(avatar: File.new(fixture_file("12k.png")))
assert_equal [], dummy.errors.keys
assert_equal [], error_attribute_names(dummy.errors)
end
end

Expand Down

0 comments on commit 379d3a8

Please sign in to comment.