Sorbet sig conflict with ViewComponent collection rendering

Published Nov 11, 2020

Error message:

FooComponent initializer must accept `foo` collection parameter.

Solutions that didn’t work:

  1. ::Method.prepend(T::CompatibilityPatches::MethodExtensions)

    The methods we’re looking at turn out to be ::UnboundMethod instances.

  2. ::UnboundMethod.prepend(T::CompatibilityPatches::MethodExtensions)

    Infinite recursion somehow!

Solution that did work:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
module ViewComponentSorbetMonkeyPatch
  def self.prepended(base)
    base.singleton_class.send(:prepend, ClassMethods)
  end

  module ClassMethods
    def initialize_parameters
      initialize = instance_method(:initialize)
      # Copied from T::CompaibilityPatches::MethodExtensions#parameters
      sig = T::Private::Methods.signature_for_method(initialize)
      sig ? sig.method.parameters : super
    end
  end
end

ViewComponent::Base.prepend(ViewComponentSorbetMonkeyPatch)

I’ll definitely be cleaning this up and writing a real blog post about it. This deserves a real explanation but I have to find one first!