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
-
#errors ⇒ ActiveModel::Errors
Returns the
ActiveModel::Errorsobject that holds all information about attribute error messages. - #save(**options) ⇒ void
- #save!(**options) ⇒ void
-
#valid?(context = nil) ⇒ Boolean
Runs all the validations and returns the result as true or false.
-
#validate(context = nil) ⇒ Boolean
Alias for #valid?.
-
#validate!(context = nil) ⇒ void
Runs all the validations within the specified context.
Instance Method Details
#errors ⇒ ActiveModel::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 << account
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]
|
|
# 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(**) perform_validations() ? super : false end |
#save!(**options) ⇒ void
20 21 22 |
# File 'lib/active_record_compose/validations.rb', line 20 def save!(**) perform_validations() ? super : raise_validation_error end |
#valid?(context = nil) ⇒ Boolean
Runs all the validations and returns the result as true or false.
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?
|
|
# 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.
|
|
# File 'lib/active_record_compose/validations.rb', line 36
|