aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-06-17 14:57:55 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-06-17 14:57:55 -0300
commit63b347df427ed2189fac7e75e36a3a4b9f6c2a68 (patch)
tree59278a753e92ff7a0e4ccc60ecdf87ce80d849e3 /activerecord/lib
parente7a8fda0336fe733adbdd0bdb642a09327ef22f1 (diff)
parente30d701afe08acaa1c04da50791f07d4bb4efa83 (diff)
downloadrails-63b347df427ed2189fac7e75e36a3a4b9f6c2a68.tar.gz
rails-63b347df427ed2189fac7e75e36a3a4b9f6c2a68.tar.bz2
rails-63b347df427ed2189fac7e75e36a3a4b9f6c2a68.zip
Merge pull request #15771 from sgrif/sg-stop-messing-with-column-defaults
Don't mess with `column_defaults` when optimistic locking is enabled
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/base.rb4
-rw-r--r--activerecord/lib/active_record/locking/optimistic.rb33
2 files changed, 25 insertions, 12 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 0b788ea1f9..662c99269e 100644
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -308,10 +308,10 @@ module ActiveRecord #:nodoc:
include Integration
include Validations
include CounterCache
- include Locking::Optimistic
- include Locking::Pessimistic
include Attributes
include AttributeDecorators
+ include Locking::Optimistic
+ include Locking::Pessimistic
include AttributeMethods
include Callbacks
include Timestamp
diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb
index 4528d8783c..0a764fb7ad 100644
--- a/activerecord/lib/active_record/locking/optimistic.rb
+++ b/activerecord/lib/active_record/locking/optimistic.rb
@@ -53,6 +53,11 @@ module ActiveRecord
included do
class_attribute :lock_optimistically, instance_writer: false
self.lock_optimistically = true
+
+ is_lock_column = ->(name, _) { lock_optimistically && name == locking_column }
+ decorate_matching_attribute_types(is_lock_column, :_optimistic_locking) do |type|
+ LockingType.new(type)
+ end
end
def locking_enabled? #:nodoc:
@@ -141,7 +146,7 @@ module ActiveRecord
# Set the column to use for optimistic locking. Defaults to +lock_version+.
def locking_column=(value)
- @column_defaults = nil
+ clear_caches_calculated_from_columns
@locking_column = value.to_s
end
@@ -162,18 +167,26 @@ module ActiveRecord
counters = counters.merge(locking_column => 1) if locking_enabled?
super
end
+ end
+ end
- def column_defaults
- @column_defaults ||= begin
- defaults = super
+ class LockingType < SimpleDelegator
+ def type_cast_from_database(value)
+ # `nil` *should* be changed to 0
+ super.to_i
+ end
- if defaults.key?(locking_column) && lock_optimistically
- defaults[locking_column] ||= 0
- end
+ def changed?(old_value, *)
+ # Ensure we save if the default was `nil`
+ super || old_value == 0
+ end
- defaults
- end
- end
+ def init_with(coder)
+ __setobj__(coder['subtype'])
+ end
+
+ def encode_with(coder)
+ coder['subtype'] = __getobj__
end
end
end