Module: ActiveRecord::Embedded::Model::Persistence

Extended by:
ActiveSupport::Concern
Included in:
ActiveRecord::Embedded::Model
Defined in:
lib/active_record/embedded/model/persistence.rb

Overview

Functionality for persisting an embedded model's data into the database. Includes basic CRUD methods and defines the callback lifecycle for Embedded::Model.

Instance Method Summary collapse

Instance Method Details

#destroyBoolean

Delete this model from its parent and the database.


71
72
73
74
75
# File 'lib/active_record/embedded/model/persistence.rb', line 71

def destroy
  run_callbacks :destroy do
    _association.destroy(_parent, id: id) && _parent.save
  end
end

#destroy!TrueClass

Delete this model. Throw an error if unsuccessful.


81
82
83
# File 'lib/active_record/embedded/model/persistence.rb', line 81

def destroy!
  destroy || raise(RecordNotDestroyed, self)
end

#new_record?Boolean

Whether this model does not exist in the database yet.


95
96
97
# File 'lib/active_record/embedded/model/persistence.rb', line 95

def new_record?
  !persisted?
end

#persisted?Boolean

Whether this model exists in the database.


88
89
90
# File 'lib/active_record/embedded/model/persistence.rb', line 88

def persisted?
  attributes[:id].present?
end

#reloadActiveRecord::Embedded::Model

Assign attributes to this model from the database, overwriting what is stored in memory.

Raises:

  • (RecordNotFound)

103
104
105
106
107
108
# File 'lib/active_record/embedded/model/persistence.rb', line 103

def reload
  raise RecordNotFound unless persisted?

  self.attributes = _association.find(_parent, id).attributes
  self
end

#save(validate: true) ⇒ Boolean

Attempt to persist this model to the database.


29
30
31
32
33
34
35
# File 'lib/active_record/embedded/model/persistence.rb', line 29

def save(validate: true)
  return false if validate && !valid?

  run_callbacks :save do
    persist! && _parent.save
  end
end

#save!TrueClass

Attempt to persist this model to the database. Throw an error if unsuccessful.

Raises:

  • (RecordNotSaved)

42
43
44
45
46
47
48
# File 'lib/active_record/embedded/model/persistence.rb', line 42

def save!
  raise RecordNotSaved, errors unless valid?

  run_callbacks :save do
    persist! && _parent.save!
  end
end

#update(params = {}) ⇒ Boolean

Mass-assign parameters to the data on this model.


53
54
55
# File 'lib/active_record/embedded/model/persistence.rb', line 53

def update(params = {})
  valid? && assign_attributes(params) && save
end

#update!(params = {}) ⇒ TrueClass

Mass-assign parameters to the data on this model. Throw an error if it does not succeed.

Raises:

  • (RecordNotValid)

62
63
64
65
66
# File 'lib/active_record/embedded/model/persistence.rb', line 62

def update!(params = {})
  raise RecordNotValid, errors unless valid?

  assign_attributes(params) && save!
end

#valid?Boolean

Run validations on this model.


22
23
24
# File 'lib/active_record/embedded/model/persistence.rb', line 22

def valid?(*)
  run_callbacks(:validation) { super }
end