Last active
October 9, 2015 03:55
-
-
Save jdickey/7c87420b22075fb5b297 to your computer and use it in GitHub Desktop.
Trivial example of isolating Rails helper logic.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the idea.