Class: ActiveRecordCompose::Model
- Inherits:
-
Object
- Object
- ActiveRecordCompose::Model
- Includes:
- ActiveModel::Model
- Defined in:
- lib/active_record_compose/model.rb
Overview
This is the core class of ActiveRecordCompose.
By defining subclasses of this model, you can use ActiveRecordCompose functionality in your application.
It has the basic functionality of ActiveModel::Model
and ActiveModel::Attributes
,
and also provides aggregation of multiple models and atomic updates through transaction control.
Model Core collapse
-
.attribute_names ⇒ Array<String>
Returns a array of attribute name.
-
.delegate_attribute(*attributes, to: , allow_nil: false) ⇒ void
Provides a method of attribute access to the encapsulated model.
-
#attribute_names ⇒ Array<String>
Returns a array of attribute name.
-
#attributes ⇒ Hash<String, Object>
Returns a hash with the attribute name as key and the attribute value as value.
-
#initialize(attributes = {}) ⇒ Model
constructor
A new instance of Model.
-
#models ⇒ ActiveRecordCompose::ComposedCollection
private
Returns a collection of model elements to encapsulate.
-
#persisted? ⇒ Boolean
Returns true if model is persisted.
Validations collapse
-
#errors ⇒ ActiveModel::Errors
Returns the
ActiveModel::Errors
object that holds all information about attribute error messages. -
#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.
Persistences collapse
-
#save(**options) ⇒ Boolean
Save the models that exist in models.
-
#save!(**options) ⇒ void
Behavior is same to #save, but raises an exception prematurely on failure.
-
#update(attributes) ⇒ Boolean
Assign attributes and #save.
-
#update!(attributes) ⇒ void
Behavior is same to #update, but raises an exception prematurely on failure.
Callbacks collapse
-
.after_commit(*args, &block) ⇒ void
Registers a block to be called after the transaction is fully committed.
-
.after_create(*args, &block) ⇒ void
Registers a callback to be called after a model is created.
-
.after_rollback(*args, &block) ⇒ void
Registers a block to be called after the transaction is rolled back.
-
.after_save(*args, &block) ⇒ void
Registers a callback to be called after a model is saved.
-
.after_update(*args, &block) ⇒ void
Registers a callback to be called after a update is updated.
-
.around_create(*args, &block) ⇒ void
Registers a callback to be called around the creation of a model.
-
.around_save(*args, &block) ⇒ void
Registers a callback to be called around the save of a model.
-
.around_update(*args, &block) ⇒ void
Registers a callback to be called around the update of a model.
-
.before_create(*args, &block) ⇒ void
Registers a callback to be called before a model is created.
-
.before_save(*args, &block) ⇒ void
Registers a callback to be called before a model is saved.
-
.before_update(*args, &block) ⇒ void
Registers a callback to be called before a model is updated.
Constructor Details
#initialize(attributes = {}) ⇒ Model
Returns a new instance of Model.
356 357 358 |
# File 'lib/active_record_compose/model.rb', line 356 def initialize(attributes = {}) super end |
Class Method Details
.after_commit(*args, &block) ⇒ void
Registers a block to be called after the transaction is fully committed.
|
# File 'lib/active_record_compose/model.rb', line 345
|
.after_create(*args, &block) ⇒ void
Registers a callback to be called after a model is created.
|
# File 'lib/active_record_compose/model.rb', line 333
|
.after_rollback(*args, &block) ⇒ void
Registers a block to be called after the transaction is rolled back.
|
# File 'lib/active_record_compose/model.rb', line 348
|
.after_save(*args, &block) ⇒ void
Registers a callback to be called after a model is saved.
|
# File 'lib/active_record_compose/model.rb', line 324
|
.after_update(*args, &block) ⇒ void
Registers a callback to be called after a update is updated.
|
# File 'lib/active_record_compose/model.rb', line 342
|
.around_create(*args, &block) ⇒ void
Registers a callback to be called around the creation of a model.
|
# File 'lib/active_record_compose/model.rb', line 330
|
.around_save(*args, &block) ⇒ void
Registers a callback to be called around the save of a model.
|
# File 'lib/active_record_compose/model.rb', line 321
|
.around_update(*args, &block) ⇒ void
Registers a callback to be called around the update of a model.
|
# File 'lib/active_record_compose/model.rb', line 339
|
.attribute_names ⇒ Array<String>
Returns a array of attribute name. Attributes declared with delegate_attribute are also merged.
|
# File 'lib/active_record_compose/model.rb', line 109
|
.before_create(*args, &block) ⇒ void
Registers a callback to be called before a model is created.
|
# File 'lib/active_record_compose/model.rb', line 327
|
.before_save(*args, &block) ⇒ void
Registers a callback to be called before a model is saved.
|
# File 'lib/active_record_compose/model.rb', line 318
|
.before_update(*args, &block) ⇒ void
Registers a callback to be called before a model is updated.
|
# File 'lib/active_record_compose/model.rb', line 336
|
.delegate_attribute(*attributes, to: , allow_nil: false) ⇒ void
Provides a method of attribute access to the encapsulated model.
It provides a way to access the attributes of the model it encompasses, allowing transparent access as if it had those attributes itself.
|
# File 'lib/active_record_compose/model.rb', line 91
|
Instance Method Details
#attribute_names ⇒ Array<String>
Returns a array of attribute name. Attributes declared with delegate_attribute are also merged.
class Foo < ActiveRecordCompose::Base
def initialize(attributes = {})
@account = Account.new
super
end
attribute :confirmation, :boolean, default: false # plain attribute
delegate_attribute :name, to: :account # delegated attribute
private
attr_reader :account
end
Foo.attribute_names # Returns the merged state of plain and delegated attributes
# => ["confirmation" ,"name"]
foo = Foo.new
foo.attribute_names # Similar behavior for instance method version
# => ["confirmation", "name"]
|
# File 'lib/active_record_compose/model.rb', line 116
|
#attributes ⇒ Hash<String, Object>
Returns a hash with the attribute name as key and the attribute value as value. Attributes declared with delegate_attribute are also merged.
class Foo < ActiveRecordCompose::Base
def initialize(attributes = {})
@account = Account.new
super
end
attribute :confirmation, :boolean, default: false # plain attribute
delegate_attribute :name, to: :account # delegated attribute
private
attr_reader :account
end
foo = Foo.new
foo.name = "Alice"
foo.confirmation = true
foo.attributes # Returns the merged state of plain and delegated attributes
# => { "confirmation" => true, "name" => "Alice" }
|
# File 'lib/active_record_compose/model.rb', line 144
|
#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/model.rb', line 231
|
#models ⇒ ActiveRecordCompose::ComposedCollection (private)
Returns a collection of model elements to encapsulate.
371 |
# File 'lib/active_record_compose/model.rb', line 371 def models = @__models ||= ActiveRecordCompose::ComposedCollection.new(self) |
#persisted? ⇒ Boolean
Returns true if model is persisted.
By overriding this definition, you can control the callbacks that are triggered when a save is made. For example, returning false will trigger before_create, around_create and after_create, and returning true will trigger before_update, around_update and after_update.
|
# File 'lib/active_record_compose/model.rb', line 171
|
#save(**options) ⇒ Boolean
Save the models that exist in models. Returns false if any of the targets fail, true if all succeed.
The save is performed within a single transaction.
Only the :validate
option takes effect as it is required internally.
However, we do not recommend explicitly specifying validate: false
to skip validation.
Additionally, the :context
option is not accepted.
The need for such a value indicates that operations from multiple contexts are being processed.
If the contexts differ, we recommend separating them into different model definitions.
|
# File 'lib/active_record_compose/model.rb', line 271
|
#save!(**options) ⇒ void
Behavior is same to #save, but raises an exception prematurely on failure.
|
# File 'lib/active_record_compose/model.rb', line 290
|
#update(attributes) ⇒ Boolean
Assign attributes and #save.
|
# File 'lib/active_record_compose/model.rb', line 296
|
#update!(attributes) ⇒ void
Behavior is same to #update, but raises an exception prematurely on failure.
|
# File 'lib/active_record_compose/model.rb', line 304
|
#valid?(context = nil) ⇒ Boolean
Runs all the validations and returns the result as true or false.
|
# File 'lib/active_record_compose/model.rb', line 213
|
#validate(context = nil) ⇒ Boolean
Alias for #valid?
|
# File 'lib/active_record_compose/model.rb', line 218
|
#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/model.rb', line 224
|