Class: ActsAsTable::Table

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/acts_as_table/table.rb

Overview

ActsAsTable table.

Instance Attribute Summary collapse

Belongs to collapse

Has many collapse

Instance Method Summary collapse

Instance Attribute Details

#records_countInteger (readonly)

Returns the number of ActsAsTable records for this ActsAsTable table.

Returns:

  • (Integer)


8
9
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
# File 'app/models/acts_as_table/table.rb', line 8

class Table < ::ActiveRecord::Base
  # @!parse
  #   include ActsAsTable::ValueProvider
  #   include ActsAsTable::ValueProviderAssociationMethods

  self.table_name = ActsAsTable.tables_table

  # Returns the ActsAsTable row model for this ActsAsTable table.
  belongs_to :row_model, **{
    class_name: 'ActsAsTable::RowModel',
    inverse_of: :tables,
    required: true,
  }

  # Returns the ActsAsTable records for this ActsAsTable table.
  has_many :records, -> { order(position: :asc) }, **{
    autosave: true,
    class_name: 'ActsAsTable::Record',
    dependent: :destroy,
    foreign_key: 'table_id',
    inverse_of: :table,
    validate: true,
  }

  # Returns the ActsAsTable records for the given row.
  #
  # @param [Array<String, nil>, nil] row
  # @return [Array<ActsAsTable::Record>]
  # @raise [ArgumentError] If the name of a class for a given record does not match the class name for the corresponding ActsAsTable record model.
  def from_row(row = [])
    self.row_model.from_row(row, self.records)
  end
end

Instance Method Details

#from_row(row = []) ⇒ Array<ActsAsTable::Record>

Returns the ActsAsTable records for the given row.

Parameters:

  • row (Array<String, nil>, nil) (defaults to: [])

Returns:

Raises:

  • (ArgumentError)

    If the name of a class for a given record does not match the class name for the corresponding ActsAsTable record model.



37
38
39
# File 'app/models/acts_as_table/table.rb', line 37

def from_row(row = [])
  self.row_model.from_row(row, self.records)
end

#recordsActiveRecord::Relation<ActsAsTable::Record>

Returns the ActsAsTable records for this ActsAsTable table.

Returns:

See Also:



23
24
25
26
27
28
29
30
# File 'app/models/acts_as_table/table.rb', line 23

has_many :records, -> { order(position: :asc) }, **{
  autosave: true,
  class_name: 'ActsAsTable::Record',
  dependent: :destroy,
  foreign_key: 'table_id',
  inverse_of: :table,
  validate: true,
}

#row_modelActsAsTable::RowModel

Returns the ActsAsTable row model for this ActsAsTable table.



16
17
18
19
20
# File 'app/models/acts_as_table/table.rb', line 16

belongs_to :row_model, **{
  class_name: 'ActsAsTable::RowModel',
  inverse_of: :tables,
  required: true,
}