Module: ActiveRecordCompose::Validations

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Validations::Callbacks
Included in:
Model
Defined in:
lib/active_record_compose/validations.rb

Instance Method Summary collapse

Instance Method Details

#errorsActiveModel::Errors

Returns the ActiveModel::Errors object that holds all information about attribute error messages.

The ActiveModel::Base implementation itself, but also aggregates error information for objects stored in #models when validation is performed.

class Account < ActiveRecord::Base
  validates :name, :email, presence: true
end

class AccountRegistration < ActiveRecordCompose::Model
  def initialize(attributes = {})
    @account = Account.new
    super(attributes)
    models << 
  end

  attribute :confirmation, :boolean, default: false
  validates :confirmation, presence: true

  private

  attr_reader :account
end

registration = AccountRegistration
registration.valid?
#=> false

# In addition to the model's own validation error information (`confirmation`), also aggregates
# error information for objects stored in `account` (`name`, `email`) when validation is performed.

registration.errors.map { _1.attribute }  #=> [:name, :email, :confirmation]

Returns:

  • (ActiveModel::Errors)


# File 'lib/active_record_compose/validations.rb', line 43

#save(**options) ⇒ void



16
17
18
# File 'lib/active_record_compose/validations.rb', line 16

def save(**options)
  perform_validations(options) ? super : false
end

#save!(**options) ⇒ void



20
21
22
# File 'lib/active_record_compose/validations.rb', line 20

def save!(**options)
  perform_validations(options) ? super : raise_validation_error
end

#valid?(context = nil) ⇒ Boolean

Runs all the validations and returns the result as true or false.

Parameters:

  • context (defaults to: nil)

    Validation context.

Returns:

  • (Boolean)

    true on success, false on failure.



28
# File 'lib/active_record_compose/validations.rb', line 28

def valid?(context = nil) = context_for_override_validation.with_override(context) { super }

#validate(context = nil) ⇒ Boolean

Alias for #valid?

Parameters:

  • context (defaults to: nil)

Returns:

  • (Boolean)

    true on success, false on failure.

See Also:



# File 'lib/active_record_compose/validations.rb', line 30

#validate!(context = nil) ⇒ void

Runs all the validations within the specified context. no errors are found, raises ActiveRecord::RecordInvalid otherwise.

Parameters:

  • context (defaults to: nil)

    Validation context.

Raises:

  • ActiveRecord::RecordInvalid

See Also:



# File 'lib/active_record_compose/validations.rb', line 36