Class: Phlexi::Table::Base

Inherits:
HTML
  • Object
show all
Includes:
Phlex::DeferredRender, Options::Captions, Options::Descriptions
Defined in:
lib/phlexi/table/base.rb

Overview

Base class for creating customizable table components

Examples:

Basic usage

class UsersTable < Phlexi::Table::Base
  def table_template
    table_caption "Caption"
    table_description "Description"
    selection_column :id
    column :name
    column :a_sorted_column, sort_params: {url: "url", reset_url: "reset_url", position: "multisort position", direction: "ASC|DESC"}
    column_group :a_grouped_column do |g|
      g.column :sub_col1
      g.column :sub_col2
    end
    actions do |user|
      link_to "Edit", edit_user_path(user)
    end
  end
end

render UsersTable.new(User.all)

Defined Under Namespace

Classes: ActionsColumn, ColumnGroup, DataColumn, Display, SelectionColumn

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Options::Descriptions

#has_table_description?, #table_description

Methods included from Options::Captions

#has_table_caption?, #table_caption

Constructor Details

#initialize(collection, **options) ⇒ Base

Initialize a new table component

Parameters:

  • collection (Enumerable)

    The collection of items to display

  • options (Hash)

    Additional options for customizing the table

Options Hash (**options):

  • :id (String)

    The ID attribute for the table

  • :class (String)

    The CSS class(es) for the table

Raises:

  • (ArgumentError)

    If the collection is empty



53
54
55
56
57
58
59
60
# File 'lib/phlexi/table/base.rb', line 53

def initialize(collection, **options)
  @collection = Array(collection)
  raise ArgumentError, "Collection cannot be empty" if @collection.empty?

  @columns = {}
  @options = options
  initialize_key
end

Instance Attribute Details

#collectionEnumerable (readonly)

The collection of items to display in the table

Returns:

  • (Enumerable)

    the current value of collection



29
30
31
# File 'lib/phlexi/table/base.rb', line 29

def collection
  @collection
end

#columnsHash (readonly)

The columns defined for the table

Returns:

  • (Hash)

    the current value of columns



29
30
31
# File 'lib/phlexi/table/base.rb', line 29

def columns
  @columns
end

#keyObject (readonly)

Returns the value of attribute key.



44
45
46
# File 'lib/phlexi/table/base.rb', line 44

def key
  @key
end

#optionsObject (readonly)

Returns the value of attribute options.



44
45
46
# File 'lib/phlexi/table/base.rb', line 44

def options
  @options
end

Instance Method Details

#actionsObject



110
111
112
113
114
115
# File 'lib/phlexi/table/base.rb', line 110

def actions(**, &)
  raise "Action column already added" if @has_action_column

  @has_action_column = true
  add_column(actions_column_class.new(:phlexi_table_actions, self, label: "Actions", **, &))
end

#actions_column_classObject



137
138
139
# File 'lib/phlexi/table/base.rb', line 137

def actions_column_class
  self.class::ActionsColumn
end

#add_column(column) ⇒ void

This method returns an undefined value.

Add a column to the table

Parameters:

  • column (Columns::Base)

    The column object to add

Raises:

  • (ArgumentError)

    If a column with the same key already exists



88
89
90
91
92
# File 'lib/phlexi/table/base.rb', line 88

def add_column(column)
  raise ArgumentError, "Column '#{column.key}' already exists" if @columns.key?(column.key)

  @columns[column.key] = column
end

#around_templateObject



62
63
64
65
66
67
68
# File 'lib/phlexi/table/base.rb', line 62

def around_template
  original_template = Phlexi::Display::Theme.instance
  Phlexi::Display::Theme.instance = Phlexi::Table::DisplayTheme.instance
  super
ensure
  Phlexi::Display::Theme.instance = original_template
end

#before_templateObject



70
71
72
73
# File 'lib/phlexi/table/base.rb', line 70

def before_template
  super
  table_template
end

#column(key) ⇒ Object



94
95
96
# File 'lib/phlexi/table/base.rb', line 94

def column(key, **, &)
  add_column(column_class.new(key, self, **, &))
end

#column_classObject



129
130
131
# File 'lib/phlexi/table/base.rb', line 129

def column_class
  self.class::DataColumn
end

#column_group(key) ⇒ Object



105
106
107
108
# File 'lib/phlexi/table/base.rb', line 105

def column_group(key, **, &)
  @has_grouped_columns = true
  add_column(column_group_class.new(key, self, **, &))
end

#column_group_classObject



141
142
143
# File 'lib/phlexi/table/base.rb', line 141

def column_group_class
  self.class::ColumnGroup
end

#dom_idObject



125
126
127
# File 'lib/phlexi/table/base.rb', line 125

def dom_id
  key
end

#sampleObject



117
118
119
# File 'lib/phlexi/table/base.rb', line 117

def sample
  collection[0]
end

#selection_column(key) ⇒ Object



98
99
100
101
102
103
# File 'lib/phlexi/table/base.rb', line 98

def selection_column(key, **, &)
  raise "Selection column already added" if @has_selection_column

  @has_selection_column = true
  add_column(selection_column_class.new(key, self, **, &))
end

#selection_column_classObject



133
134
135
# File 'lib/phlexi/table/base.rb', line 133

def selection_column_class
  self.class::SelectionColumn
end

#table_templateObject



79
80
81
# File 'lib/phlexi/table/base.rb', line 79

def table_template
  # implement this in subclasses to define the column template
end

#view_templateObject



75
76
77
# File 'lib/phlexi/table/base.rb', line 75

def view_template
  render_table
end

#wrapped_sampleObject



121
122
123
# File 'lib/phlexi/table/base.rb', line 121

def wrapped_sample
  @wrapped_sample ||= WrappedObject.new(sample, index: -1, display_class: self.class::Display)
end