Class: YiffSpace::Utils::TableBuilder::Column

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

Overview

Represents a single column in the table.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute = nil, column: nil, th: {}, td: {}, width: nil, name: nil) {|item| ... } ⇒ Column

Define a table column.

Examples:

<% table.column :post_count %>
<% table.column :name do |tag| %>
  <%= tag.pretty_name %>
<% end %>

Parameters:

  • attribute (Symbol) (defaults to: nil)

    The attribute in the model the column is for. The column’s name and value will come from this attribute by default.

  • name (String) (defaults to: nil)

    the column’s name, if different from the attribute name.

  • column (String) (defaults to: nil)

    the column name

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

    the HTML attributes for the column’s <th> tag.

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

    the HTML attributes for the column’s <td> tag.

  • width (String) (defaults to: nil)

    the HTML width value for the <th> tag.

Yield Parameters:

  • item

    a block that returns the column value based on the item.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/yiffspace/utils/table_builder.rb', line 40

def initialize(attribute = nil, column: nil, th: {}, td: {}, width: nil, name: nil, &block)
  @attribute = attribute
  @column = column
  @header_attributes = { width: width, **th }
  @body_attributes = td
  @block = block

  @name = name || attribute
  @name = @name.to_s.titleize unless @name.is_a?(String)

  return unless @name.present? || @column.present?

  if @column.present?
    column_class = "#{@column}-column"
  else
    column_class = "#{@name.parameterize.dasherize}-column"
  end
  @header_attributes[:class] = "#{column_class} #{@header_attributes[:class]}".strip
  @body_attributes[:class] = "#{column_class} #{@body_attributes[:class]}".strip
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



20
21
22
# File 'lib/yiffspace/utils/table_builder.rb', line 20

def attribute
  @attribute
end

#blockObject (readonly)

Returns the value of attribute block.



20
21
22
# File 'lib/yiffspace/utils/table_builder.rb', line 20

def block
  @block
end

#body_attributesObject (readonly)

Returns the value of attribute body_attributes.



20
21
22
# File 'lib/yiffspace/utils/table_builder.rb', line 20

def body_attributes
  @body_attributes
end

#captionObject (readonly)

Returns the value of attribute caption.



20
21
22
# File 'lib/yiffspace/utils/table_builder.rb', line 20

def caption
  @caption
end

#header_attributesObject (readonly)

Returns the value of attribute header_attributes.



20
21
22
# File 'lib/yiffspace/utils/table_builder.rb', line 20

def header_attributes
  @header_attributes
end

#nameObject (readonly)

Returns the value of attribute name.



20
21
22
# File 'lib/yiffspace/utils/table_builder.rb', line 20

def name
  @name
end

Instance Method Details

#value(item, row, column) ⇒ #to_s

Returns the value of the table cell.

Parameters:

  • item (ApplicationRecord)

    the table cell item

  • row (Integer)

    the table row number

  • column (Integer)

    the table column number

Returns:

  • (#to_s)

    the value of the table cell



66
67
68
69
70
71
72
73
74
75
# File 'lib/yiffspace/utils/table_builder.rb', line 66

def value(item, row, column)
  if block.present?
    block.call(item, row, column, self)
    nil
  elsif attribute.is_a?(Symbol)
    item.send(attribute)
  else
    ""
  end
end