Sha256: 8322098566c51d76c6cff67945b4652e18973f56c560e1e5ba3b93dbdb14fa7b

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

require_relative "frontend/builder/base"
require_relative "frontend/builder/body_cell"
require_relative "frontend/builder/body_row"
require_relative "frontend/builder/header_cell"
require_relative "frontend/builder/header_row"
require_relative "frontend/helper"
require_relative "frontend/table_builder"

module Katalyst
  module Tables
    # View Helper for generating HTML tables. Include in your ApplicationHelper, or similar.
    module Frontend
      include Helper

      def table_with(collection:, **options, &block)
        table_options = options.slice(:header, :object_name, :sort)

        table_options[:object_name] ||= collection.try(:model_name)&.param_key

        html_options = html_options_for_table_with(**options)

        builder = options.fetch(:builder) { default_table_builder_class }
        builder.new(self, collection, table_options, html_options).build(&block)
      end

      def table_header_row(table, builder, &block)
        builder.new(table).build(&block)
      end

      def table_header_cell(table, method, builder, **options, &block)
        builder.new(table, method, **options).build(&block)
      end

      def table_body_row(table, object, builder, &block)
        builder.new(table, object).build(&block)
      end

      def table_body_cell(table, object, method, builder, **options, &block)
        builder.new(table, object, method, **options).build(&block)
      end

      private

      def default_table_builder_class
        builder = controller.try(:default_table_builder) || TableBuilder
        builder.respond_to?(:constantize) ? builder.constantize : builder
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
katalyst-tables-1.0.0 lib/katalyst/tables/frontend.rb