Class: ActiveRecordCompose::Model
- Inherits:
-
Object
- Object
- ActiveRecordCompose::Model
- Includes:
- ActiveModel::Attributes, ActiveModel::Model, ActiveModel::Validations::Callbacks, ActiveRecord::Transactions, DelegateAttribute
- 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 ⇒ Object
- #save! ⇒ Object
-
#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 DelegateAttribute
Constructor Details
#initialize(attributes = {}) ⇒ Model
Returns a new instance of Model.
21 22 23 |
# File 'lib/active_record_compose/model.rb', line 21 def initialize(attributes = {}) super(attributes) 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!
58 59 60 61 62 63 |
# File 'lib/active_record_compose/model.rb', line 58 def create(attributes = {}) assign_attributes(attributes) return false if invalid? save_in_transaction { run_callbacks(:save) { run_callbacks(:create) { save_models } } } end |
#create!(attributes = {}) ⇒ Object
Behavior is same to ‘#create`, but raises an exception prematurely on failure.
67 68 69 |
# File 'lib/active_record_compose/model.rb', line 67 def create!(attributes = {}) create(attributes) || raise_on_save_error end |
#save ⇒ Object
25 26 27 28 29 |
# File 'lib/active_record_compose/model.rb', line 25 def save return false if invalid? save_in_transaction { run_callbacks(:save) { save_models } } end |
#save! ⇒ Object
31 |
# File 'lib/active_record_compose/model.rb', line 31 def save! = save || raise_on_save_error |
#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!
96 97 98 99 100 101 |
# File 'lib/active_record_compose/model.rb', line 96 def update(attributes = {}) assign_attributes(attributes) return false if invalid? save_in_transaction { run_callbacks(:save) { run_callbacks(:update) { save_models } } } end |
#update!(attributes = {}) ⇒ Object
Behavior is same to ‘#update`, but raises an exception prematurely on failure.
105 106 107 |
# File 'lib/active_record_compose/model.rb', line 105 def update!(attributes = {}) update(attributes) || raise_on_save_error end |