Skip to content

Commit

Permalink
Fix: Fix size matcher for has_many_attached attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
jozzi05 committed Nov 2, 2023
1 parent dc76544 commit a444203
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ def passes_validation_with_size(new_size)
@subject.public_send(@attribute_name).attach(io: io, filename: 'test.png', content_type: 'image/pg')
@subject.validate
exclude_error_message = @custom_message || "file_size_not_"
@subject.errors.details[@attribute_name].none? do |error|
error[:error].to_s.include?(exclude_error_message)
end
@subject.errors.details[@attribute_name]
.none? { |error| error[:error].to_s.include?(exclude_error_message) }
.tap { @subject.attachment_changes["#{@attribute_name}"] = nil }
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions test/dummy/app/models/size/portfolio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class Size::Portfolio < ApplicationRecord
has_one_attached :proc_size_between
has_one_attached :proc_size_with_message

has_many_attached :many_size_between

validates :title, presence: true

validates :size_less_than, size: { less_than: 2.kilobytes }
Expand All @@ -34,6 +36,8 @@ class Size::Portfolio < ApplicationRecord
validates :size_between, size: { between: 2.kilobytes..7.kilobytes }
validates :size_with_message, size: { between: 2.kilobytes..7.kilobytes, message: 'is not in required file size range' }

validates :many_size_between, size: { between: 2.kilobytes..7.kilobytes }

validates :proc_size_less_than, size: { less_than: -> (record) { 2.kilobytes } }
validates :proc_size_less_than_or_equal_to, size: { less_than_or_equal_to: -> (record) { 2.kilobytes } }
validates :proc_size_greater_than, size: { greater_than: -> (record) { 7.kilobytes } }
Expand Down
37 changes: 37 additions & 0 deletions test/matchers/size_validator_matcher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,43 @@ class BetweenMatcher < ActiveStorageValidations::Matchers::SizeValidatorMatcher:
end
end

class BetweenMatcherForManyAttachments < ActiveStorageValidations::Matchers::SizeValidatorMatcher::Test
test 'matches when provided with the model validation value' do
matcher = ActiveStorageValidations::Matchers::SizeValidatorMatcher.new(:many_size_between)
matcher.between 2.kilobytes..7.kilobytes
assert matcher.matches?(Size::Portfolio)
end

test 'does not match when provided a higher value than the model validation value for highest possible size' do
matcher = ActiveStorageValidations::Matchers::SizeValidatorMatcher.new(:many_size_between)
matcher.between 2.kilobytes..10.kilobytes
refute matcher.matches?(Size::Portfolio)
end

test 'does not match when provided a lower value than the model validation value for highest possible size' do
matcher = ActiveStorageValidations::Matchers::SizeValidatorMatcher.new(:many_size_between)
matcher.between 1.kilobytes..7.kilobytes
refute matcher.matches?(Size::Portfolio)
end

test 'does not match when provided a higher value than the model validation value for lowest possible size' do
matcher = ActiveStorageValidations::Matchers::SizeValidatorMatcher.new(:many_size_between)
matcher.between 5.kilobytes..7.kilobytes
refute matcher.matches?(Size::Portfolio)
end

test 'does not match when provided a lower value than the model validation value for lowest possible size' do
matcher = ActiveStorageValidations::Matchers::SizeValidatorMatcher.new(:many_size_between)
matcher.between 1.kilobytes..7.kilobytes
refute matcher.matches?(Size::Portfolio)
end

test 'does not match when provided both lowest and highest possible values different than the model validation value' do
matcher = ActiveStorageValidations::Matchers::SizeValidatorMatcher.new(:many_size_between)
matcher.between 4.kilobytes..20.kilobytes
refute matcher.matches?(Size::Portfolio)
end
end

class WithMessageMatcher < ActiveStorageValidations::Matchers::SizeValidatorMatcher::Test
test 'matches when provided with the model validation message' do
Expand Down

0 comments on commit a444203

Please sign in to comment.