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.



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

def locking_column
  reset_locking_column
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)


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

def locking_enabled?
  lock_optimistically && columns_hash[locking_column]
end

#quoted_locking_columnObject

Quote the column name used for optimistic locking.



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

def quoted_locking_column
  connection.quote_column_name(locking_column)
end

#reset_locking_columnObject

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



165
166
167
# File 'lib/active_record/locking/optimistic.rb', line 165

def reset_locking_column
  set_locking_column DEFAULT_LOCKING_COLUMN
end

#set_locking_column(value = nil, &block) ⇒ Object

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



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

def set_locking_column(value = nil, &block)
  define_attr_method :locking_column, value, &block
  value
end

#update_counters(id, counters) ⇒ Object

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



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

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