Created
          August 7, 2019 09:04 
        
      - 
      
- 
        Save bananatron/e299157719e057d8bb766bceada89e4b to your computer and use it in GitHub Desktop. 
    Used to supply the client w/ AR model validation rules
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | # Used to supply the client w/ AR model validation rules | |
| module ModelValidations | |
| def model_validations | |
| validations = {} | |
| self.class.validators.each do |validation| | |
| # Presence validations (is required) | |
| if validation.class == ActiveRecord::Validations::PresenceValidator | |
| validations[validation.attributes.first] = { allow_nil: false } | |
| end | |
| # Inclusion validations (must be included in list) | |
| if validation.class == ActiveModel::Validations::InclusionValidator | |
| validation_list = validation.options[:in] | |
| if validation_list.class == Symbol | |
| validations[validation.attributes.first] = validation.options.merge({in: self.send(validation_list)}) | |
| else | |
| validations[validation.attributes.first] = validation.options | |
| end | |
| end | |
| end | |
| return validations | |
| end | |
| end | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment