@@ -18,11 +18,7 @@ module ClassMethods
1818 # Definition
1919 #
2020
21- def virtual_delegate ( *methods , to :, type : nil , prefix : nil , allow_nil : nil , default : nil , **options ) # rubocop:disable Naming/MethodParameterName
22- unless type
23- ActiveRecord ::VirtualAttributes . deprecator . warn ( "Calling virtual_delegate without :type is now deprecated" , caller )
24- end
25-
21+ def virtual_delegate ( *methods , to :, type :, prefix : nil , allow_nil : nil , default : nil , **options ) # rubocop:disable Naming/MethodParameterName
2622 to = to . to_s
2723 if to . include? ( "." ) && ( methods . size > 1 || prefix )
2824 raise ArgumentError , 'Delegation only supports specifying a target method name when defining a single virtual method with no prefix'
@@ -54,15 +50,15 @@ def virtual_delegate(*methods, to:, type: nil, prefix: nil, allow_nil: nil, defa
5450 # @option options :to [Symbol] name of the association from the source class to be referenced
5551 # @option options :arel [Proc] (optional and not common)
5652 # @option options :uses [Array|Symbol|Hash] sql includes hash. (default: to)
53+ # @option options :type [Symbol|ActiveModel::Type::Value] type for the attribute
5754 def define_virtual_delegate ( method_name , col , options )
5855 unless ( to = options [ :to ] ) && ( to_ref = reflection_with_virtual ( to . to_s ) )
5956 raise ArgumentError , 'Delegation needs an association. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, to: :greeter).'
6057 end
6158
6259 col = col . to_s
63- type = options [ :type ] || to_ref . klass . type_for_attribute ( col )
60+ type = options [ :type ]
6461 type = ActiveRecord ::Type . lookup ( type ) if type . kind_of? ( Symbol )
65- raise "unknown attribute #{ to } ##{ col } referenced in #{ name } " unless type
6662
6763 arel = virtual_delegate_arel ( col , to_ref )
6864 define_virtual_attribute ( method_name , type , :uses => ( options [ :uses ] || to ) , :arel => arel )
@@ -85,6 +81,7 @@ def define_delegate(method_name, method, to: nil, allow_nil: nil, default: nil)
8581 # On the other hand it could be that the target has side-effects,
8682 # whereas conceptually, from the user point of view, the delegator should
8783 # be doing one call.
84+ # NOTE: This is based upon ActiveSupport 6.0 delegate.rb, but we added has_attribute? and default
8885 if allow_nil
8986 method_def = <<-METHOD
9087 def #{ method_name } (#{ definition } )
0 commit comments