Sha256: ae97cb286e163dbf9d55c844cf9b43639244d623edaaae0f2ac3bc9ee87e5b26

Contents?: true

Size: 1.59 KB

Versions: 25

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module Katalyst
  module Tables
    class RowRenderer < ActionView::PartialRenderer # :nodoc:
      include ObjectRendering

      def initialize(lookup_context, collection:, partial:, **options)
        super(lookup_context, options)

        @collection = collection
        @partial    = partial
      end

      def render_row(row, object, view_context)
        @row    = row
        @object = object

        if @partial.blank?
          example  = example_for(@collection)
          @partial = partial_path(example, view_context) if example.present?
        end

        # if we still cannot find an example return an empty table (no header row)
        return "" if @partial.blank?

        @local_name ||= local_variable(@partial)
        render(@partial, view_context, nil)
      end

      private

      def example_for(collection)
        if collection.respond_to?(:items)
          example_for(collection.items)
        elsif collection.respond_to?(:any?) && collection.any?
          collection.first
        elsif collection.respond_to?(:model)
          collection.model.new
        end
        # if none of the above strategies match, return nil
      rescue ArgumentError
        nil # if we could not construct an example without passing arguments, return nil
      end

      def template_keys(path)
        super + [@local_name, :row]
      end

      def render_partial_template(view, locals, template, layout, block)
        locals[@local_name || template.variable] = @object
        locals[:row]                             = @row
        super
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
katalyst-tables-3.8.0 app/components/concerns/katalyst/tables/row_renderer.rb
katalyst-tables-3.7.0 app/components/concerns/katalyst/tables/row_renderer.rb
katalyst-tables-3.6.2 app/components/concerns/katalyst/tables/row_renderer.rb
katalyst-tables-3.6.1 app/components/concerns/katalyst/tables/row_renderer.rb
katalyst-tables-3.6.0 app/components/concerns/katalyst/tables/row_renderer.rb
katalyst-tables-3.5.5 app/components/concerns/katalyst/tables/row_renderer.rb
katalyst-tables-3.5.4 app/components/concerns/katalyst/tables/row_renderer.rb
katalyst-tables-3.5.3 app/components/concerns/katalyst/tables/row_renderer.rb
katalyst-tables-3.5.2 app/components/concerns/katalyst/tables/row_renderer.rb
katalyst-tables-3.5.1 app/components/concerns/katalyst/tables/row_renderer.rb
katalyst-tables-3.5.0 app/components/concerns/katalyst/tables/row_renderer.rb
katalyst-tables-3.4.6 app/components/concerns/katalyst/tables/row_renderer.rb
katalyst-tables-3.4.5 app/components/concerns/katalyst/tables/row_renderer.rb
katalyst-tables-3.4.4 app/components/concerns/katalyst/tables/row_renderer.rb
katalyst-tables-3.4.3 app/components/concerns/katalyst/tables/row_renderer.rb
katalyst-tables-3.4.2 app/components/concerns/katalyst/tables/row_renderer.rb
katalyst-tables-3.4.1 app/components/concerns/katalyst/tables/row_renderer.rb
katalyst-tables-3.4.0 app/components/concerns/katalyst/tables/row_renderer.rb
katalyst-tables-3.3.4 app/components/concerns/katalyst/tables/row_renderer.rb
katalyst-tables-3.3.3 app/components/concerns/katalyst/tables/row_renderer.rb