Sha256: 658186f793725319fe5e41c795260f4f77f2b26e8a6159675773f03b175258d0

Contents?: true

Size: 667 Bytes

Versions: 2

Compression:

Stored size: 667 Bytes

Contents

# frozen_string_literal: true

module Chic
  module Presentable
    def presenter(context = nil)
      @presenter ||= presenter_class.new(self, context)
    end

    def presenter_class
      "#{respond_to?(:model_name) ? send(:model_name) : self.class.name}Presenter".constantize
    end

    module_function

    def presenter_for(object)
      if object.respond_to?(:presenter_class)
        object.presenter_class
      else
        "#{object&.model_name || object.class.name}Presenter".constantize
      end
    rescue NameError, LoadError
      Chic.configuration.logger&.debug "Couldn't find a presenter for '#{object.class.name}'"
      nil
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
chic-0.2.0 lib/chic/presentable.rb
chic-0.1.0 lib/chic/presentable.rb