Sha256: 5ee43c0b57c6f3fe3f03fb974cda28f406011e557723d2fecf41cded6560cf1d

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

require 'admino/table/row'
require 'showcase/helpers/html_options'

module Admino
  module Table
    class HeadRow < Row
      attr_reader :resource_klass
      attr_reader :query

      def initialize(resource_klass, query, view_context)
        @resource_klass = resource_klass
        @query = query
        @columns = ""

        super(view_context)
      end

      def actions(*args, &block)
        default_options = column_html_options(:actions)
        label = I18n.t(
          :"#{resource_klass.model_name.i18n_key}.title",
          scope: 'table.actions',
          default: [ :title, 'Actions' ]
        )

        @columns << h.content_tag(:th, label.to_s, default_options)
      end

      def column(*args, &block)
        params = parse_column_args(args)

        attribute_name = params[:attribute_name]
        label = params[:label]
        options = params[:html_options]

        if label.nil? && attribute_name
          label = resource_klass.human_attribute_name(attribute_name.to_s)
        end

        default_options = column_html_options(attribute_name)
        html_options = Showcase::Helpers::HtmlOptions.new(default_options)
        html_options.merge_attrs!(options)
        html_options = html_options.to_h

        sorting_scope = html_options.delete(:sorting)
        if sorting_scope
          raise ArgumentError, 'query object is required' unless query
          sorting_html_options = html_options.delete(:sorting_html_options) { {} }
          label = query.sorting.scope_link(sorting_scope, label, sorting_html_options)
        end

        @columns << h.content_tag(:th, label.to_s, html_options.to_h)
      end

      def to_html
        @columns.html_safe
      end

      private

      def column_html_options(attribute_name)
        if attribute_name
          { role: attribute_name.to_s.gsub(/_/, '-') }
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
admino-0.0.3 lib/admino/table/head_row.rb
admino-0.0.2 lib/admino/table/head_row.rb