Sha256: b184d53c061ddeef98ce290081d72342516ed1d7ae3ed045b99e2b40842e5036

Contents?: true

Size: 1.55 KB

Versions: 4

Compression:

Stored size: 1.55 KB

Contents

module Draper
  module AllHelpers
    # Most of the black magic here thanks to Xavier Shay (@xshay)
    # 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
          def controller
            #Object.controller
          end
        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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
draper-0.7.4 lib/draper/all_helpers.rb
draper-0.7.3 lib/draper/all_helpers.rb
draper-0.7.2 lib/draper/all_helpers.rb
draper-0.7.1 lib/draper/all_helpers.rb