Class: Rapids::Batch::CreateTable

Inherits:
Object
  • Object
show all
Includes:
ModelExtensions
Defined in:
lib/rapids/batch/create_table.rb

Instance Method Summary collapse

Methods included from ModelExtensions

#batch_table_name, #columns, #default_on_nil, #quote_bound_value, #sql_column_name

Constructor Details

#initialize(model, batch_definition) ⇒ CreateTable

Returns a new instance of CreateTable.



9
10
11
12
# File 'lib/rapids/batch/create_table.rb', line 9

def initialize(model,batch_definition)
  @model = model
  @batch = batch_definition
end

Instance Method Details

#to_sqlObject



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
# File 'lib/rapids/batch/create_table.rb', line 14

def to_sql
  columns_helper = ColumnsHelper.new(@model,@batch)
  columns_sql = columns_helper.map do |column,path|
    association = nil
    model = @model
    path.each do |association_name_or_hash|
      association_name = if association_name_or_hash.is_a?(Hash)
        association_name_or_hash[:name]
      else
        association_name_or_hash
      end
      if model.reflections[association_name]
        association = model.reflections[association_name]
        model = model.reflections[association_name].klass
      end
    end
    column_type = if association && association.collection? && column.number?
      "varchar(255)"
    else
      column.sql_type
    end
    "#{sql_column_name(column,path)} #{column_type}"
  end.join(",")

  "CREATE TABLE `#{batch_table_name}` (#{columns_sql}) ENGINE=BLACKHOLE DEFAULT CHARSET=utf8 COLLATE=#{@model.connection.collation}"
end