Sha256: 6aa512dceb374e576694131d02d3f62225540336181bc12a9e82dfa3dd8ae9e3

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

module ExtraValidations
  # Makes sure that the nested object is valid.
  #
  # Suppose that you have the following class:
  #
  #   class Book
  #     include ActiveModel::Model
  #     include ExtraValidations
  #
  #     attr_accessor :author
  #
  #     validates :author, nested: true
  #   end
  #
  # This validator will call +#valid?+ on the nested object:
  #
  #   book = Book.new
  #   book.author = invalid_author
  #   book.valid? # => false
  #
  #   book.author = valid_author
  #   book.valid? # => true
  #
  # Each validation error found will be appended on to the errors object:
  #
  #   book.errors.messages # => {:"author/name"=>["can't be blank"]}
  #
  class NestedValidator < ActiveModel::EachValidator
    # @param record An object that has ActiveModel::Validations included
    # @param attribute [Symbol]
    # @param nested An object that has ActiveModel::Validations included
    def validate_each(record, attribute, nested)
      return if !nested || nested.valid?

      nested.errors.each do |nested_attr, error|
        record.errors.add("#{attribute}/#{nested_attr}", error)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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