123456789_123456789_123456789_123456789_123456789_

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

Relationships & Source Files
Defined in: activerecord/lib/active_record/locking/optimistic.rb

Constant Summary

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#locking_column (rw)

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

[ GitHub ]

  
# File 'activerecord/lib/active_record/locking/optimistic.rb', line 171

attr_reader :locking_column

#locking_column=(value) (rw)

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

[ GitHub ]

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

def locking_column=(value)
  reload_schema_from_cache
  @locking_column = value.to_s
end

#locking_enabled?Boolean (readonly)

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).

[ GitHub ]

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

def locking_enabled?
  lock_optimistically && columns_hash[locking_column]
end

Instance Method Details

#hook_attribute_type(name, cast_type) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/locking/optimistic.rb', line 186

def hook_attribute_type(name, cast_type)
  if lock_optimistically && name == locking_column
    cast_type = LockingType.new(cast_type)
  end

  super
end

#inherited(base) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/locking/optimistic.rb', line 194

def inherited(base)
  super
  base.class_eval do
    @locking_column = DEFAULT_LOCKING_COLUMN
  end
end

#reset_locking_column

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

[ GitHub ]

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

def reset_locking_column
  self.locking_column = DEFAULT_LOCKING_COLUMN
end

#update_counters(id, counters)

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

[ GitHub ]

  
# File 'activerecord/lib/active_record/locking/optimistic.rb', line 180

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