Sha256: 288939e891614ac621e94d5bf099df35a8265d4511f29b72c5575beeb04eb3af

Contents?: true

Size: 819 Bytes

Versions: 1

Compression:

Stored size: 819 Bytes

Contents

module ActionPresenter
  module ViewHelper
    extend ActiveSupport::Concern

    included do
      helper_method :present
    end

    def present(object, klass = nil)
      klass ||= presenter_name object

      object = object.last if object.is_a? Array

      presenter = klass.new(object, view_context)

      return yield presenter if block_given?
      presenter
    end

    def present_collection(objects, klass = nil)
      present([objects], klass)
    end

  private

    def presenter_name(scope)
      "#{klass_name(scope)}Presenter".constantize
    end

    def klass_name(scope)
      return scope.class.to_s unless scope.is_a? Array
      return scope.first.class.to_s if scope.length == 1

      scope.map do |s|
        s.is_a?(Symbol) ? s.capitalize : s.class
      end.join('::')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
action_presenter-1.1.5 lib/action_presenter/view_helper.rb