class MotionRecord::Schema::TableDefinition
Public Class Methods
new(name, options={})
click to toggle source
# File lib/motion_record/schema/table_definition.rb, line 4 def initialize(name, options={}) @name = name @columns = [] @index_definitions = [] unless options.has_key?(:id) && !options[:id] add_default_primary_column end end
Public Instance Methods
execute()
click to toggle source
# File lib/motion_record/schema/table_definition.rb, line 14 def execute # Create table column_sql = @columns.map(&:to_sql_definition).join(", ") MotionRecord::Base.connection.execute "CREATE TABLE #{@name} (#{column_sql})" # Create table's indexes @index_definitions.each(&:execute) end
float(column_name, options={})
click to toggle source
# File lib/motion_record/schema/table_definition.rb, line 31 def float(column_name, options={}) @columns << ColumnDefinition.new(:float, column_name, options) end
index(columns, options={})
click to toggle source
# File lib/motion_record/schema/table_definition.rb, line 35 def index(columns, options={}) @index_definitions << IndexDefinition.new(@name, columns, options) end
integer(column_name, options={})
click to toggle source
# File lib/motion_record/schema/table_definition.rb, line 27 def integer(column_name, options={}) @columns << ColumnDefinition.new(:integer, column_name, options) end
text(column_name, options={})
click to toggle source
# File lib/motion_record/schema/table_definition.rb, line 23 def text(column_name, options={}) @columns << ColumnDefinition.new(:text, column_name, options) end
timestamps()
click to toggle source
Add :created_at and :updated_at columns to the table
# File lib/motion_record/schema/table_definition.rb, line 40 def timestamps self.integer(:created_at) self.integer(:updated_at) end
Protected Instance Methods
add_default_primary_column()
click to toggle source
# File lib/motion_record/schema/table_definition.rb, line 47 def add_default_primary_column @columns << ColumnDefinition.new(:integer, "id", primary: true) end