Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jdickey/7c87420b22075fb5b297 to your computer and use it in GitHub Desktop.
Save jdickey/7c87420b22075fb5b297 to your computer and use it in GitHub Desktop.
Trivial example of isolating Rails helper logic.
# In app/helpers/users_helper.rb
require_relative 'users_helper/greet'
module UsersHelper
module Internals
end
private_constant :Internals
include Internals
def greet(user)
UserLinkGreeting.new(self).greet(user)
end
# ...
end
# In app/helpers/users_helper/ greet.rb
module UsersHelper
module Internals
class UserLinkGreeting
def initialize(helper_module)
@h = helper_module
end
def greet(user)
link_to "Hello, #{user.name}!", h.user_path(user)
end
private
attr_reader :h
end # end class UserLinkGreeting
end
end
@aruprakshit
Copy link

Thanks for the idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment