Skip to content

Commit

Permalink
[Validator] Refactor AspectRatioValidator (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mth0158 committed Oct 22, 2024
1 parent 507b758 commit d060731
Showing 1 changed file with 50 additions and 31 deletions.
81 changes: 50 additions & 31 deletions lib/active_storage_validations/aspect_ratio_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,43 +40,62 @@ def validate_each(record, attribute, _value)

def is_valid?(record, attribute, attachable, metadata)
flat_options = unfold_procs(record, self.options, AVAILABLE_CHECKS)
errors_options = initialize_error_options(options, attachable)

if metadata[:width].to_i <= 0 || metadata[:height].to_i <= 0
errors_options[:aspect_ratio] = flat_options[:with]

add_error(record, attribute, :image_metadata_missing, **errors_options)
return false
end
return if image_metadata_missing?(record, attribute, attachable, flat_options, metadata)

case flat_options[:with]
when :square
return true if metadata[:width] == metadata[:height]
errors_options[:aspect_ratio] = flat_options[:with]
add_error(record, attribute, :aspect_ratio_not_square, **errors_options)

when :portrait
return true if metadata[:height] > metadata[:width]
errors_options[:aspect_ratio] = flat_options[:with]
add_error(record, attribute, :aspect_ratio_not_portrait, **errors_options)

when :landscape
return true if metadata[:width] > metadata[:height]
errors_options[:aspect_ratio] = flat_options[:with]
add_error(record, attribute, :aspect_ratio_not_landscape, **errors_options)

when ASPECT_RATIO_REGEX
flat_options[:with] =~ ASPECT_RATIO_REGEX
x = $1.to_i
y = $2.to_i

return true if x > 0 && y > 0 && (x.to_f / y).round(PRECISION) == (metadata[:width].to_f / metadata[:height]).round(PRECISION)

errors_options[:aspect_ratio] = "#{x}:#{y}"
add_error(record, attribute, :aspect_ratio_is_not, **errors_options)
when :square then validate_square_aspect_ratio(record, attribute, attachable, flat_options, metadata)
when :portrait then validate_portrait_aspect_ratio(record, attribute, attachable, flat_options, metadata)
when :landscape then validate_landscape_aspect_ratio(record, attribute, attachable, flat_options, metadata)
when ASPECT_RATIO_REGEX then validate_regex_aspect_ratio(record, attribute, attachable, flat_options, metadata)
end
end

def image_metadata_missing?(record, attribute, attachable, flat_options, metadata)
return false if metadata[:width].to_i > 0 && metadata[:height].to_i > 0

errors_options = initialize_error_options(self.options, attachable)
errors_options[:aspect_ratio] = flat_options[:with]
add_error(record, attribute, :image_metadata_missing, **errors_options)
true
end

def validate_square_aspect_ratio(record, attribute, attachable, flat_options, metadata)
return if metadata[:width] == metadata[:height]

errors_options = initialize_error_options(self.options, attachable)
errors_options[:aspect_ratio] = flat_options[:with]
add_error(record, attribute, :aspect_ratio_not_square, **errors_options)
end

def validate_portrait_aspect_ratio(record, attribute, attachable, flat_options, metadata)
return if metadata[:width] < metadata[:height]

errors_options = initialize_error_options(self.options, attachable)
errors_options[:aspect_ratio] = flat_options[:with]
add_error(record, attribute, :aspect_ratio_not_portrait, **errors_options)
end

def validate_landscape_aspect_ratio(record, attribute, attachable, flat_options, metadata)
return true if metadata[:width] > metadata[:height]

errors_options = initialize_error_options(self.options, attachable)
errors_options[:aspect_ratio] = flat_options[:with]
add_error(record, attribute, :aspect_ratio_not_landscape, **errors_options)
end

def validate_regex_aspect_ratio(record, attribute, attachable, flat_options, metadata)
flat_options[:with] =~ ASPECT_RATIO_REGEX
x = $1.to_i
y = $2.to_i

return true if x > 0 && y > 0 && (x.to_f / y).round(PRECISION) == (metadata[:width].to_f / metadata[:height]).round(PRECISION)

errors_options = initialize_error_options(self.options, attachable)
errors_options[:aspect_ratio] = "#{x}:#{y}"
add_error(record, attribute, :aspect_ratio_is_not, **errors_options)
end

def ensure_at_least_one_validator_option
unless AVAILABLE_CHECKS.any? { |argument| options.key?(argument) }
raise ArgumentError, 'You must pass :with to the validator'
Expand Down

0 comments on commit d060731

Please sign in to comment.