forked from drapergem/draper
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
85 additions
and
19 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
require "draper/version" | ||
require 'draper/base' | ||
require 'draper/system' | ||
require 'draper/all_helpers' | ||
require 'draper/base' | ||
require 'draper/lazy_helpers' | ||
|
||
Draper::System.setup |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module Draper | ||
module AllHelpers | ||
# 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 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) | ||
proxy = ActionController::Base.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 | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Draper | ||
module LazyHelpers | ||
def method_missing(method_name, *args, &block) | ||
begin | ||
helpers.send method_name, *args, &block | ||
rescue NoMethodError | ||
super | ||
end | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Draper | ||
class System | ||
def self.setup | ||
ActionController::Base.send(:extend, Draper::AllHelpers) | ||
end | ||
end | ||
end |
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