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

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

Constant Summary collapse

DEFAULT_LOCKING_COLUMN =
"lock_version"

Instance Method Summary collapse

Instance Method Details

#locking_columnObject

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



150
151
152
153
# File 'lib/active_record/locking/optimistic.rb', line 150

def locking_column
  @locking_column = DEFAULT_LOCKING_COLUMN unless defined?(@locking_column)
  @locking_column
end

#locking_column=(value) ⇒ Object

Set the column to use for optimistic locking. Defaults to lock_version.



144
145
146
147
# File 'lib/active_record/locking/optimistic.rb', line 144

def locking_column=(value)
  reload_schema_from_cache
  @locking_column = value.to_s
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)


139
140
141
# File 'lib/active_record/locking/optimistic.rb', line 139

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.



156
157
158
# File 'lib/active_record/locking/optimistic.rb', line 156

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.



162
163
164
165
# File 'lib/active_record/locking/optimistic.rb', line 162

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