Class: ActiveRecordCompose::Model
- Inherits:
-
Object
- Object
- ActiveRecordCompose::Model
- Includes:
- ActiveModel::Attributes, ActiveModel::Model, ActiveModel::Validations::Callbacks, DelegateAttribute, TransactionSupport
- Defined in:
- lib/active_record_compose/model.rb
Instance Method Summary collapse
-
#create(attributes = {}) ⇒ Object
Behavior is same to ‘#save`, but `before_create` and `after_create` hooks fires.
-
#create!(attributes = {}) ⇒ Object
Behavior is same to ‘#create`, but raises an exception prematurely on failure.
-
#initialize(attributes = {}) ⇒ Model
constructor
A new instance of Model.
-
#save ⇒ Boolean
Save the models that exist in models.
-
#save! ⇒ Object
Save the models that exist in models.
-
#update(attributes = {}) ⇒ Object
Behavior is same to ‘#save`, but `before_update` and `after_update` hooks fires.
-
#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
Constructor Details
#initialize(attributes = {}) ⇒ Model
Returns a new instance of Model.
22 23 24 |
# File 'lib/active_record_compose/model.rb', line 22 def initialize(attributes = {}) super end |
Instance Method Details
#create(attributes = {}) ⇒ Object
Behavior is same to ‘#save`, but `before_create` and `after_create` hooks fires.
class ComposedModel < ActiveRecordCompose::Model
# ...
before_save { puts 'before_save called!' }
before_create { puts 'before_create called!' }
before_update { puts 'before_update called!' }
after_save { puts 'after_save called!' }
after_create { puts 'after_create called!' }
after_update { puts 'after_update called!' }
end
model = ComposedModel.new
model.save
# before_save called!
# after_save called!
model.create
# before_save called!
# before_create called!
# after_create called!
# after_save called!
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/active_record_compose/model.rb', line 80 def create(attributes = {}) assign_attributes(attributes) return false if invalid? with_transaction_returning_status do run_callbacks(:save) { run_callbacks(:create) { save_models(bang: false) } } rescue ActiveRecord::RecordInvalid false end end |
#create!(attributes = {}) ⇒ Object
Behavior is same to ‘#create`, but raises an exception prematurely on failure.
93 94 95 96 97 98 99 100 |
# File 'lib/active_record_compose/model.rb', line 93 def create!(attributes = {}) assign_attributes(attributes) valid? || raise_validation_error with_transaction_returning_status do run_callbacks(:save) { run_callbacks(:create) { save_models(bang: true) } } end || raise_on_save_error end |
#save ⇒ 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.
32 33 34 35 36 37 38 39 40 |
# File 'lib/active_record_compose/model.rb', line 32 def save return false if invalid? with_transaction_returning_status do run_callbacks(:save) { save_models(bang: false) } rescue ActiveRecord::RecordInvalid false end end |
#save! ⇒ 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.
47 48 49 50 51 52 53 |
# File 'lib/active_record_compose/model.rb', line 47 def save! valid? || raise_validation_error with_transaction_returning_status do run_callbacks(:save) { save_models(bang: true) } end || raise_on_save_error end |
#update(attributes = {}) ⇒ Object
Behavior is same to ‘#save`, but `before_update` and `after_update` hooks fires.
class ComposedModel < ActiveRecordCompose::Model
# ...
before_save { puts 'before_save called!' }
before_create { puts 'before_create called!' }
before_update { puts 'before_update called!' }
after_save { puts 'after_save called!' }
after_create { puts 'after_create called!' }
after_update { puts 'after_update called!' }
end
model = ComposedModel.new
model.save
# before_save called!
# after_save called!
model.update
# before_save called!
# before_update called!
# after_update called!
# after_save called!
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/active_record_compose/model.rb', line 127 def update(attributes = {}) assign_attributes(attributes) return false if invalid? with_transaction_returning_status do run_callbacks(:save) { run_callbacks(:update) { save_models(bang: false) } } rescue ActiveRecord::RecordInvalid false end end |
#update!(attributes = {}) ⇒ Object
Behavior is same to ‘#update`, but raises an exception prematurely on failure.
140 141 142 143 144 145 146 147 |
# File 'lib/active_record_compose/model.rb', line 140 def update!(attributes = {}) assign_attributes(attributes) valid? || raise_validation_error with_transaction_returning_status do run_callbacks(:save) { run_callbacks(:update) { save_models(bang: true) } } end || raise_on_save_error end |