Skip to content

Commit

Permalink
fixing review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammednasser-32 committed Dec 14, 2024
1 parent 1631967 commit 98cfe67
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions lib/active_storage_validations/aspect_ratio_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,18 @@ def check_validity!
def validate_each(record, attribute, _value)
return if no_attachments?(record, attribute)

flat_options = set_flat_options(record)
@authorized_aspect_ratios = authorized_aspect_ratios_from_options(flat_options).compact
return if @authorized_aspect_ratios.empty?

validate_changed_files_from_metadata(record, attribute)
end

private

def is_valid?(record, attribute, attachable, metadata)
flat_options = set_flat_options(record)

return if image_metadata_missing?(record, attribute, attachable, flat_options, metadata)

@authorized_aspect_ratios = authorized_aspect_ratios(flat_options).compact

return true if authorized_aspect_ratio?(record, attribute, attachable, metadata)

errors_options = initialize_error_options(options, attachable)
errors_options[:aspect_ratio] = string_aspect_ratios(flat_options)
add_error(record, attribute, aspect_ratio_error_mapping, **errors_options)
false
!image_metadata_missing?(record, attribute, attachable, metadata) &&
authorized_aspect_ratio?(record, attribute, attachable, metadata)
end

def authorized_aspect_ratio?(record, attribute, attachable, metadata)
Expand All @@ -66,20 +60,27 @@ def authorized_aspect_ratio?(record, attribute, attachable, metadata)
when ASPECT_RATIO_REGEX then valid_regex_aspect_ratio?(authorized_aspect_ratio, metadata)
end
end

return true if attachable_aspect_ratio_is_authorized

errors_options = initialize_error_options(options, attachable)
errors_options[:aspect_ratio] = string_aspect_ratios
add_error(record, attribute, aspect_ratio_error_mapping, **errors_options)
false
end

def aspect_ratio_error_mapping
return :aspect_ratio_invalid unless @authorized_aspect_ratios.length == 1
return :aspect_ratio_invalid unless @authorized_aspect_ratios.one?

aspect_ratio = @authorized_aspect_ratios.first
NAMED_ASPECT_RATIOS.include?(aspect_ratio) ? :"aspect_ratio_not_#{aspect_ratio}" : :aspect_ratio_is_not
end

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

errors_options = initialize_error_options(options, attachable)
errors_options[:aspect_ratio] = string_aspect_ratios(flat_options)
errors_options[:aspect_ratio] = string_aspect_ratios
add_error(record, attribute, :image_metadata_missing, **errors_options)
true
end
Expand Down Expand Up @@ -113,7 +114,7 @@ def ensure_at_least_one_validator_option
def ensure_aspect_ratio_validity
return true if options[:with]&.is_a?(Proc) || options[:in]&.is_a?(Proc)

authorized_aspect_ratios(options).each do |aspect_ratio|
authorized_aspect_ratios_from_options(options).each do |aspect_ratio|
unless NAMED_ASPECT_RATIOS.include?(aspect_ratio) || aspect_ratio =~ ASPECT_RATIO_REGEX
raise ArgumentError, invalid_aspect_ratio_message
end
Expand All @@ -128,12 +129,12 @@ def invalid_aspect_ratio_message
ERROR_MESSAGE
end

def authorized_aspect_ratios(flat_options)
def authorized_aspect_ratios_from_options(flat_options)
(Array.wrap(flat_options[:with]) + Array.wrap(flat_options[:in]))
end

def string_aspect_ratios(flat_options)
authorized_aspect_ratios(flat_options).map do |aspect_ratio|
def string_aspect_ratios
@authorized_aspect_ratios.map do |aspect_ratio|
if NAMED_ASPECT_RATIOS.include?(aspect_ratio)
aspect_ratio
else
Expand Down

0 comments on commit 98cfe67

Please sign in to comment.