SHARE
TWEET

Untitled

a guest Aug 19th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Used to supply the client w/ AR model validation rules
  2.  
  3. module ModelValidations
  4.     def model_validations
  5.         validations = {}
  6.         self.class.validators.each do |validation|
  7.             # Presence validations (is required)
  8.             if validation.class == ActiveRecord::Validations::PresenceValidator
  9.                 validations[validation.attributes.first] = { allow_nil: false }
  10.             end
  11.  
  12.             # Inclusion validations (must be included in list)
  13.             if validation.class == ActiveModel::Validations::InclusionValidator
  14.                 validation_list = validation.options[:in]
  15.                 if validation_list.class == Symbol
  16.                     validations[validation.attributes.first] = validation.options.merge({in: self.send(validation_list)})
  17.                 else
  18.                     validations[validation.attributes.first] = validation.options
  19.                 end
  20.             end
  21.         end
  22.         return validations
  23.     end
  24. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top