Skip to content

Commit

Permalink
Merge pull request #169 from sobrinho/fix-slowness
Browse files Browse the repository at this point in the history
Fix slowness
  • Loading branch information
igorkasyanchuk committed Oct 19, 2022
2 parents 0d371f8 + 64b8b1d commit 7fce7c5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ def description

def allowing(*types)
@allowed_types = types.flatten
either_allowing_or_rejecting
self
end

def rejecting(*types)
@rejected_types = types.flatten
either_allowing_or_rejecting
self
end

Expand All @@ -33,19 +35,29 @@ def matches?(subject)
end

def failure_message
<<~MESSAGE
Expected #{@attribute_name}
message = ["Expected #{@attribute_name}"]

Accept content types: #{allowed_types.join(", ")}
#{accepted_types_and_failures}
if @allowed_types
message << "Accept content types: #{allowed_types.join(", ")}"
message << "#{@missing_allowed_types.join(", ")} were rejected"
end

if @rejected_types
message << "Reject content types: #{rejected_types.join(", ")}"
message << "#{@missing_rejected_types.join(", ")} were accepted"
end

Reject content types: #{rejected_types.join(", ")}
#{rejected_types_and_failures}
MESSAGE
message.join("\n")
end

protected

def either_allowing_or_rejecting
if @allowed_types && @rejected_types
raise ArgumentError, "You must specify either allowing or rejecting"
end
end

def responds_to_methods
@subject.respond_to?(@attribute_name) &&
@subject.public_send(@attribute_name).respond_to?(:attach) &&
Expand All @@ -57,7 +69,7 @@ def allowed_types
end

def rejected_types
@rejected_types || (content_type_keys - allowed_types)
@rejected_types || []
end

def allowed_types_allowed?
Expand All @@ -70,22 +82,6 @@ def rejected_types_rejected?
@missing_rejected_types.none?
end

def accepted_types_and_failures
if @missing_allowed_types.present?
"#{@missing_allowed_types.join(", ")} were rejected."
else
"All were accepted successfully."
end
end

def rejected_types_and_failures
if @missing_rejected_types.present?
"#{@missing_rejected_types.join(", ")} were accepted."
else
"All were rejected successfully."
end
end

def type_allowed?(type)
@subject.public_send(@attribute_name).attach(attachment_for(type))
@subject.validate
Expand All @@ -96,16 +92,6 @@ def attachment_for(type)
suffix = type.to_s.split('/').last
{ io: Tempfile.new('.'), filename: "test.#{suffix}", content_type: type }
end

private

def content_type_keys
if Rails.gem_version < Gem::Version.new('6.1.0')
Mime::LOOKUP.keys
else
Marcel::TYPES.keys
end
end
end
end
end
14 changes: 12 additions & 2 deletions test/matchers/content_type_validator_matcher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@
require 'active_storage_validations/matchers'

class ActiveStorageValidations::Matchers::ContentTypeValidatorMatcher::Test < ActiveSupport::TestCase
test 'positive and negative' do
matcher = ActiveStorageValidations::Matchers::ContentTypeValidatorMatcher.new(:avatar)
matcher.allowing('image/png')
assert_raise(ArgumentError) { matcher.rejecting('image/jpg') }
end

test 'negative and positive' do
matcher = ActiveStorageValidations::Matchers::ContentTypeValidatorMatcher.new(:avatar)
matcher.rejecting('image/png')
assert_raise(ArgumentError) { matcher.allowing('image/jpg') }
end

test 'positive match when providing class' do
matcher = ActiveStorageValidations::Matchers::ContentTypeValidatorMatcher.new(:avatar)
matcher.allowing('image/png')
matcher.rejecting('application/pdf')
assert matcher.matches?(User)
end

Expand All @@ -26,7 +37,6 @@ class ActiveStorageValidations::Matchers::ContentTypeValidatorMatcher::Test < Ac
test 'positive match when providing instance' do
matcher = ActiveStorageValidations::Matchers::ContentTypeValidatorMatcher.new(:avatar)
matcher.allowing('image/png')
matcher.rejecting('application/pdf')
assert matcher.matches?(User.new)
end

Expand Down

0 comments on commit 7fce7c5

Please sign in to comment.