Class: ActiveStorageValidations::DimensionValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- ActiveStorageValidations::DimensionValidator
- Includes:
- OptionProcUnfolding
- Defined in:
- lib/active_storage_validations/dimension_validator.rb
Overview
:nodoc
Constant Summary collapse
- AVAILABLE_CHECKS =
%i[width height min max].freeze
Instance Method Summary collapse
- #add_error(record, attribute, default_message, **attrs) ⇒ Object
- #check_validity! ⇒ Object
- #is_valid?(record, attribute, file_metadata) ⇒ Boolean
- #process_options(record) ⇒ Object
-
#validate_each(record, attribute, _value) ⇒ Object
Rails 5.
Methods included from OptionProcUnfolding
Instance Method Details
#add_error(record, attribute, default_message, **attrs) ⇒ Object
129 130 131 132 133 |
# File 'lib/active_storage_validations/dimension_validator.rb', line 129 def add_error(record, attribute, , **attrs) = [:message].presence || return if record.errors.added?(attribute, ) record.errors.add(attribute, , **attrs) end |
#check_validity! ⇒ Object
34 35 36 37 |
# File 'lib/active_storage_validations/dimension_validator.rb', line 34 def check_validity! return true if AVAILABLE_CHECKS.any? { |argument| .key?(argument) } raise ArgumentError, 'You must pass either :width, :height, :min or :max to the validator' end |
#is_valid?(record, attribute, file_metadata) ⇒ Boolean
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 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 |
# File 'lib/active_storage_validations/dimension_validator.rb', line 71 def is_valid?(record, attribute, ) = (record) # 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) return false end # Validation based on checks :min and :max (:min, :max has higher priority to :width, :height). if [:min] || [:max] if [:min] && ( ([:width][:min] && [:width] < [:width][:min]) || ([:height][:min] && [:height] < [:height][:min]) ) add_error(record, attribute, :dimension_min_inclusion, width: [:width][:min], height: [:height][:min]) return false end if [:max] && ( ([:width][:max] && [:width] > [:width][:max]) || ([:height][:max] && [:height] > [:height][:max]) ) add_error(record, attribute, :dimension_max_inclusion, width: [:width][:max], height: [:height][:max]) return false end # Validation based on checks :width and :height. else width_or_height_invalid = false [:width, :height].each do |length| next unless [length] if [length].is_a?(Hash) if [length][:in] && ([length] < [length][:min] || [length] > [length][:max]) add_error(record, attribute, :"dimension_#{length}_inclusion", min: [length][:min], max: [length][:max]) width_or_height_invalid = true else if [length][:min] && [length] < [length][:min] add_error(record, attribute, :"dimension_#{length}_greater_than_or_equal_to", length: [length][:min]) width_or_height_invalid = true end if [length][:max] && [length] > [length][:max] add_error(record, attribute, :"dimension_#{length}_less_than_or_equal_to", length: [length][:max]) width_or_height_invalid = true end end else if [length] != [length] add_error(record, attribute, :"dimension_#{length}_equal_to", length: [length]) width_or_height_invalid = true end end end return false if width_or_height_invalid end true # valid file end |
#process_options(record) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/active_storage_validations/dimension_validator.rb', line 11 def (record) = unfold_procs(record, self., AVAILABLE_CHECKS) [:width, :height].each do |length| if [length] and [length].is_a?(Hash) if (range = [length][:in]) raise ArgumentError, ":in must be a Range" unless range.is_a?(Range) [length][:min], [length][:max] = range.min, range.max end end end [:min, :max].each do |dim| if (range = [dim]) raise ArgumentError, ":#{dim} must be a Range (width..height)" unless range.is_a?(Range) [:width] = { dim => range.first } [:height] = { dim => range.last } end end end |
#validate_each(record, attribute, _value) ⇒ Object
Rails 5
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/active_storage_validations/dimension_validator.rb', line 56 def validate_each(record, attribute, _value) return true unless record.send(attribute).attached? changes = record.[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, ) break end end |