Class: ActsAsTable::Headers::Array
- Inherits:
-
Array
- Object
- Array
- ActsAsTable::Headers::Array
- Defined in:
- lib/acts_as_table/headers.rb
Overview
ActsAsTable headers array object.
Instance Attribute Summary collapse
-
#column_models ⇒ Enumerable<ActsAsTable::ColumnModel>
readonly
Returns the ActsAsTable column models for this ActsAsTable headers array object.
Instance Method Summary collapse
-
#initialize(column_models) ⇒ ActsAsTable::Headers::Array
constructor
Returns a new ActsAsTable headers array object.
-
#with_column_models_count ⇒ Array<Array<Array<Object>>>
Returns a 3-level array indexed by the header index, the header name index and the pair index.
Constructor Details
#initialize(column_models) ⇒ ActsAsTable::Headers::Array
Returns a new ActsAsTable headers array object.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# 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 |
Instance Attribute Details
#column_models ⇒ Enumerable<ActsAsTable::ColumnModel> (readonly)
Returns the ActsAsTable column models for this ActsAsTable headers array object.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/acts_as_table/headers.rb', line 10 class Array < ::Array attr_reader :column_models # Returns a new ActsAsTable headers array object. # # @param [Enumerable<ActsAsTable::ColumnModel>] column_models # @return [ActsAsTable::Headers::Array] 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 # 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> # 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 end |
Instance Method Details
#with_column_models_count ⇒ Array<Array<Array<Object>>>
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).
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.
62 63 64 65 |
# 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 |