Sha256: f0efe6dc6c692b6bea1f029b38778dd14b5cb1825f79165cae7296b1594493ab

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 KB

Contents

module Xeroizer
  module Record
    
    class Validator
  
      class AssociatedValidator < Validator
        
        def valid?(record)
          case record.class.fields[attribute][:type]
            when :belongs_to
              return true if options[:allow_blanks] && record[attribute].nil?
              unless record[attribute].is_a?(Xeroizer::Record::Base) && record[attribute].valid?
                record.errors << [attribute, options[:message] || "must be valid"]
              end
              
            when :has_many
              return true if options[:allow_blanks] && (record[attribute].nil? || (record[attribute].is_a?(Array) && record[attribute].size == 0))
              if record[attribute].is_a?(Array) && record[attribute].size > 0
                unless record[attribute].all? { | r | r.is_a?(Xeroizer::Record::Base) && r.valid? }
                  record.errors << [attribute, options[:message] || "must all be valid"]
                end
              else
                record.errors << [attribute, "must have one or more records"] 
              end

          end
        end
        
      end
  
    end
    
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
xeroizer-3.0.1 lib/xeroizer/record/validators/associated_validator.rb
xeroizer-3.0.0 lib/xeroizer/record/validators/associated_validator.rb
xeroizer-3-pre-beta-3.0.0.pre.beta lib/xeroizer/record/validators/associated_validator.rb