r4663@asus: jeremy | 2006-06-19 17:23:57 -0700

ActiveRecord::Locking is now ActiveRecord::Locking::Optimistic (to make way for Pessimistic.)


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4461 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper 2006-06-20 01:58:27 +00:00
parent 15aa6e0552
commit e4254939aa
2 changed files with 79 additions and 72 deletions

View File

@ -54,7 +54,7 @@ require 'active_record/xml_serialization'
ActiveRecord::Base.class_eval do
include ActiveRecord::Validations
include ActiveRecord::Locking
include ActiveRecord::Locking::Optimistic
include ActiveRecord::Callbacks
include ActiveRecord::Observing
include ActiveRecord::Timestamp

View File

@ -1,4 +1,5 @@
module ActiveRecord
module Locking
# Active Records support optimistic locking if the field <tt>lock_version</tt> is present. Each update to the
# record increments the lock_version column and the locking facilities ensure that records instantiated twice
# will let the last one saved raise a StaleObjectError if the first was also updated. Example:
@ -20,13 +21,24 @@ module ActiveRecord
# This behavior can be turned off by setting <tt>ActiveRecord::Base.lock_optimistically = false</tt>.
# To override the name of the lock_version column, invoke the <tt>set_locking_column</tt> method.
# This method uses the same syntax as <tt>set_table_name</tt>
module Locking
module Optimistic
def self.included(base) #:nodoc:
base.class_eval do
alias_method_chain :update, :lock
super
base.extend ClassMethods
base.cattr_accessor :lock_optimistically
base.lock_optimistically = true
base.alias_method_chain :update, :lock
class << base
alias_method :locking_column=, :set_locking_column
end
end
def locking_enabled? #:nodoc:
lock_optimistically && respond_to?(self.class.locking_column)
end
def update_with_lock #:nodoc:
return update_without_lock unless locking_enabled?
@ -47,31 +59,26 @@ module ActiveRecord
return true
end
end
class Base
@@lock_optimistically = true
cattr_accessor :lock_optimistically
module ClassMethods
DEFAULT_LOCKING_COLUMN = 'lock_version'
def locking_enabled? #:nodoc:
lock_optimistically && respond_to?(self.class.locking_column)
end
class << self
# Set the column to use for optimistic locking. Defaults to lock_version.
def set_locking_column(value = nil, &block)
define_attr_method :locking_column, value, &block
value
end
def locking_column #:nodoc:
# The version column used for optimistic locking. Defaults to lock_version.
def locking_column
reset_locking_column
end
def reset_locking_column #:nodoc:
default = 'lock_version'
set_locking_column(default)
default
# Reset the column used for optimistic locking back to the lock_version default.
def reset_locking_column
set_locking_column DEFAULT_LOCKING_COLUMN
end
end
end
end
end