Skip to content

Commit

Permalink
Fix delegation in ActiveModel::Type::Registry
Browse files Browse the repository at this point in the history
* Without the change the new test fails like this:
  Failure:
  ActiveModel::Type::RegistryTest#test_a_class_can_be_registered_for_a_symbol [test/cases/type/registry_test.rb:16]:
  Expected: [{}, {}]
    Actual: [nil, nil]
* (*args, **kwargs)-delegation is not correct on Ruby 2.7 unless the
  target always accepts keyword arguments (not the case for `Array.new`).
  See https://eregon.me/blog/2021/02/13/correct-delegation-in-ruby-2-27-3.html
  • Loading branch information
eregon committed May 21, 2021
1 parent c453a5f commit 8ccc3bf
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
6 changes: 6 additions & 0 deletions activemodel/CHANGELOG.md
@@ -1,3 +1,9 @@
* Fix delegation in ActiveModel::Type::Registry#lookup

Passing a last positional argument `{}` would be incorrectly considered as keyword argument.

*Benoit Daloze*

* Cache and re-use generated attribute methods.

Generated methods with identical implementations will now share their instruction sequences
Expand Down
5 changes: 3 additions & 2 deletions activemodel/lib/active_model/type/registry.rb
Expand Up @@ -20,15 +20,16 @@ def register(type_name, klass = nil, &block)
registrations[type_name] = block
end

def lookup(symbol, *args, **kwargs)
def lookup(symbol, *args)
registration = registrations[symbol]

if registration
registration.call(symbol, *args, **kwargs)
registration.call(symbol, *args)
else
raise ArgumentError, "Unknown type #{symbol.inspect}"
end
end
ruby2_keywords(:lookup)

private
attr_reader :registrations
Expand Down
2 changes: 2 additions & 0 deletions activemodel/test/cases/type/registry_test.rb
Expand Up @@ -12,6 +12,8 @@ class RegistryTest < ActiveModel::TestCase

assert_equal "", registry.lookup(:foo)
assert_equal [], registry.lookup(:bar)
assert_equal [:a, :a], registry.lookup(:bar, 2, :a) # Array.new(2, :a)
assert_equal [{}, {}], registry.lookup(:bar, 2, {}) # Array.new(2, {})
end

test "a block can be registered" do
Expand Down

0 comments on commit 8ccc3bf

Please sign in to comment.