Skip to content

Commit a05204f

Browse files
authored
Merge pull request #185 from kbrock/delegate_type
Require type for delegation
2 parents 99f1d8b + 8d484bd commit a05204f

File tree

5 files changed

+17
-19
lines changed

5 files changed

+17
-19
lines changed

lib/active_record/virtual_attributes/virtual_delegates.rb

Lines changed: 4 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)
@@ -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})

spec/db/models.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class VirtualTotalTestBase < ActiveRecord::Base # rubocop:disable Rails/ApplicationRecord
1+
class VirtualTotalTestBase < ActiveRecord::Base # rubocop:disable Rails/ApplicationRecord
22
self.abstract_class = true
33

44
include VirtualFields

spec/support/with_test_class.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#
1414
RSpec.shared_context 'with test_class', :with_test_class do
1515
before do
16-
class TestClassBase < ActiveRecord::Base
16+
class TestClassBase < ActiveRecord::Base # rubocop:disable Rails/ApplicationRecord
1717
self.abstract_class = true
1818

1919
include VirtualFields

spec/virtual_attributes_spec.rb

Lines changed: 6 additions & 6 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
@@ -208,7 +208,7 @@
208208
end
209209

210210
context "add_virtual_reflection integration" do
211-
it "with invalid parameters" do
211+
it "expects an association name" do
212212
expect { TestClass.virtual_has_one }.to raise_error(ArgumentError)
213213
end
214214

@@ -218,17 +218,17 @@
218218
expect(TestClass.virtual_reflection(:vref1).name).to eq(:vref1)
219219
end
220220

221-
it("with has_one macro") do
221+
it "with has_one macro" do
222222
TestClass.virtual_has_one(:vref1)
223223
expect(TestClass.virtual_reflection(:vref1).macro).to eq(:has_one)
224224
end
225225

226-
it("with has_many macro") do
226+
it "with has_many macro" do
227227
TestClass.virtual_has_many(:vref1)
228228
expect(TestClass.virtual_reflection(:vref1).macro).to eq(:has_many)
229229
end
230230

231-
it("with belongs_to macro") do
231+
it "with belongs_to macro" do
232232
TestClass.virtual_belongs_to(:vref1)
233233
expect(TestClass.virtual_reflection(:vref1).macro).to eq(:belongs_to)
234234
end

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)