Class: ActiveRecordCompose::Model

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Attributes, ActiveModel::Model, ActiveModel::Validations::Callbacks, ActiveRecord::Transactions, DelegateAttribute
Defined in:
lib/active_record_compose/model.rb

Instance Method Summary collapse

Methods included from DelegateAttribute

#attributes

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!


73
74
75
76
77
78
# File 'lib/active_record_compose/model.rb', line 73

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.



82
83
84
85
86
87
# File 'lib/active_record_compose/model.rb', line 82

def create!(attributes = {})
  assign_attributes(attributes)
  valid? || raise_validation_error

  save_in_transaction { run_callbacks(:save) { run_callbacks(:create) { save_models } } } || raise_on_save_error
end

#saveBoolean

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.

Returns:

  • (Boolean)

    returns true on success, false on failure.



31
32
33
34
35
# File 'lib/active_record_compose/model.rb', line 31

def save
  return false if invalid?

  save_in_transaction { run_callbacks(:save) { save_models } }
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.



42
43
44
45
46
# File 'lib/active_record_compose/model.rb', line 42

def save!
  valid? || raise_validation_error

  save_in_transaction { run_callbacks(:save) { save_models } } || 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!


114
115
116
117
118
119
# File 'lib/active_record_compose/model.rb', line 114

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.



123
124
125
126
127
128
# File 'lib/active_record_compose/model.rb', line 123

def update!(attributes = {})
  assign_attributes(attributes)
  valid? || raise_validation_error

  save_in_transaction { run_callbacks(:save) { run_callbacks(:update) { save_models } } } || raise_on_save_error
end