Sha256: 605832eb4e931ddf84510e3e5a47d1e40b5be85f297386c379a1b9512b9b720f
Contents?: true
Size: 1.21 KB
Versions: 4
Compression:
Stored size: 1.21 KB
Contents
# frozen_string_literal: true require 'active_record' module WithModel # 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}. class Table # @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 https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-create_table def initialize(name, options = {}, &block) @name = name.freeze @options = options.freeze @block = block end # Creates the table with the initialized options. Drops the table if # it already exists. def create connection.drop_table(@name) if exists? connection.create_table(@name, **@options, &@block) end def destroy connection.drop_table(@name) end private def exists? if connection.respond_to?(:data_source_exists?) connection.data_source_exists?(@name) else connection.table_exists?(@name) end end def connection ActiveRecord::Base.connection end end end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
with_model-2.1.7 | lib/with_model/table.rb |
with_model-2.1.6 | lib/with_model/table.rb |
with_model-2.1.5 | lib/with_model/table.rb |
with_model-2.1.4 | lib/with_model/table.rb |