Sha256: ab9799e3f01255ce8c55a9e2b3988cb1d67815c9a8da4fc6447467480a772bcc

Contents?: true

Size: 966 Bytes

Versions: 21

Compression:

Stored size: 966 Bytes

Contents

# frozen_string_literal: true
module Kadmin
  module Presentable
    extend ActiveSupport::Concern

    module ClassMethods
      def presenter_class
        return @presenter_class ||= begin
          self::Presenter
        rescue NameError
          nil
        end
      end

      def presenter_class=(klass)
        @presenter_class = klass
      end
    end

    # Delegates present to a newly instantiated presenter object
    # Checks the including class' presenter_class attribute, with fallback to an inner Presenter class
    # @param [ActiveView::Base] view the view to present in if available; can be supplied to the presenter later
    # @raise [Kadmin::Error] raises an error if no presenter class defined
    def present(view = nil)
      if self.class.presenter_class.nil?
        raise Kadmin::Error, 'cannot present without a presenter_class'
      else
        return self.class.presenter_class.new(self, view: view)
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
kadmin-0.4.0 lib/kadmin/presentable.rb