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

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

Constant Summary collapse

DEFAULT_LOCKING_COLUMN =
'lock_version'

Instance Method Summary collapse

Instance Method Details

#initialize_attributes(attributes, options = {}) ⇒ Object

If the locking column has no default value set, start the lock version at zero. Note we can’t use locking_enabled? at this point as @attributes may not have been initialized yet.



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

def initialize_attributes(attributes, options = {}) #:nodoc:
  if attributes.key?(locking_column) && lock_optimistically
    attributes[locking_column] ||= 0
  end

  attributes
end

#locking_columnObject

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



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

def locking_column
  reset_locking_column unless defined?(@locking_column)
  @locking_column
end

#locking_column=(value) ⇒ Object



132
133
134
135
# File 'activerecord/lib/active_record/locking/optimistic.rb', line 132

def locking_column=(value)
  @original_locking_column = @locking_column if defined?(@locking_column)
  @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)


128
129
130
# File 'activerecord/lib/active_record/locking/optimistic.rb', line 128

def locking_enabled?
  lock_optimistically && columns_hash[locking_column]
end

#original_locking_columnObject

:nodoc:



148
149
150
# File 'activerecord/lib/active_record/locking/optimistic.rb', line 148

def original_locking_column #:nodoc:
  deprecated_original_property_getter :locking_column
end

#quoted_locking_columnObject

Quote the column name used for optimistic locking.



153
154
155
# File 'activerecord/lib/active_record/locking/optimistic.rb', line 153

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.



158
159
160
# File 'activerecord/lib/active_record/locking/optimistic.rb', line 158

def reset_locking_column
  self.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.



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

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

#update_counters(id, counters) ⇒ Object

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



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

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