Module: ActiveRecord::Locking::Optimistic::ClassMethods
- Defined in:
- lib/active_record/locking/optimistic.rb
Constant Summary collapse
- DEFAULT_LOCKING_COLUMN =
'lock_version'
Class Method Summary collapse
Instance Method Summary collapse
-
#locking_column ⇒ Object
The version column used for optimistic locking.
-
#locking_enabled? ⇒ Boolean
Is optimistic locking enabled for this table? 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).
-
#quoted_locking_column ⇒ Object
Quote the column name used for optimistic locking.
-
#reset_locking_column ⇒ Object
Reset the column used for optimistic locking back to the lock_version default.
-
#set_locking_column(value = nil, &block) ⇒ Object
Set the column to use for optimistic locking.
-
#update_counters_with_lock(id, counters) ⇒ Object
make sure the lock version column gets updated when counters are updated.
Class Method Details
.extended(base) ⇒ Object
100 101 102 103 104 |
# File 'lib/active_record/locking/optimistic.rb', line 100 def self.extended(base) class <<base alias_method_chain :update_counters, :lock end end |
Instance Method Details
#locking_column ⇒ Object
The version column used for optimistic locking. Defaults to lock_version.
121 122 123 |
# File 'lib/active_record/locking/optimistic.rb', line 121 def locking_column reset_locking_column end |
#locking_enabled? ⇒ Boolean
Is optimistic locking enabled for this table? 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).
110 111 112 |
# File 'lib/active_record/locking/optimistic.rb', line 110 def locking_enabled? lock_optimistically && columns_hash[locking_column] end |
#quoted_locking_column ⇒ Object
Quote the column name used for optimistic locking.
126 127 128 |
# File 'lib/active_record/locking/optimistic.rb', line 126 def quoted_locking_column connection.quote_column_name(locking_column) end |
#reset_locking_column ⇒ Object
Reset the column used for optimistic locking back to the lock_version default.
131 132 133 |
# File 'lib/active_record/locking/optimistic.rb', line 131 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.
115 116 117 118 |
# File 'lib/active_record/locking/optimistic.rb', line 115 def set_locking_column(value = nil, &block) define_attr_method :locking_column, value, &block value end |
#update_counters_with_lock(id, counters) ⇒ Object
make sure the lock version column gets updated when counters are updated.
137 138 139 140 |
# File 'lib/active_record/locking/optimistic.rb', line 137 def update_counters_with_lock(id, counters) counters = counters.merge(locking_column => 1) if locking_enabled? update_counters_without_lock(id, counters) end |