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

Fix HelperMethods definitions #1611

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/tapioca/dsl/compilers/action_controller_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ class << self

sig { override.returns(T::Enumerable[Module]) }
def gather_constants
descendants_of(::ActionController::Base).reject(&:abstract?).select(&:name)
descendants_of(::ActionController::Base).select(&:name).select do |klass|
klass.const_defined?(:HelperMethods, false)
end
end
end

Expand Down
147 changes: 88 additions & 59 deletions spec/tapioca/dsl/compilers/action_controller_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ class ActionControllerHelpersSpec < ::DslSpec
assert_empty(gathered_constants)
end

it "gathers only ActionController subclasses" do
it "gathers only ActionController subclasses with helpers" do
add_ruby_file("content.rb", <<~RUBY)
class UserController < ActionController::Base
helper_method :foo
end

class AnotherController < ActionController::Base
end

class User
Expand All @@ -32,76 +36,54 @@ module UserHelper

class UserController < ActionController::Base
include UserHelper
helper_method :foo
end
RUBY

assert_equal(["UserController"], gathered_constants)
end

it "gathers subclasses of ActionController subclasses" do
it "gathers subclasses of ActionController subclasses with helpers" do
add_ruby_file("content.rb", <<~RUBY)
class UserController < ActionController::Base
helper_method :foo
end

class HandController < UserController
helper_method :bar
end
RUBY

assert_equal(["HandController", "UserController"], gathered_constants)
end

it "ignores abstract subclasses of ActionController" do
it "gathers abstract subclasses of ActionController" do
add_ruby_file("content.rb", <<~RUBY)
class UserController < ActionController::Base
helper_method :foo
end

class HomeController < ActionController::Base
abstract!
helper_method :foo
end
RUBY

assert_equal(["UserController"], gathered_constants)
assert_equal(["HomeController", "UserController"], gathered_constants)
end

it "ignores anonymous subclasses of ActionController" do
add_ruby_file("content.rb", <<~RUBY)
Class.new(ActionController::Base)
Class.new(ActionController::Base) do
helper_method :foo
end
RUBY

assert_equal([], gathered_constants)
end
end

describe "decorate" do
it "generates empty helper module when there are no helper methods specified" do
add_ruby_file("controller.rb", <<~RUBY)
class UserController < ActionController::Base
def current_user_name
# ...
end
end
RUBY

expected = <<~RBI
# typed: strong

class UserController
sig { returns(HelperProxy) }
def helpers; end

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

class HelperProxy < ::ActionView::Base
include HelperMethods
end
end
RBI

assert_equal(expected, rbi_for(:UserController))
end

it "generates helper module and helper proxy class if helper_method target does not exist" do
add_ruby_file("controller.rb", <<~RUBY)
class BaseController < ActionController::Base
Expand Down Expand Up @@ -151,31 +133,6 @@ class HelperProxy < ::ActionView::Base
RBI

assert_equal(expected, rbi_for(:BaseController))

expected = <<~RBI
# typed: strong

class UserController
sig { returns(HelperProxy) }
def helpers; end

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

sig { returns(T.untyped) }
def current_user_name; end

sig { params(user_id: ::Integer).void }
def notify_user(user_id); end
end

class HelperProxy < ::ActionView::Base
include HelperMethods
end
end
RBI

assert_equal(expected, rbi_for(:UserController))
end

it "generates helper module and helper proxy class when defining helper using helper_method" do
Expand Down Expand Up @@ -349,6 +306,78 @@ class HelperProxy < ::ActionView::Base

assert_equal(expected, rbi_for(:UserController))
end

it "works correctly with abstract ApplicationController" do
add_ruby_file("greet_helper.rb", <<~RUBY)
module GreetHelper
def greet(user)
# ...
end
end
RUBY

add_ruby_file("application_controller.rb", <<~RUBY)
# typed: false

class ApplicationController < ActionController::Base
abstract!

helper_method :foo
def foo; end
end
RUBY
add_ruby_file("user_controller.rb", <<~RUBY)
class UserController < ApplicationController
helper GreetHelper
end
RUBY
add_ruby_file("posts_controller.rb", <<~RUBY)
class PostsController < ApplicationController
end
RUBY
assert_equal(["ApplicationController", "UserController"], gathered_constants)

expected = <<~RBI
# typed: strong

class ApplicationController
sig { returns(HelperProxy) }
def helpers; end

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

sig { returns(T.untyped) }
def foo; end
end

class HelperProxy < ::ActionView::Base
include HelperMethods
end
end
RBI
assert_equal expected, rbi_for(:ApplicationController)

expected = <<~RBI
# typed: strong

class UserController
sig { returns(HelperProxy) }
def helpers; end

module HelperMethods
include ::ActionController::Base::HelperMethods
include ::ApplicationController::HelperMethods
include ::GreetHelper
end

class HelperProxy < ::ActionView::Base
include HelperMethods
end
end
RBI
assert_equal expected, rbi_for(:UserController)
end
end
end
end
Expand Down