Skip to content

Commit 7c74a1d

Browse files
committed
Fix AR type cast to mirror quoting
1 parent 9bb0135 commit 7c74a1d

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/active_record/connection_adapters/postgis/quoting.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ module ConnectionAdapters
55
module PostGIS
66
module Quoting
77
def type_cast(value)
8-
case value
9-
when RGeo::Feature::Instance
8+
if RGeo::Feature::Geometry.check_type(value)
9+
RGeo::WKRep::WKBGenerator.new(hex_format: true, type_format: :ewkb, emit_ewkb_srid: true).generate(value)
10+
elsif value.is_a?(RGeo::Cartesian::BoundingBox)
1011
value.to_s
1112
else
1213
super

test/cases/spatial_queries_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,23 @@ def test_geo_safe_where
122122
assert_equal 1, SpatialModel.where("ST_DWITHIN(latlon_geo, ?, 500)", geographic_factory.point(-72.099, 42.099)).count
123123
end
124124

125+
def test_geo_query_matches_sql
126+
value = geographic_factory.point(-72.099, 42.099)
127+
128+
assert_match(
129+
RGeo::WKRep::WKBGenerator.new(hex_format: true, type_format: :ewkb, emit_ewkb_srid: true).generate(value),
130+
SpatialModel.where("ST_DWITHIN(latlon_geo, ?, 500)", value).explain.inspect
131+
)
132+
end
133+
134+
def test_srid_aware_query
135+
center = RGeo::Geos.factory(srid: 4326).point(3.808591, 43.606092)
136+
polygon = center.buffer(10_000)
137+
model = SpatialModel.create!(geometry: polygon)
138+
139+
assert_equal [model], SpatialModel.where("ST_Within(?, geometry)", center)
140+
end
141+
125142
private
126143

127144
def create_model
@@ -130,6 +147,7 @@ def create_model
130147
t.column "latlon_geo", :st_point, srid: 4326, geographic: true
131148
t.column "points", :multi_point, srid: 3785
132149
t.column "path", :line_string, srid: 3785
150+
t.column "geometry", :geometry, srid: 4326
133151
end
134152
SpatialModel.reset_column_information
135153
end

0 commit comments

Comments
 (0)