class WithModel::Table

In general, direct use of this class should be avoided. Instead use either the {WithModel high-level API} or {WithModel::Model::DSL low-level API}.

Public Class Methods

new(name, options = {}, &block) click to toggle source

@param [Symbol] name The name of the table to create. @param options Passed to ActiveRecord `create_table`. @param block Passed to ActiveRecord `create_table`. @see api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-create_table

# File lib/with_model/table.rb, line 13
def initialize(name, options = {}, &block)
  @name = name.freeze
  @options = options.freeze
  @block = block
end

Public Instance Methods

create() click to toggle source

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

# File lib/with_model/table.rb, line 21
def create
  connection.drop_table(@name) if exists?
  connection.create_table(@name, **@options, &@block)
end
destroy() click to toggle source
# File lib/with_model/table.rb, line 26
def destroy
  connection.drop_table(@name)
end

Private Instance Methods

connection() click to toggle source
# File lib/with_model/table.rb, line 40
def connection
  ActiveRecord::Base.connection
end
exists?() click to toggle source
# File lib/with_model/table.rb, line 32
def exists?
  if connection.respond_to?(:data_source_exists?)
    connection.data_source_exists?(@name)
  else
    connection.table_exists?(@name)
  end
end