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

#column_defaultsObject



168
169
170
171
172
173
174
175
176
177
178
# File 'activerecord/lib/active_record/locking/optimistic.rb', line 168

def column_defaults
  @column_defaults ||= begin
    defaults = super

    if defaults.key?(locking_column) && lock_optimistically
      defaults[locking_column] ||= 0
    end

    defaults
  end
end

#locking_columnObject

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



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

def locking_column
  reset_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.



140
141
142
143
# File 'activerecord/lib/active_record/locking/optimistic.rb', line 140

def locking_column=(value)
  @column_defaults = nil
  @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)


135
136
137
# File 'activerecord/lib/active_record/locking/optimistic.rb', line 135

def locking_enabled?
  lock_optimistically && columns_hash[locking_column]
end

#quoted_locking_columnObject

Quote the column name used for optimistic locking.



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

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.



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

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.



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

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