From 8fcd9b6e86274cc36820bfbf55a9d2967177339a Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Fri, 17 Aug 2012 16:38:55 +0100 Subject: The default value can be set once in #column_defaults Rather than doing it every time an instance is instantiated. --- activerecord/lib/active_record/locking/optimistic.rb | 18 +++++++++--------- activerecord/lib/active_record/model_schema.rb | 7 +++++++ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb index e0344f3c56..e96ed00f9c 100644 --- a/activerecord/lib/active_record/locking/optimistic.rb +++ b/activerecord/lib/active_record/locking/optimistic.rb @@ -168,16 +168,16 @@ module ActiveRecord super end - # 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. - def initialize_attributes(attributes, options = {}) #:nodoc: - if attributes.key?(locking_column) && lock_optimistically - attributes[locking_column] ||= 0 - end + def column_defaults + @column_defaults ||= begin + defaults = super + + if defaults.key?(locking_column) && lock_optimistically + defaults[locking_column] ||= 0 + end - attributes + defaults + end end end end diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb index def48c03bf..ff7c996648 100644 --- a/activerecord/lib/active_record/model_schema.rb +++ b/activerecord/lib/active_record/model_schema.rb @@ -312,6 +312,13 @@ module ActiveRecord @relation = nil end + # This is a hook for use by modules that need to do extra stuff to + # attributes when they are initialized. (e.g. attribute + # serialization) + def initialize_attributes(attributes, options = {}) #:nodoc: + attributes + end + private # Guesses the table name, but does not decorate it with prefix and suffix information. -- cgit v1.2.3