Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include base controller helpers when possible #1299

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

waj
Copy link

@waj waj commented Dec 8, 2022

Motivation

In a Rails project is common to have helpers defined in a base controller. Currently those helpers are declared on every .rbi file for descendant controllers unless those controllers define new helpers. The problem with this approach is that whenever a slight change is made on the base controller helpers, most .rbi files for descendants get updated making reviews of PRs annoying.

Implementation

If the helpers module of a controller is the same helpers module of it superclass then it's guaranteed that the helpers methods are the same. So, instead of generating the helpers methods, use the helper methods module generated for the parent class.

Tests

There was already a test with the case I described, so I updated the test to check that the subclass module just includes the parent's class module.

@waj waj requested a review from a team as a code owner December 8, 2022 14:22
@waj waj force-pushed the include-base-class-helpers branch from 8d1db6c to d3e1fb7 Compare December 8, 2022 14:26
sig { returns(T.untyped) }
def current_user_name; end

sig { params(user_id: ::Integer).void }
Copy link
Contributor

@KaanOzkan KaanOzkan Dec 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the duplication is nice but I don't think it's worth losing the typing information in the signature. I believe depending on the loading order for Sorbet the signature in BaseController can take precedence.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not following, what typing information is lost? The helper methods are still properly defined in the base module with the type signature.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh... I see what you mean. I didn't notice in this case the signature for notify_user was different in the base class. I'll see how can I avoid this case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah now someone could write UserController.new.helpers.notify_user("Not an int") without any typechecking errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants