Module: ActiveRecord::ConnectionAdapters::PostGIS::AdapterExtensions

Defined in:
lib/active_record/connection_adapters/postgis/adapter_extensions.rb

Instance Method Summary collapse

Instance Method Details

#arel_visitorObject

Use PostGIS Arel visitor for spatial queries



78
79
80
81
# File 'lib/active_record/connection_adapters/postgis/adapter_extensions.rb', line 78

def arel_visitor
  require_relative "../../../arel/visitors/postgis"
  @arel_visitor ||= Arel::Visitors::PostGIS.new(self)
end

#create_table_definition(*args, **kwargs) ⇒ Object

Override create_table_definition to use our custom table definition



71
72
73
74
75
# File 'lib/active_record/connection_adapters/postgis/adapter_extensions.rb', line 71

def create_table_definition(*args, **kwargs)
  table_def = super(*args, **kwargs)
  table_def.extend(PostGIS::TableDefinition)
  table_def
end

#lookup_cast_type(sql_type) ⇒ Object

Override to handle PostGIS types



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/active_record/connection_adapters/postgis/adapter_extensions.rb', line 47

def lookup_cast_type(sql_type)
  # Handle PostGIS types
  if sql_type.to_s =~ /^(geometry|geography)/i
    type_name = case sql_type.to_s
    when /geography\(Point/i, /geometry\(Point/i then :st_point
    when /geography\(LineString/i, /geometry\(LineString/i then :st_line_string
    when /geography\(Polygon/i, /geometry\(Polygon/i then :st_polygon
    when /geography\(MultiPoint/i, /geometry\(MultiPoint/i then :st_multi_point
    when /geography\(MultiLineString/i, /geometry\(MultiLineString/i then :st_multi_line_string
    when /geography\(MultiPolygon/i, /geometry\(MultiPolygon/i then :st_multi_polygon
    when /geography\(GeometryCollection/i, /geometry\(GeometryCollection/i then :st_geometry_collection
    when /geography/i then :st_geography
    when /geometry/i then :st_geometry
    else
                  :st_geometry
    end

    ActiveRecord::Type.lookup(type_name, adapter: adapter_name.downcase.to_sym)
  else
    super
  end
end

#type_to_sql(type, limit: nil, precision: nil, scale: nil, geographic: false, srid: nil, has_z: false, has_m: false, **options) ⇒ Object

Override type_to_sql to handle PostGIS spatial types



8
9
10
11
12
13
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
40
41
42
43
44
# File 'lib/active_record/connection_adapters/postgis/adapter_extensions.rb', line 8

def type_to_sql(type, limit: nil, precision: nil, scale: nil, geographic: false, srid: nil, has_z: false, has_m: false, **options)
  if type.to_s =~ /^st_/ || type.to_s == "geography"
    geometric_type = type.to_s.sub(/^st_/, "")

    # If limit contains our custom format, parse it
    if limit.is_a?(String) && limit.include?(",")
      geo_part, srid_part = limit.split(",", 2)
      # Extract geometry type from the limit if provided
      if geo_part && !geo_part.empty? && geo_part != geometric_type.upcase
        geometric_type = geo_part.downcase
      end
      # Extract SRID from limit
      if srid_part && !srid_part.empty?
        srid = srid_part.to_i
      end
      # Extract Z/M modifiers
      if geo_part && geo_part.include?("Z")
        has_z = true
        geometric_type = geo_part.gsub(/[ZM]/, "").downcase
      end
      if geo_part && geo_part.include?("M")
        has_m = true
        geometric_type = geo_part.gsub(/[ZM]/, "").downcase
      end
    end

    PostGIS::SpatialColumnType.new(
      geometric_type,
      srid,
      has_z: has_z,
      has_m: has_m,
      geography: geographic
    ).to_sql
  else
    super
  end
end