Skip to content

Add a DSL generator for requiring the right ancestors in helpers #621

@vinistock

Description

@vinistock

Because view helpers are dynamically included by Rails, Sorbet is not aware that the modules will be included into certain classes. We can solve this by generating RBIs that add the right requires_ancestor to each helper.

There are two possible behaviors outlined in the Rails documentation for include_all_helpers.

If include_all_helpers is turned on, then every helper is included into ActionView::Base. If not, then application_helper is the only one included into ActionView::Base and every other gets included only in the controller views they match with (e.g.: UsersHelper gets included only in views from UsersController, but not into views from other controllers).

Because Rails loads every module ending in Helper as a helper module, I suspect the DSL generator can just filter all_modules based on whether their name ends with Helper or not and then produce an RBI that looks like this (depending on the value of the configuration, which may alter the requires_ancestor statement).

# users_helper.rbi
# When include_all_helpers is turned on
module UsersHelper
  requires_ancestor { ActionView::Base }
end


# When include_all_helpers is turned off
module UsersHelper
  requires_ancestor { ??? } # Not sure if it would be UsersController or some internal Rails class
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions