Skip to content

Instantly share code, notes, and snippets.

@yowchun93
Last active August 13, 2020 12:38
Show Gist options
  • Save yowchun93/baf7ae4ec47386cc1e0014179474766a to your computer and use it in GitHub Desktop.
Save yowchun93/baf7ae4ec47386cc1e0014179474766a to your computer and use it in GitHub Desktop.
Custom Validators
# Creating custom validator
# https://github.com/igorkasyanchuk/active_storage_validations/blob/master/lib/active_storage_validations/size_validator.rb
# inherited
class SizeValidator < ActiveModel::EachValidator
def check_validity!
raise ArgumentError, ‘bla bla’
end
def validate_each(record, attribute, _value)
return true unless record.send(attribute).attached?
files = Array.wrap(record.send(attributes))
files.each do |file|
next if content_size_valid?(file.blob)
end
end
def content_size_valid?(file_size)
# where the hell is options coming from man??
# could come from ActiveModel Validator??
if options[:between].present?
elsif options[:less_than].present?
end
end
module ActiveStorageValidations
class AttachedValidator < ActiveModel::EachValidator
def validate_each(record, attribute, _value)
return if record.send(attribute).attached?
record.errors.add(attribute, :blank)
end
end
end
## Maybe ask Hass how Gems are tied together?
lib/active_storage_validations.rb
ActiveSupport.on_load(:active_record) do
send :include, ActiveStorageValidations
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment