Module: ActiveRecord::Locking::Optimistic::ClassMethods

Defined in:
activerecord/lib/active_record/locking/optimistic.rb

Constant Summary collapse

DEFAULT_LOCKING_COLUMN =
"lock_version"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#locking_columnObject

The version column used for optimistic locking. Defaults to lock_version.



172
173
174
# File 'activerecord/lib/active_record/locking/optimistic.rb', line 172

def locking_column
  @locking_column
end

Instance Method Details

#define_attribute(name, cast_type) ⇒ Object

:nodoc:



186
187
188
189
190
191
# File 'activerecord/lib/active_record/locking/optimistic.rb', line 186

def define_attribute(name, cast_type, **) # :nodoc:
  if lock_optimistically && name == locking_column
    cast_type = LockingType.new(cast_type)
  end
  super
end

#locking_enabled?Boolean

Returns true if the lock_optimistically flag is set to true (which it is, by default) and the table includes the locking_column column (defaults to lock_version).

Returns:

  • (Boolean)


161
162
163
# File 'activerecord/lib/active_record/locking/optimistic.rb', line 161

def locking_enabled?
  lock_optimistically && columns_hash[locking_column]
end

#reset_locking_columnObject

Reset the column used for optimistic locking back to the lock_version default.



175
176
177
# File 'activerecord/lib/active_record/locking/optimistic.rb', line 175

def reset_locking_column
  self.locking_column = DEFAULT_LOCKING_COLUMN
end

#update_counters(id, counters) ⇒ Object

Make sure the lock version column gets updated when counters are updated.



181
182
183
184
# File 'activerecord/lib/active_record/locking/optimistic.rb', line 181

def update_counters(id, counters)
  counters = counters.merge(locking_column => 1) if locking_enabled?
  super
end