Created
July 12, 2011 02:21
-
-
Save xaviershay/1077274 to your computer and use it in GitHub Desktop.
Helpers outside your view
This file contains hidden or 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
class ApplicationController | |
# Provide access to helper methods from outside controllers and views, | |
# such as in Presenter objects. Rails provides ActionController::Base.helpers, | |
# but this does not include any of our application helpers. | |
def self.all_helpers | |
@all_helpers_proxy ||= begin | |
# Start with just the rails helpers. This is the same method used | |
# by ActionController::Base.helpers | |
proxy = ActionView::Base.new.extend(_helpers) | |
# url_for depends on _routes method being defined | |
proxy.instance_eval do | |
def _routes | |
Rails.application.routes | |
end | |
end | |
# Import all named path methods | |
proxy.extend(Rails.application.routes.named_routes.module) | |
# Load all our application helpers to extend | |
modules_for_helpers([:all]).each do |mod| | |
proxy.extend(mod) | |
end | |
proxy.instance_eval do | |
# A hack since this proxy doesn't pick up default_url_options from anywhere | |
def url_for(*args) | |
if args.last.is_a?(Hash) && !args.last[:only_path] | |
args = args.dup | |
args << args.pop.merge(host: ActionMailer::Base.default_url_options[:host]) | |
end | |
super(*args) | |
end | |
end | |
proxy | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice, was able to wrap this into a module and get it working! https://github.com/jcasimir/draper/blob/untested/lib/draper/all_helpers.rb
Super big thanks!