Skip to content

Commit a9efe07

Browse files
apply quoting patch
From #402 (comment)
1 parent 1290093 commit a9efe07

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
module ActiveRecord
4+
module ConnectionAdapters
5+
module PostGIS
6+
module Quoting
7+
def type_cast(value)
8+
case value
9+
when RGeo::Feature::Instance
10+
value.to_s
11+
else
12+
super
13+
end
14+
end
15+
end
16+
end
17+
end
18+
end

lib/active_record/connection_adapters/postgis_adapter.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
require_relative "postgis/arel_tosql"
1717
require_relative "postgis/oid/spatial"
1818
require_relative "postgis/oid/date_time"
19+
require_relative "postgis/quoting"
1920
require_relative "postgis/type" # has to be after oid/*
2021
# :startdoc:
2122

@@ -42,6 +43,7 @@ class PostGISAdapter < PostgreSQLAdapter
4243
# http://postgis.17.x6.nabble.com/Default-SRID-td5001115.html
4344
DEFAULT_SRID = 0
4445

46+
include PostGIS::Quoting
4547
include PostGIS::SchemaStatements
4648
include PostGIS::DatabaseStatements
4749

test/cases/spatial_queries_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,19 @@ def test_ewkt_parser_query
115115
assert_nil(obj3)
116116
end
117117

118+
def test_geo_safe_where
119+
create_model
120+
SpatialModel.create!(latlon_geo: geographic_factory.point(-72.1, 42.1))
121+
SpatialModel.create!(latlon_geo: geographic_factory.point(10.0, 10.0))
122+
assert_equal 1, SpatialModel.where("ST_DWITHIN(latlon_geo, ?, 500)", geographic_factory.point(-72.099, 42.099)).count
123+
end
124+
118125
private
119126

120127
def create_model
121128
SpatialModel.lease_connection.create_table(:spatial_models, force: true) do |t|
122129
t.column "latlon", :st_point, srid: 3785
130+
t.column "latlon_geo", :st_point, srid: 4326, geographic: true
123131
t.column "points", :multi_point, srid: 3785
124132
t.column "path", :line_string, srid: 3785
125133
end

0 commit comments

Comments
 (0)