Permalink
Browse files

The default value can be set once in #column_defaults

Rather than doing it every time an instance is instantiated.
  • Loading branch information...
1 parent f396c01 commit 8fcd9b6e86274cc36820bfbf55a9d2967177339a @jonleighton jonleighton committed Aug 17, 2012
@@ -168,16 +168,16 @@ def update_counters(id, counters)
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
@@ -312,6 +312,13 @@ def reset_column_information
@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.

0 comments on commit 8fcd9b6

Please sign in to comment.