class ActsAsTable::Headers::Array

ActsAsTable headers array object.

@!attribute [r] column_models

Returns the ActsAsTable column models for this ActsAsTable headers array object.

@return [Enumerable<ActsAsTable::ColumnModel>]

Attributes

column_models[R]

Public Class Methods

new(column_models) click to toggle source

Returns a new ActsAsTable headers array object.

@param [Enumerable<ActsAsTable::ColumnModel>] column_models @return [ActsAsTable::Headers::Array]

Calls superclass method
# File lib/acts_as_table/headers.rb, line 17
def initialize(column_models)
  @column_models = column_models.to_a

  # @return [Array<Array<String>>]
  header_names_without_padding = @column_models.collect { |column_model|
    column_model.name.split(column_model.separator)
  }

  # @return [Integer]
  max_header_names_without_padding_size = header_names_without_padding.collect(&:size).max || 0

  # @return [Array<Array<String, nil>>]
  @header_names_with_padding = header_names_without_padding.collect { |split_name|
    split_name + ::Array.new(max_header_names_without_padding_size - split_name.size) { nil }
  }.collect { |header_names|
    header_names.freeze
  }.freeze

  # @return [Array<Array<String, nil>>]
  headers = @header_names_with_padding.transpose.collect(&:freeze)

  super(headers)

  self.freeze
end

Public Instance Methods

with_column_models_count() click to toggle source

Returns a 3-level array indexed by the header index, the header name index and the pair index.

The elements in the 3rd level are pairs, where the first element is the header name and the second element is the ActsAsTable column models count for the header name.

@note This method is intended to be used to render tables with merged cells in the header rows (where the ActsAsTable column models count is the column span).

@return [Array<Array<Array<Object>>>]

@example Render HTML “thead” element with merged “th” elements.

<thead>
  <% @row_model.to_headers.with_column_models_count.each do |header| %>
    <tr>
      <% header.each do |pair| %>
        <%= content_tag(:th, pair[0], colspan: pair[1], scope: 'col') %>
      <% end %>
    </tr>
  <% end %>
</thead>
# File lib/acts_as_table/headers.rb, line 62
def with_column_models_count
  # Drop the last row (the corresponding instances of the {ActsAsTable::ColumnModel} class).
  @with_column_models_count ||= Hash.for(@column_models, @header_names_with_padding).to_array[0..-2]
end