aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/locking
diff options
context:
space:
mode:
authorSean Griffin & Sean Doyle <sean+seandoyle@thoughtbot.com>2014-06-27 10:23:43 -0600
committerSean Griffin <sean@thoughtbot.com>2014-06-27 11:33:32 -0600
commitccc1d3dbbec878309ea99839bb8ea2f8aca4dd72 (patch)
tree4678bf1eac2b61f9224b5300697e5b1252e060a3 /activerecord/lib/active_record/locking
parent11ac0cad081eb418dfabe8a427332347beb12a0e (diff)
downloadrails-ccc1d3dbbec878309ea99839bb8ea2f8aca4dd72.tar.gz
rails-ccc1d3dbbec878309ea99839bb8ea2f8aca4dd72.tar.bz2
rails-ccc1d3dbbec878309ea99839bb8ea2f8aca4dd72.zip
Stop using instance exec for type decorators
We are moving this behavior out to an object that we would like to keep separated from `ActiveRecord::Base`, which means not passing the class object to it. As such, we need to stop using `instance_exec`, and instead close over the subclass on global type decorators that are applied in `Base`.
Diffstat (limited to 'activerecord/lib/active_record/locking')
-rw-r--r--activerecord/lib/active_record/locking/optimistic.rb21
1 files changed, 16 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb
index f4ec13a177..52eeb8ae1f 100644
--- a/activerecord/lib/active_record/locking/optimistic.rb
+++ b/activerecord/lib/active_record/locking/optimistic.rb
@@ -53,11 +53,6 @@ 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:
@@ -167,6 +162,22 @@ module ActiveRecord
counters = counters.merge(locking_column => 1) if locking_enabled?
super
end
+
+ private
+
+ # We need to apply this decorator here, rather than on module inclusion. The closure
+ # created by the matcher would otherwise evaluate for `ActiveRecord::Base`, not the
+ # sub class being decorated. As such, changes to `lock_optimistically`, or
+ # `locking_column` would not be picked up.
+ def inherited(subclass)
+ subclass.class_eval do
+ 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
+ super
+ end
end
end