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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/tapioca/dsl/compilers/action_controller_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ def decorate

# Create helper method module
controller.create_module(helper_methods_name) do |helper_methods|
# If the controller has the same helpers module as the superclass
# just include superclass helpers
if helpers_module == constant.superclass._helpers
superclass_helper_methods = constant.superclass.const_get(helper_methods_name)
next helper_methods.create_include(qualified_name_of(superclass_helper_methods))
end

# If the controller has no helper defined, then it just inherits
# the Action Controlller base helper methods module, so we should
# just add that as an include and stop doing more processing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,7 @@ class UserController
def helpers; end

module HelperMethods
include ::ActionController::Base::HelperMethods

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.

def notify_user(user_id); end
include ::BaseController::HelperMethods
end

class HelperProxy < ::ActionView::Base
Expand Down