Module: ActiveRecord::ConnectionAdapters::PostGIS::TableDefinition

Included in:
SpatialTableDefinition
Defined in:
lib/active_record/connection_adapters/postgis/table_definition.rb

Instance Method Summary collapse

Instance Method Details

#new_column_definition(name, type, **options) ⇒ Object

Override new_column_definition to handle spatial column options



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/active_record/connection_adapters/postgis/table_definition.rb', line 17

def new_column_definition(name, type, **options)
  col_type = if type.to_sym == :virtual
    options[:type]
  else
    type
  end

  if spatial_column_type?(col_type)
    if (limit = options.delete(:limit)) && limit.is_a?(::Hash)
      options.merge!(limit)
    end

    # Set geographic option for geography types
    if col_type.to_sym == :st_geography || col_type.to_sym == :geography
      options[:geographic] = true
    end

    geo_type = ColumnDefinitionUtils.geo_type(options[:type] || type)
    base_type = determine_base_type(col_type, options)


    # Create hash format limit for column metadata
    spatial_limit = {}
    spatial_limit[:type] = col_type.to_s
    spatial_limit[:srid] = options[:srid] if options[:srid] && options[:srid] != (options[:geographic] ? 4326 : 0)
    spatial_limit[:has_z] = options[:has_z] if options[:has_z]
    spatial_limit[:has_m] = options[:has_m] if options[:has_m]
    spatial_limit[:geographic] = options[:geographic] if options[:geographic]

    # Use hash format as limit for spatial columns, string format for SQL generation
    options[:limit] = spatial_limit.empty? ? nil : spatial_limit
    options[:spatial_type] = geo_type
    options[:spatial_sql] = ColumnDefinitionUtils.limit_from_options(geo_type, options)


    column = super(name, base_type, **options)
  else
    column = super(name, type, **options)
  end

  column
end