Sha256: c36abd34ebf9e588bf915daa0aee5e4e1f2e0f81e52808ac7abffb686081899f

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

module ExtraValidations
  # Makes sure that each member of the collection is a valid object.
  #
  # Suppose that you have the following class:
  #
  #   class Book
  #     include ActiveModel::Model
  #     include ExtraValidations
  #
  #     attr_accessor :authors
  #
  #     validates :authors, collection: true
  #   end
  #
  # This validator will call +#valid?+ for each member of the collection:
  #
  #   book = Book.new
  #   book.authors = [invalid_author1, valid_author1]
  #   book.valid? # => false
  #
  #   book.authors = [valid_author1, valid_author2]
  #   book.valid? # => true
  #
  # Each validation error found on members will be appended on to the errors
  # object:
  #
  #   book.errors.messages # => {:"authors/1/name"=>["can't be blank"]}
  #
  class CollectionValidator < ActiveModel::EachValidator
    # @param record An object that has ActiveModel::Validations included
    # @param attribute [Symbol]
    # @param collection [Array] An array of objects that have
    #   ActiveModel::Validations included
    def validate_each(record, attribute, collection)
      return if collection.blank?

      collection
        .each(&:valid?)
        .each_with_index do |obj, i|
          obj.errors.each do |attr, error|
            record.errors.add("#{attribute}/#{i}/#{attr}", error)
          end
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
extra_validations-0.1.1 lib/extra_validations/collection_validator.rb