1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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`.
This commit is contained in:
Sean Griffin & Sean Doyle 2014-06-27 10:23:43 -06:00 committed by Sean Griffin
parent 11ac0cad08
commit ccc1d3dbbe
3 changed files with 38 additions and 17 deletions

View file

@ -26,7 +26,7 @@ module ActiveRecord
def add_user_provided_columns(*) def add_user_provided_columns(*)
super.map do |column| super.map do |column|
decorated_type = attribute_type_decorations.apply(self, column.name, column.cast_type) decorated_type = attribute_type_decorations.apply(column.name, column.cast_type)
column.with_type(decorated_type) column.with_type(decorated_type)
end end
end end
@ -43,8 +43,8 @@ module ActiveRecord
TypeDecorator.new(@decorations.merge(*args)) TypeDecorator.new(@decorations.merge(*args))
end end
def apply(context, name, type) def apply(name, type)
decorations = decorators_for(context, name, type) decorations = decorators_for(name, type)
decorations.inject(type) do |new_type, block| decorations.inject(type) do |new_type, block|
block.call(new_type) block.call(new_type)
end end
@ -52,13 +52,13 @@ module ActiveRecord
private private
def decorators_for(context, name, type) def decorators_for(name, type)
matching(context, name, type).map(&:last) matching(name, type).map(&:last)
end end
def matching(context, name, type) def matching(name, type)
@decorations.values.select do |(matcher, _)| @decorations.values.select do |(matcher, _)|
context.instance_exec(name, type, &matcher) matcher.call(name, type)
end end
end end
end end

View file

@ -33,15 +33,25 @@ module ActiveRecord
class_attribute :skip_time_zone_conversion_for_attributes, instance_writer: false class_attribute :skip_time_zone_conversion_for_attributes, instance_writer: false
self.skip_time_zone_conversion_for_attributes = [] self.skip_time_zone_conversion_for_attributes = []
matcher = ->(name, type) { create_time_zone_conversion_attribute?(name, type) }
decorate_matching_attribute_types(matcher, :_time_zone_conversion) do |type|
TimeZoneConverter.new(type)
end
end end
module ClassMethods module ClassMethods
private private
def inherited(subclass)
# 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 `time_zone_aware_attributes`, or
# `skip_time_zone_conversion_for_attributes` would not be picked up.
subclass.class_eval do
matcher = ->(name, type) { create_time_zone_conversion_attribute?(name, type) }
decorate_matching_attribute_types(matcher, :_time_zone_conversion) do |type|
TimeZoneConverter.new(type)
end
end
super
end
def create_time_zone_conversion_attribute?(name, cast_type) def create_time_zone_conversion_attribute?(name, cast_type)
time_zone_aware_attributes && time_zone_aware_attributes &&
!self.skip_time_zone_conversion_for_attributes.include?(name.to_sym) && !self.skip_time_zone_conversion_for_attributes.include?(name.to_sym) &&

View file

@ -53,11 +53,6 @@ module ActiveRecord
included do included do
class_attribute :lock_optimistically, instance_writer: false class_attribute :lock_optimistically, instance_writer: false
self.lock_optimistically = true 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 end
def locking_enabled? #:nodoc: def locking_enabled? #:nodoc:
@ -167,6 +162,22 @@ module ActiveRecord
counters = counters.merge(locking_column => 1) if locking_enabled? counters = counters.merge(locking_column => 1) if locking_enabled?
super super
end 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
end end