Skip to content

Commit 476371d

Browse files
committed
Drop virtual_delegates_to_define
By defining the ruby method up front and registering the arel and uses, the code can use the new simplified virtual attribute registration process. Care does need to go into ensuring that virtual_delegate_arel does not access the schema Do note, we are not passing options into virtual_attributes, which get passed to Type.lookup. This was not working for virtual_delegates before. TBH/ I don't know if we use this in the type lookup. Maybe these are be used when using array types? It looks like rails uses the options for the matchers. It wasn't fully clear for me.
1 parent 7cec00f commit 476371d

File tree

2 files changed

+1
-29
lines changed

2 files changed

+1
-29
lines changed

lib/active_record/virtual_attributes.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,6 @@ def load_schema!
9898

9999
define_virtual_attribute(name, type)
100100
end
101-
102-
virtual_delegates_to_define.each do |method_name, (method, options)|
103-
define_virtual_delegate(method_name, method, options)
104-
end
105101
end
106102

107103
def define_virtual_attribute(name, cast_type)

lib/active_record/virtual_attributes/virtual_delegates.rb

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ module VirtualAttributes
99
module VirtualDelegates
1010
extend ActiveSupport::Concern
1111

12-
included do
13-
class_attribute :virtual_delegates_to_define, :instance_accessor => false, :default => {}
14-
end
15-
1612
module ClassMethods
1713
#
1814
# Definition
@@ -37,32 +33,12 @@ def virtual_delegate(*methods, to:, type:, prefix: nil, allow_nil: nil, default:
3733
end
3834

3935
define_delegate(method_name, method, :to => to, :allow_nil => allow_nil, :default => default)
40-
define_virtual_include(method_name, uses || to)
41-
define_virtual_arel(method_name, virtual_delegate_arel(method, to_ref))
42-
self.virtual_delegates_to_define =
43-
virtual_delegates_to_define.merge(method_name.to_s => [method, options.merge(:to => to, :type => type)])
36+
virtual_attribute(method_name, type, :uses => (uses || to), :arel => virtual_delegate_arel(method, to_ref), **options)
4437
end
4538
end
4639

4740
private
4841

49-
# define virtual_attribute for delegates
50-
#
51-
# this is called at schema load time (and not at class definition time)
52-
#
53-
# @param method_name [Symbol] name of the attribute on the source class to be defined
54-
# @param col [Symbol] name of the attribute on the associated class to be referenced
55-
# @option options :to [Symbol] name of the association from the source class to be referenced
56-
# @option options :arel [Proc] (optional and not common)
57-
# @option options :uses [Array|Symbol|Hash] sql includes hash. (default: to)
58-
# @option options :type [Symbol|ActiveModel::Type::Value] type for the attribute
59-
def define_virtual_delegate(method_name, col, options)
60-
type = options[:type]
61-
type = ActiveRecord::Type.lookup(type) if type.kind_of?(Symbol)
62-
63-
define_virtual_attribute(method_name, type)
64-
end
65-
6642
# see activesupport module/delegation.rb
6743
# rubocop:disable Style/TernaryParentheses
6844
def define_delegate(method_name, method, to: nil, allow_nil: nil, default: nil) # rubocop:disable Naming/MethodParameterName

0 commit comments

Comments
 (0)