szTheory/meta_presenter

View on GitHub
lib/meta_presenter/helpers.rb

Summary

Maintainability
A
0 mins
Test Coverage
require 'meta_presenter/builder'
require 'active_support/rescuable'

module MetaPresenter
  # Including this module in your controller will give
  # your views access to a `presenter` method that
  # delegates to controller methods
  #
  # class ApplicationController < ActionController::Base
  #   include MetaPresenter::Base
  # end
  #
  # class ApplicationMailer < ActionMailer::Base
  #   include MetaPresenter::Base
  # end
  #
  module Helpers
    def self.included(base)
      base.instance_eval do
        # Sets up the `presenter.` method as helper within your views
        # If you want to customize this for yourself just alias_method it
        helper_method :presenter
      end
    end

    private

    # Initialize presenter with the current controller
    def presenter
      @presenter ||= begin
        controller = self
        klass = MetaPresenter::Builder.new(controller, action_name).presenter_class
        klass.new(controller)
      end
    end
  end
end