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

Wrap helper_method calls in respond_to?(:helper_method) #3732

Merged
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
12 changes: 9 additions & 3 deletions lib/devise/controllers/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ module Helpers
include Devise::Controllers::StoreLocation

included do
helper_method :warden, :signed_in?, :devise_controller?
if respond_to?(:helper_method)
helper_method :warden, :signed_in?, :devise_controller?
end
end

module ClassMethods
Expand Down Expand Up @@ -69,7 +71,9 @@ def current_#{group_name.to_s.pluralize}
end.compact
end

helper_method "current_#{group_name}", "current_#{group_name.to_s.pluralize}", "#{group_name}_signed_in?"
if respond_to?(:helper_method)
helper_method "current_#{group_name}", "current_#{group_name.to_s.pluralize}", "#{group_name}_signed_in?"
end
METHODS
end

Expand Down Expand Up @@ -126,7 +130,9 @@ def #{mapping}_session
METHODS

ActiveSupport.on_load(:action_controller) do
helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session"
if respond_to?(:helper_method)
helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session"
end
end
end

Expand Down
21 changes: 21 additions & 0 deletions test/controllers/helper_methods_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'test_helper'

class ApiController < ActionController::Metal
include Devise::Controllers::Helpers
end

class HelperMethodsTest < ActionController::TestCase
tests ApiController

test 'includes Devise::Controllers::Helpers' do
assert_includes @controller.class.ancestors, Devise::Controllers::Helpers
end

test 'does not respond_to helper_method' do
refute_respond_to @controller.class, :helper_method
end

test 'defines methods like current_user' do
assert_respond_to @controller, :current_user
end

Choose a reason for hiding this comment

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

Please use the builtin methods: assert_includes, assert_respond_to and refute_respond_to. They provide nicer error messages in case of test failures.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, thank you.

end