Skip to content

Commit 79611fe

Browse files
committed
virtual_delegate requires type
1 parent 99f1d8b commit 79611fe

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

lib/active_record/virtual_attributes/virtual_delegates.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

spec/virtual_attributes_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
end
88

99
context ".virtual_column" do
10-
it "with invalid parameters" do
11-
expect { TestClass.virtual_column :vcol1 }.to raise_error(ArgumentError)
10+
it "expects a :type parameter" do
11+
expect { TestClass.virtual_column :vcol1 }.to raise_error(ArgumentError, /missing keyword: :type/)
1212
end
1313

1414
it "with symbol name" do

spec/virtual_delegates_spec.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@
4040
context "invalid" do
4141
it "expects a ':to' for delegation" do
4242
expect do
43-
TestClass.virtual_delegate :col1
43+
TestClass.virtual_delegate :col1, :type => :integer
4444
end.to raise_error(ArgumentError, /missing keyword: :to/)
4545
end
4646

4747
it "expects a ':type' for delegation" do
48-
expect(ActiveRecord::VirtualAttributes.deprecator).to receive(:warn).with(/type/, anything)
49-
TestClass.virtual_delegate :col1, :to => :ref1
50-
TestClass.new
48+
expect do
49+
TestClass.virtual_delegate :col1, :to => :ref1
50+
TestClass.new
51+
end.to raise_error(ArgumentError, /missing keyword: :type/)
5152
end
5253

5354
it "only allows 1 method when delegating to a specific method" do

0 commit comments

Comments
 (0)