Class: WithModel::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/with_model/table.rb

Overview

In general, direct use of this class should be avoided. Instead use either the high-level API or low-level API.

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}, connection: ActiveRecord::Base.connection, &block) ⇒ Table

Returns a new instance of Table.

Parameters:

  • name (Symbol)

    The name of the table to create.

  • options (defaults to: {})

    Passed to ActiveRecord create_table.

  • connection (defaults to: ActiveRecord::Base.connection)

    The connection to use for creating the table.

  • block

    Passed to ActiveRecord create_table.

See Also:



14
15
16
17
18
19
# File 'lib/with_model/table.rb', line 14

def initialize(name, options = {}, connection: ActiveRecord::Base.connection, &block)
  @name = name.freeze
  @options = options.freeze
  @block = block
  @connection = connection
end

Instance Method Details

#configure(klass) ⇒ Object

Points the model at this table.



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

def configure(klass)
  klass.table_name = @name
end

#createObject

Creates the table with the initialized options. Drops the table if it already exists.



23
24
25
26
# File 'lib/with_model/table.rb', line 23

def create
  connection.drop_table(@name) if exists?
  connection.create_table(@name, **@options, &@block)
end

#destroyObject



40
41
42
# File 'lib/with_model/table.rb', line 40

def destroy
  connection.drop_table(@name)
end

#teardown(_klass) ⇒ Object

Removes everything this table holds by dropping it. The model is not needed, but is accepted so that NullTable - which does need it - can stand in here.



36
37
38
# File 'lib/with_model/table.rb', line 36

def teardown(_klass)
  destroy
end