Class: ActiveRecordCompose::Model
- Inherits:
-
Object
- Object
- ActiveRecordCompose::Model
- Includes:
- ActiveModel::Attributes, ActiveModel::Model, ActiveModel::Validations::Callbacks, AttributeQuerying, Callbacks, DelegateAttribute, TransactionSupport, Validations
- Defined in:
- lib/active_record_compose/model.rb
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ Model
constructor
A new instance of Model.
-
#persisted? ⇒ Boolean
Returns true if model is persisted.
-
#save(**options) ⇒ Boolean
Save the models that exist in models.
-
#save!(**options) ⇒ Object
Save the models that exist in models.
-
#update(attributes = {}) ⇒ Boolean
Assign attributes and save.
-
#update!(attributes = {}) ⇒ Object
Behavior is same to
#update, but raises an exception prematurely on failure.
Methods included from TransactionSupport
#id, #restore_transaction_record_state, #trigger_transactional_callbacks?
Methods included from DelegateAttribute
Methods included from Validations
Constructor Details
#initialize(attributes = {}) ⇒ Model
Returns a new instance of Model.
26 27 28 |
# File 'lib/active_record_compose/model.rb', line 26 def initialize(attributes = {}) super end |
Instance Method Details
#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.
93 |
# File 'lib/active_record_compose/model.rb', line 93 def persisted? = super |
#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.
42 43 44 45 46 47 48 |
# File 'lib/active_record_compose/model.rb', line 42 def save(**) with_transaction_returning_status do with_callbacks { save_models(**, bang: false) } rescue ActiveRecord::RecordInvalid false end end |
#save!(**options) ⇒ Object
Save the models that exist in models. Unlike #save, an exception is raises on failure.
Saving, like #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.
61 62 63 64 65 |
# File 'lib/active_record_compose/model.rb', line 61 def save!(**) with_transaction_returning_status do with_callbacks { save_models(**, bang: true) } end || raise_on_save_error end |
#update(attributes = {}) ⇒ Boolean
Assign attributes and save.
70 71 72 73 74 75 |
# File 'lib/active_record_compose/model.rb', line 70 def update(attributes = {}) with_transaction_returning_status do assign_attributes(attributes) save end end |
#update!(attributes = {}) ⇒ Object
Behavior is same to #update, but raises an exception prematurely on failure.
79 80 81 82 83 84 |
# File 'lib/active_record_compose/model.rb', line 79 def update!(attributes = {}) with_transaction_returning_status do assign_attributes(attributes) save! end end |