Skip to content

Commit 26640bb

Browse files
authored
Merge pull request #190 from kbrock/keywords
use kwargs for virtual_column and virtual_delegate
2 parents b63a0e1 + c0a7ac2 commit 26640bb

File tree

3 files changed

+7
-19
lines changed

3 files changed

+7
-19
lines changed

lib/active_record/virtual_attributes.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ module ClassMethods
5252
#
5353

5454
# Compatibility method: `virtual_attribute` is a more accurate name
55-
def virtual_column(name, **options)
56-
type = options.delete(:type)
57-
raise ArgumentError, "missing :type attribute" unless type
58-
55+
def virtual_column(name, type:, **options)
5956
virtual_attribute(name, type, **options)
6057
end
6158

lib/active_record/virtual_attributes/virtual_delegates.rb

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,8 @@ module ClassMethods
1818
# Definition
1919
#
2020

21-
def virtual_delegate(*methods)
22-
options = methods.extract_options!
23-
unless (to = options[:to])
24-
raise ArgumentError, 'Delegation needs an association. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, to: :greeter).'
25-
end
26-
27-
unless options[:type]
21+
def virtual_delegate(*methods, to:, type: nil, prefix: nil, allow_nil: nil, default: nil, **options) # rubocop:disable Naming/MethodParameterName
22+
unless type
2823
ActiveRecord::VirtualAttributes.deprecator.warn("Calling virtual_delegate without :type is now deprecated", caller)
2924
end
3025

@@ -34,26 +29,22 @@ def virtual_delegate(*methods)
3429
end
3530

3631
if to.count(".") > 1
37-
raise ArgumentError, 'Delegation needs a single association. Supply an option hash with a :to key with only 1 period (e.g. delegate :hello, to: "greeter.greeting")'
32+
raise ArgumentError, 'Delegation needs a single association. Supply keyword :to with only 1 period (e.g. delegate :hello, to: "greeter.greeting")'
3833
end
3934

40-
allow_nil = options[:allow_nil]
41-
default = options[:default]
42-
4335
# put method entry per method name.
4436
# This better supports reloading of the class and changing the definitions
4537
methods.each do |method|
46-
method_prefix = virtual_delegate_name_prefix(options[:prefix], to)
38+
method_prefix = virtual_delegate_name_prefix(prefix, to)
4739
method_name = "#{method_prefix}#{method}"
4840
if to.include?(".") # to => "target.method"
4941
to, method = to.split(".").map(&:to_sym)
50-
options[:to] = to
5142
end
5243

5344
define_delegate(method_name, method, :to => to, :allow_nil => allow_nil, :default => default)
5445

5546
self.virtual_delegates_to_define =
56-
virtual_delegates_to_define.merge(method_name => [method, options])
47+
virtual_delegates_to_define.merge(method_name => [method, options.merge(:to => to, :type => type)])
5748
end
5849
end
5950

spec/virtual_delegates_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
it "expects a ':to' for delegation" do
4242
expect do
4343
TestClass.virtual_delegate :col1
44-
end.to raise_error(ArgumentError, /needs an association/)
44+
end.to raise_error(ArgumentError, /missing keyword: :to/)
4545
end
4646

4747
it "expects a ':type' for delegation" do

0 commit comments

Comments
 (0)