Module: Lockless::Model

Extended by:
ActiveSupport::Concern
Defined in:
lib/lockless/model.rb

Overview

Options:

  • :lockless_column - The columns used to track soft delete, defaults to `:lockless_uuid`.

Instance Method Summary collapse

Instance Method Details

#lockless_saveBoolean

Saves record if it has not be modified in the time after it was loaded from the database.

false is returned if record is outdated or invalid

Returns:

  • (Boolean)

    Similar to `.save`, true is returned if the model is updated


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/lockless/model.rb', line 32

def lockless_save
  return false unless valid?
  old_lockless_uuid = public_send(lockless_column)
  return save if new_record?

  run_callbacks(:save) do |variable|
    new_attrs = changed.collect { |prop| [prop.to_sym, self[prop]] }.to_h

    update_count = self.class.where(:id => id, lockless_column => old_lockless_uuid).update_all(new_attrs)
    if update_count == 1
      changes_applied
      true
    else
      public_send("#{lockless_column}=", old_lockless_uuid)
      false
    end
  end
end

#lockless_save!Boolean

Saves record if it has not be modified in the time after it was loaded from the database.

false is returned if record is outdated

Returns:

  • (Boolean)

    Similar to `.save!`, true is returned if the model is updated

Raises:

  • (ActiveRecord::RecordInvalid)

    if record is invalid


57
58
59
60
# File 'lib/lockless/model.rb', line 57

def lockless_save!
  validate!
  lockless_save
end