Module: ActiveRecordCompose::Persistence

Extended by:
ActiveSupport::Concern
Includes:
TransactionSupport
Included in:
Model
Defined in:
lib/active_record_compose/persistence.rb

Instance Method Summary collapse

Methods included from TransactionSupport

#id, #restore_transaction_record_state, #trigger_transactional_callbacks?

Instance Method Details

#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.

Returns:

  • (Boolean)

    returns true on success, false on failure.



24
25
26
27
28
29
30
# File 'lib/active_record_compose/persistence.rb', line 24

def save(**options)
  with_transaction_returning_status do
    with_callbacks { save_models(**options, 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.



43
44
45
46
47
# File 'lib/active_record_compose/persistence.rb', line 43

def save!(**options)
  with_transaction_returning_status do
    with_callbacks { save_models(**options, bang: true) }
  end || raise_on_save_error
end

#update(attributes = {}) ⇒ Boolean

Assign attributes and save.

Returns:

  • (Boolean)

    returns true on success, false on failure.



52
53
54
55
56
57
# File 'lib/active_record_compose/persistence.rb', line 52

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.



61
62
63
64
65
66
# File 'lib/active_record_compose/persistence.rb', line 61

def update!(attributes = {})
  with_transaction_returning_status do
    assign_attributes(attributes)
    save!
  end
end