I have a module with a function. It resides in /lib/contact.rb:
module Contact
class << self
def run(current_user)
...
end
end
end
>
I've been struggling with the niceties the helper is expecting from the default controller and stack (default_url_options, etc.), and didn't want to hardcode the host.
Our URL helpers are provided by our nifty module, of course:
include Rails.application.routes.url_helpers
But include this as is, and (1) the helper is going to look for default_url_options, and (2) won't know about the request host nor the request.
The host part comes from the controller instance's url_options. Hence, I pass the controller context into my former module, now a class:
class ApplicationController
def do_nifty_things
HasAccessToRoutes.new(self).render
end
end
class HasAccessToRoutes
include Rails.application.routes.url_helpers
delegate :default_url_options, :url_options, to: :@context
def initialize(context)
@context = context
end
def render
nifty_things_url
end
end
Might not fit every case, but it's been useful to me when implementing a sort of custom renderer.
In any way: