aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2012-08-17 16:38:55 +0100
committerJon Leighton <j@jonathanleighton.com>2012-08-17 18:22:29 +0100
commit8fcd9b6e86274cc36820bfbf55a9d2967177339a (patch)
tree7b428b8052d702bafadbc63c9be7fa399802a793 /activerecord/lib/active_record
parentf396c01db25e45d1c2defc7dbb6f330f33453d04 (diff)
downloadrails-8fcd9b6e86274cc36820bfbf55a9d2967177339a.tar.gz
rails-8fcd9b6e86274cc36820bfbf55a9d2967177339a.tar.bz2
rails-8fcd9b6e86274cc36820bfbf55a9d2967177339a.zip
The default value can be set once in #column_defaults
Rather than doing it every time an instance is instantiated.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/locking/optimistic.rb18
-rw-r--r--activerecord/lib/active_record/model_schema.rb7
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
- # <tt>locking_enabled?</tt> at this point as
- # <tt>@attributes</tt> 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.