Sha256: bb8ce2ff82f1aa18b375d0e6dd06e2c4a3c7596574b65049612ca3b627520bb5

Contents?: true

Size: 1.79 KB

Versions: 15

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true
module Hyrax
  class PresenterRenderer
    include ActionView::Helpers::TranslationHelper

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

    ##
    # Renders a collection field partial
    #
    # @return [ActiveSupport::SafeBuffer] an html safe string containing the value markup
    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

    ##
    # @deprecated
    def fields(terms, &_block)
      Deprecation.warn("Fields is deprecated for removal in Hyrax 4.0.0. use #value and #label directly instead.")
      @view_context.safe_join(terms.map { |term| yield self, term })
    end

    private

    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

15 entries across 15 versions & 1 rubygems

Version Path
hyrax-3.6.0 app/presenters/hyrax/presenter_renderer.rb
hyrax-3.5.0 app/presenters/hyrax/presenter_renderer.rb
hyrax-3.4.2 app/presenters/hyrax/presenter_renderer.rb
hyrax-4.0.0.beta1 app/presenters/hyrax/presenter_renderer.rb
hyrax-3.4.1 app/presenters/hyrax/presenter_renderer.rb
hyrax-3.4.0 app/presenters/hyrax/presenter_renderer.rb
hyrax-3.3.0 app/presenters/hyrax/presenter_renderer.rb
hyrax-3.2.0 app/presenters/hyrax/presenter_renderer.rb
hyrax-3.1.0 app/presenters/hyrax/presenter_renderer.rb
hyrax-3.0.2 app/presenters/hyrax/presenter_renderer.rb
hyrax-3.0.1 app/presenters/hyrax/presenter_renderer.rb
hyrax-3.0.0 app/presenters/hyrax/presenter_renderer.rb
hyrax-3.0.0.pre.rc4 app/presenters/hyrax/presenter_renderer.rb
hyrax-3.0.0.pre.rc3 app/presenters/hyrax/presenter_renderer.rb
hyrax-3.0.0.pre.rc2 app/presenters/hyrax/presenter_renderer.rb