From a9efe07fdb9a815e87b256e04d64d46a56199b27 Mon Sep 17 00:00:00 2001 From: James Chevalier Date: Tue, 5 Nov 2024 13:45:35 -0500 Subject: [PATCH] apply quoting patch From https://github.com/rgeo/activerecord-postgis-adapter/issues/402#issuecomment-2371701919 --- .../connection_adapters/postgis/quoting.rb | 18 ++++++++++++++++++ .../connection_adapters/postgis_adapter.rb | 2 ++ test/cases/spatial_queries_test.rb | 8 ++++++++ 3 files changed, 28 insertions(+) create mode 100644 lib/active_record/connection_adapters/postgis/quoting.rb diff --git a/lib/active_record/connection_adapters/postgis/quoting.rb b/lib/active_record/connection_adapters/postgis/quoting.rb new file mode 100644 index 00000000..1d7c8b98 --- /dev/null +++ b/lib/active_record/connection_adapters/postgis/quoting.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module ActiveRecord + module ConnectionAdapters + module PostGIS + module Quoting + def type_cast(value) + case value + when RGeo::Feature::Instance + value.to_s + else + super + end + end + end + end + end +end diff --git a/lib/active_record/connection_adapters/postgis_adapter.rb b/lib/active_record/connection_adapters/postgis_adapter.rb index 9b890d55..7b80558e 100644 --- a/lib/active_record/connection_adapters/postgis_adapter.rb +++ b/lib/active_record/connection_adapters/postgis_adapter.rb @@ -16,6 +16,7 @@ require_relative "postgis/arel_tosql" require_relative "postgis/oid/spatial" require_relative "postgis/oid/date_time" +require_relative "postgis/quoting" require_relative "postgis/type" # has to be after oid/* # :startdoc: @@ -42,6 +43,7 @@ class PostGISAdapter < PostgreSQLAdapter # http://postgis.17.x6.nabble.com/Default-SRID-td5001115.html DEFAULT_SRID = 0 + include PostGIS::Quoting include PostGIS::SchemaStatements include PostGIS::DatabaseStatements diff --git a/test/cases/spatial_queries_test.rb b/test/cases/spatial_queries_test.rb index c0e6a77a..2a44d3c7 100644 --- a/test/cases/spatial_queries_test.rb +++ b/test/cases/spatial_queries_test.rb @@ -115,11 +115,19 @@ def test_ewkt_parser_query assert_nil(obj3) end + def test_geo_safe_where + create_model + SpatialModel.create!(latlon_geo: geographic_factory.point(-72.1, 42.1)) + SpatialModel.create!(latlon_geo: geographic_factory.point(10.0, 10.0)) + assert_equal 1, SpatialModel.where("ST_DWITHIN(latlon_geo, ?, 500)", geographic_factory.point(-72.099, 42.099)).count + end + private def create_model SpatialModel.lease_connection.create_table(:spatial_models, force: true) do |t| t.column "latlon", :st_point, srid: 3785 + t.column "latlon_geo", :st_point, srid: 4326, geographic: true t.column "points", :multi_point, srid: 3785 t.column "path", :line_string, srid: 3785 end