Guest User

Untitled

a guest
Nov 24th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. class ArrayValidator < ActiveModel::EachValidator
  2. def validate_each(record, attribute, values)
  3. [values].flatten.each do |value|
  4. options.each do |key, args|
  5. validator_options = { attributes: attribute }
  6. validator_options.merge!(args) if args.is_a?(Hash)
  7.  
  8. next if value.nil? && validator_options[:allow_nil]
  9. next if value.blank? && validator_options[:allow_blank]
  10.  
  11. validator_class_name = "#{key.to_s.camelize}Validator"
  12. validator_class = begin
  13. validator_class_name.constantize
  14. rescue NameError
  15. "ActiveModel::Validations::#{validator_class_name}".constantize
  16. end
  17.  
  18. validator = validator_class.new(validator_options)
  19. validator.validate_each(record, attribute, value)
  20. end
  21. end
  22. end
  23. end
Add Comment
Please, Sign In to add comment