Module: EcsRails::Validations

Defined in:
lib/ecs_rails/validations.rb

Overview

Validation error merging: entity.valid? reflects its components’ validity, and entity.errors reads naturally in a Rails form.

Implements RFC-0007. Closes the gap RFC-0006 left explicit (see its “Status” section): the save/save! contract was already atomic, but it held by accident — the after_save cascade’s component.save! raised, and save rescued it. valid? itself did not yet know a dirty component was bad.

user = User.create! user.email.address = “not-an-email” user.valid? # => false (now, not just on save) user.errors[:”email.address”] # => [“is invalid”] user.errors.full_messages # => [“Email address is invalid”] user.save # => false, and inserts nothing

With this in place, entity.save returns false because valid? is false before the cascade ever runs — the cascade’s bang becomes belt-and-braces, exactly as ActiveRecord’s autosave is. See RFC-0006’s #save_dirty_components.

Defined Under Namespace

Modules: Entity