Class: YiffSpace::Utils::TableBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/yiffspace/utils/table_builder.rb

Overview

A helper class for building HTML tables. Used in views.

Examples:

<%= table_for @tags do |table| %>
  <% table.column :name do |tag| %>
    <%= link_to_wiki "?", tag.name  %>
    <%= link_to tag.name, posts_path(tags: tag.name) %>
  <% end %>
  <% table.column :post_count %>
<% end %>

See Also:

  • app/views/table_builder/_tableapp/views/table_builder/_table.htmlapp/views/table_builder/_table.html.erb

Defined Under Namespace

Classes: Column

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items, tr: {}, **table_attributes) {|table| ... } ⇒ TableBuilder

Build a table for an array of objects, one object per row.

The <table> tag is automatically given an HTML id of the form ‘name-table`. For example, `posts-table`, `tags-table`.

The <tr> tag is automatically given an HTML id of the form ‘name-id`. For example, `post-1234`, `tag-4567`, etc. Each <tr> tag also gets a set of data attributes for each model; see #html_data_attributes in app/policies.

Parameters:

  • items (Array<ApplicationRecord>)

    The list of ActiveRecord objects to build the table for. One item per table row.

  • tr (Hash) (defaults to: {})

    optional HTML attributes for the <tr> tag for each row

  • table_attributes (Hash)

    optional HTML attributes for the <table> tag

Yield Parameters:

  • table (self)

    the table being built



94
95
96
97
98
99
100
101
102
103
# File 'lib/yiffspace/utils/table_builder.rb', line 94

def initialize(items, tr: {}, **table_attributes)
  @items = items
  @columns = []
  @table_attributes = { class: "striped", **table_attributes }
  @row_attributes = tr

  @table_attributes[:id] ||= "#{items.model_name.plural.dasherize}-table" if items.respond_to?(:model_name)

  yield(self) if block_given?
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



78
79
80
# File 'lib/yiffspace/utils/table_builder.rb', line 78

def columns
  @columns
end

#itemsObject (readonly)

Returns the value of attribute items.



78
79
80
# File 'lib/yiffspace/utils/table_builder.rb', line 78

def items
  @items
end

#row_attributesObject (readonly)

Returns the value of attribute row_attributes.



78
79
80
# File 'lib/yiffspace/utils/table_builder.rb', line 78

def row_attributes
  @row_attributes
end

#table_attributesObject (readonly)

Returns the value of attribute table_attributes.



78
79
80
# File 'lib/yiffspace/utils/table_builder.rb', line 78

def table_attributes
  @table_attributes
end

Instance Method Details

#all_row_attributes(item, _row) ⇒ Hash

Return the HTML attributes for each <tr> tag.

Parameters:

  • item (ApplicationRecord)

    the item for this row

  • _row (Integer)

    the row number (unused)

Returns:

  • (Hash)

    the <tr> attributes



125
126
127
128
129
130
131
132
133
# File 'lib/yiffspace/utils/table_builder.rb', line 125

def all_row_attributes(item, _row)
  return {} unless item.is_a?(ApplicationRecord)

  {
    id: "#{item.model_name.singular.dasherize}-#{item.id}",
    **row_attributes,
    **ApplicationController.helpers.data_attributes_for(item),
  }
end

#captionObject

Set the table caption



106
107
108
109
# File 'lib/yiffspace/utils/table_builder.rb', line 106

def caption
  @caption = yield if block_given?
  @caption
end

#column(**options) ⇒ Object

Add a column to the table.

Examples:

table.column(:name)


114
115
116
117
118
119
# File 'lib/yiffspace/utils/table_builder.rb', line 114

def column(*, **options, &)
  opt = options.extract!(:if, :unless)
  return if (opt.key?(:if) && !opt[:if]) || (opt.key?(:unless) && opt[:unless])

  @columns << Column.new(*, **options, &)
end