Sha256: c5ffe45115814c129ce08fca2b58698d52c2a051e8f2d74853a6265c4f3aa577

Contents?: true

Size: 1.53 KB

Versions: 10

Compression:

Stored size: 1.53 KB

Contents

module Hyrax
  class PresenterRenderer
    include ActionView::Helpers::TranslationHelper

    def initialize(presenter, view_context)
      @presenter = presenter
      @view_context = view_context
    end

    def value(field_name, locals = {})
      render_show_field_partial(field_name, locals)
    end

    def label(field)
      t(:"#{model_name.param_key}.#{field}",
        scope: label_scope,
        default: [:"defaults.#{field}", field.to_s.humanize]).presence
    end

    def fields(terms, &_block)
      @view_context.safe_join(terms.map { |term| yield self, term })
    end

    protected

      def render_show_field_partial(field_name, locals)
        partial = find_field_partial(field_name)
        @view_context.render partial, locals.merge(key: field_name, record: @presenter)
      end

      def find_field_partial(field_name)
        ["#{collection_path}/show_fields/_#{field_name}", "records/show_fields/_#{field_name}",
         "#{collection_path}/show_fields/_default", "records/show_fields/_default"].find do |partial|
          Rails.logger.debug "Looking for show field partial #{partial}"
          return partial.sub(/\/_/, '/') if partial_exists?(partial)
        end
      end

      def collection_path
        @collection_path ||= ActiveSupport::Inflector.tableize(model_name)
      end

      def partial_exists?(partial)
        @view_context.lookup_context.find_all(partial).any?
      end

      def label_scope
        :"simple_form.labels"
      end

      def model_name
        @presenter.model_name
      end
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
hyrax-1.1.1 app/presenters/hyrax/presenter_renderer.rb
hyrax-1.1.0 app/presenters/hyrax/presenter_renderer.rb
hyrax-1.0.5 app/presenters/hyrax/presenter_renderer.rb
hyrax-1.0.4 app/presenters/hyrax/presenter_renderer.rb
hyrax-1.0.3 app/presenters/hyrax/presenter_renderer.rb
hyrax-1.0.2 app/presenters/hyrax/presenter_renderer.rb
hyrax-1.0.1 app/presenters/hyrax/presenter_renderer.rb
hyrax-1.0.0.rc2 app/presenters/hyrax/presenter_renderer.rb
hyrax-1.0.0.rc1 app/presenters/hyrax/presenter_renderer.rb
test_hyrax-0.0.1.alpha app/presenters/hyrax/presenter_renderer.rb