Sha256: f0a437ccadb6fcfe4c47d78eda6afe52c9c9a4daeb6d91438e8e7d9dc1809567

Contents?: true

Size: 1.36 KB

Versions: 8

Compression:

Stored size: 1.36 KB

Contents

module ActiveData
  module Model
    module Validations
      class NestedValidator < ActiveModel::EachValidator
        def self.validate_nested(record, name, value)
          if value.is_a?(Enumerable)
            value.each.with_index do |object, i|
              if yield(object)
                object.errors.each do |key, message|
                  key = "#{name}.#{i}.#{key}"
                  record.errors[key] << message
                  record.errors[key].uniq!
                end
              end
            end
          elsif value && yield(value)
            value.errors.each do |key, message|
              key = "#{name}.#{key}"
              record.errors[key] << message
              record.errors[key].uniq!
            end
          end
        end

        def validate_each(record, attribute, value)
          self.class.validate_nested(record, attribute, value) do |object|
            object.invalid? && !(object.respond_to?(:marked_for_destruction?) && object.marked_for_destruction?)
          end
        end
      end

      module HelperMethods
        def validates_nested(*attr_names)
          validates_with NestedValidator, _merge_attributes(attr_names)
        end

        def validates_nested?(attr)
          _validators[attr.to_sym]
            .grep(ActiveData::Model::Validations::NestedValidator).present?
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
active_data-1.1.7 lib/active_data/model/validations/nested.rb
active_data-1.1.6 lib/active_data/model/validations/nested.rb
active_data-1.1.5 lib/active_data/model/validations/nested.rb
active_data-1.1.4 lib/active_data/model/validations/nested.rb
active_data-1.1.3 lib/active_data/model/validations/nested.rb
active_data-1.1.2 lib/active_data/model/validations/nested.rb
active_data-1.1.1 lib/active_data/model/validations/nested.rb
active_data-1.1.0 lib/active_data/model/validations/nested.rb