Class: ActiveStorageValidations::DimensionValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Includes:
Errorable, OptionProcUnfolding, Symbolizable
Defined in:
lib/active_storage_validations/dimension_validator.rb

Overview

:nodoc

Constant Summary collapse

AVAILABLE_CHECKS =
%i[width height min max].freeze
ERROR_TYPES =
%i[
  image_metadata_missing
  dimension_min_inclusion
  dimension_max_inclusion
  dimension_width_inclusion
  dimension_height_inclusion
  dimension_width_greater_than_or_equal_to
  dimension_height_greater_than_or_equal_to
  dimension_width_less_than_or_equal_to
  dimension_height_less_than_or_equal_to
  dimension_width_equal_to
  dimension_height_equal_to
].freeze

Instance Method Summary collapse

Methods included from Errorable

#add_error, #initialize_error_options

Methods included from OptionProcUnfolding

#unfold_procs

Instance Method Details

#check_validity!Object



50
51
52
53
54
# File 'lib/active_storage_validations/dimension_validator.rb', line 50

def check_validity!
  unless AVAILABLE_CHECKS.any? { |argument| options.key?(argument) }
    raise ArgumentError, 'You must pass either :width, :height, :min or :max to the validator'
  end
end

#is_valid?(record, attribute, file, metadata) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/active_storage_validations/dimension_validator.rb', line 88

def is_valid?(record, attribute, file, )
  flat_options = process_options(record)
  errors_options = initialize_error_options(options, file)

  # Validation fails unless file metadata contains valid width and height.
  if [:width].to_i <= 0 || [:height].to_i <= 0
    add_error(record, attribute, :image_metadata_missing, **errors_options)
    return false
  end

  # Validation based on checks :min and :max (:min, :max has higher priority to :width, :height).
  if flat_options[:min] || flat_options[:max]
    if flat_options[:min] && (
      (flat_options[:width][:min] && [:width] < flat_options[:width][:min]) ||
      (flat_options[:height][:min] && [:height] < flat_options[:height][:min])
      )
      errors_options[:width] = flat_options[:width][:min]
      errors_options[:height] = flat_options[:height][:min]

      add_error(record, attribute, :dimension_min_inclusion, **errors_options)
      return false
    end
    if flat_options[:max] && (
      (flat_options[:width][:max] && [:width] > flat_options[:width][:max]) ||
      (flat_options[:height][:max] && [:height] > flat_options[:height][:max])
      )
      errors_options[:width] = flat_options[:width][:max]
      errors_options[:height] = flat_options[:height][:max]

      add_error(record, attribute, :dimension_max_inclusion, **errors_options)
      return false
    end

  # Validation based on checks :width and :height.
  else
    width_or_height_invalid = false

    [:width, :height].each do |length|
      next unless flat_options[length]
      if flat_options[length].is_a?(Hash)
        if flat_options[length][:in] && ([length] < flat_options[length][:min] || [length] > flat_options[length][:max])
          error_type = :"dimension_#{length}_inclusion"
          errors_options[:min] = flat_options[length][:min]
          errors_options[:max] = flat_options[length][:max]

          add_error(record, attribute, error_type, **errors_options)
          width_or_height_invalid = true
        else
          if flat_options[length][:min] && [length] < flat_options[length][:min]
            error_type = :"dimension_#{length}_greater_than_or_equal_to"
            errors_options[:length] = flat_options[length][:min]

            add_error(record, attribute, error_type, **errors_options)
            width_or_height_invalid = true
          elsif flat_options[length][:max] && [length] > flat_options[length][:max]
            error_type = :"dimension_#{length}_less_than_or_equal_to"
            errors_options[:length] = flat_options[length][:max]

            add_error(record, attribute, error_type, **errors_options)
            width_or_height_invalid = true
          end
        end
      else
        if [length] != flat_options[length]
          error_type = :"dimension_#{length}_equal_to"
          errors_options[:length] = flat_options[length]

          add_error(record, attribute, error_type, **errors_options)
          width_or_height_invalid = true
        end
      end
    end

    return false if width_or_height_invalid
  end

  true # valid file
end

#process_options(record) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/active_storage_validations/dimension_validator.rb', line 28

def process_options(record)
  flat_options = unfold_procs(record, self.options, AVAILABLE_CHECKS)

  [:width, :height].each do |length|
    if flat_options[length] and flat_options[length].is_a?(Hash)
      if (range = flat_options[length][:in])
        raise ArgumentError, ":in must be a Range" unless range.is_a?(Range)
        flat_options[length][:min], flat_options[length][:max] = range.min, range.max
      end
    end
  end
  [:min, :max].each do |dim|
    if (range = flat_options[dim])
      raise ArgumentError, ":#{dim} must be a Range (width..height)" unless range.is_a?(Range)
      flat_options[:width] = { dim => range.first }
      flat_options[:height] = { dim => range.last }
    end
  end

  flat_options
end

#validate_each(record, attribute, _value) ⇒ Object

Rails 5



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/active_storage_validations/dimension_validator.rb', line 73

def validate_each(record, attribute, _value)
  return true unless record.send(attribute).attached?

  changes = record.attachment_changes[attribute.to_s]
  return true if changes.blank?

  files = Array.wrap(changes.is_a?(ActiveStorage::Attached::Changes::CreateMany) ? changes.attachables : changes.attachable)
  files.each do |file|
     = Metadata.new(file).
    next if is_valid?(record, attribute, file, )
    break
  end
end