Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/rgeo/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
require_relative "active_record/arel_spatial_queries"
require_relative "active_record/common_adapter_elements"
require_relative "active_record/geometry_mixin"
require_relative "active_record/multi_geom_sanitization"
17 changes: 17 additions & 0 deletions lib/rgeo/active_record/multi_geom_sanitization.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true
require 'active_support/core_ext/array/wrap'

module MultiGeomSanitization
private

# NOTE connection and value order is swapped in Rails 8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should we do about this note?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add a

if ActiveRecord::VERSION::MAJOR < 8

def replace_bind_variable(connection, value)
if value.class.name.start_with?("RGeo::") && value.respond_to?(:map)
super(connection, Array.wrap(value))
else
super
end
end
end

ActiveRecord::Sanitization::ClassMethods.prepend(MultiGeomSanitization)
10 changes: 10 additions & 0 deletions test/basic_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ def test_arel_visit_RGeo_ActiveRecord_SpatialNamedFunction_with_distinct
assert_equal("SPATIAL_FUNC(DISTINCT ST_GeomFromText('POINT (1.0 2.0)'), ST_GeomFromText('LINESTRING (1.0 2.0, 2.0 3.0)'))", sql.value)
end

def test_multi_geom_sanitization
multi_geom = RGeo::Geos.factory.parse_wkt("MULTIPOLYGON (((0 0, 0 1, 1 1, 0 0)),((1 1, 0 0, 0 1, 1 1)))")
sql = "ST_DWithin(geom, :geom, :buffer)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need that st_dwithin function for the test?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not need it. It could be SQL valid or not that has a geom interpolated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd keep it simple then :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me a single ST function is pretty simple. Would you prefer ST_DWithin -> ST_Intersects or SPATIAL_FUNC (as the SQL doesn't have to be valid)?


assert_equal(
"ST_DWithin(geom, 'MULTIPOLYGON (((0 0, 0 1, 1 1, 0 0)), ((1 1, 0 0, 0 1, 1 1)))', '10')",
FakeRecord::Base.sanitize_sql([ sql, geom: multi_geom, buffer: 10 ])
)
end

private

def arel_visitor
Expand Down
10 changes: 10 additions & 0 deletions test/support/fake_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ def quote(thing, column = nil)
"'#{thing.to_s.gsub("'", "\\\\'")}'"
end
end

def cast_bound_value(value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why using this fake record class rather than any record?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't see any record, what do you suggest? It looks like the tests don't require a database so my guess is the fake record was used to avoid a db being required for tests but I could be missing something.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh you are right ! And could we use one method deeper in the backtrace rather than #sanitize_sql and not need this fake record while still testing properly ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There isn't a public method below #sanitize_sql, the rest are private

value.to_s
end
end

class ConnectionPool
Expand Down Expand Up @@ -123,8 +127,14 @@ def quote(thing, column = nil)
end

class Base
include ActiveRecord::Sanitization

attr_accessor :connection_pool

def self.with_connection
yield new.connection
end

def initialize
@connection_pool = ConnectionPool.new
end
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require "minitest/autorun"
require "rgeo-activerecord"
require "support/fake_record"
require "active_support/core_ext/date/acts_like"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My concern is that a user loading our gem would have the same issue and need to load this as well. It is in the rails codebase that this should be loaded prior usage, not here in tests. So either our test suite is not initialising properly (I suspect that is the issue) or there is an issue in rails codebase. WDYT?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think Rails is initialised at all. The specs on initialise the minimal things required which I believe is active record.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikolai-b I don't think this supersedes my comment. If we load active-record in our gem, then active-record code should be working, no?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. This is because in the gem the FakeRecord::Base is being used. If instead ActiveRecord::Base is used then this require is not needed as then
https://github.com/rails/rails/blob/6f57590388ca38ed2b83bc1207a8be13a9ba2aef/activerecord/lib/active_record/base.rb#L6 which pulls in acts_like https://github.com/rails/rails/tree/6f57590388ca38ed2b83bc1207a8be13a9ba2aef/activesupport/lib/active_support/core_ext/time . Using active_record without using ActiveRecord::Base is very strange but it is what is done in the specs here.


Arel::Visitors::ToSql.include RGeo::ActiveRecord::SpatialToSql
Arel::Table.engine = FakeRecord::Base.new
Expand Down