Skip to content

Commit

Permalink
Don't call ruby2_keywords for user supplied block
Browse files Browse the repository at this point in the history
  • Loading branch information
kamipo committed Jun 3, 2020
1 parent 8717744 commit 4dbbba4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
6 changes: 4 additions & 2 deletions activemodel/lib/active_model/type/registry.rb
Expand Up @@ -9,8 +9,10 @@ def initialize
end

def register(type_name, klass = nil, **options, &block)
block ||= proc { |_, *args| klass.new(*args) }
block.ruby2_keywords if block.respond_to?(:ruby2_keywords)
unless block_given?
block = proc { |_, *args| klass.new(*args) }
block.ruby2_keywords if block.respond_to?(:ruby2_keywords)
end
registrations << registration_klass.new(type_name, block, **options)
end

Expand Down
18 changes: 11 additions & 7 deletions activemodel/test/cases/type/registry_test.rb
Expand Up @@ -16,16 +16,20 @@ class RegistryTest < ActiveModel::TestCase

test "a block can be registered" do
registry = Type::Registry.new
registry.register(:foo) do |*args|
[*args, "block for foo"]
registry.register(:foo) do |type, *args|
[type, args, "block for foo"]
end
registry.register(:bar) do |*args|
[*args, "block for bar"]
registry.register(:bar) do |type, *args|
[type, args, "block for bar"]
end
registry.register(:baz) do |type, **kwargs|
[type, kwargs, "block for baz"]
end

assert_equal [:foo, 1, "block for foo"], registry.lookup(:foo, 1)
assert_equal [:foo, 2, "block for foo"], registry.lookup(:foo, 2)
assert_equal [:bar, 1, 2, 3, "block for bar"], registry.lookup(:bar, 1, 2, 3)
assert_equal [:foo, [1], "block for foo"], registry.lookup(:foo, 1)
assert_equal [:foo, [2], "block for foo"], registry.lookup(:foo, 2)
assert_equal [:bar, [1, 2, 3], "block for bar"], registry.lookup(:bar, 1, 2, 3)
assert_equal [:baz, { kw: 1 }, "block for baz"], registry.lookup(:baz, kw: 1)
end

test "a reasonable error is given when no type is found" do
Expand Down

0 comments on commit 4dbbba4

Please sign in to comment.