rgeo / rgeo-activerecord

RGeo ActiveRecord extensions and tools for spatial connection adapters
Other
89 stars 64 forks source link

How to configure the factory for a specific column in a model #68

Open waissbluth opened 3 years ago

waissbluth commented 3 years ago

The readme and wiki cover how to configure new factories as a default or for specific geometry types, but I am wondering how to configure a factory for a specific model. Is this possible?

Thanks!

waissbluth commented 3 years ago

Looks like i am looking for an alternative to the (deprecated?) set_rgeo_factory_for_column. Is there no longer a way of doing this?

keithdoggett commented 3 years ago

Hi @waissbluth unfortunately, there is not a way to set a factory per model. The current version works with ActiveRecord's TypeMap that maps a column's OID to a Type (ex. https://github.com/rgeo/activerecord-postgis-adapter/blob/master/lib/active_record/connection_adapters/postgis/oid/spatial.rb). So the serialization happens outside of the scope of the model.

You could probably set a factory in the desired model and override the geometry attributes using RGeo::Feature.cast to cast them to the model's factory.

class MyModel < ActiveRecord::Base
  CLASS_SPECIFIC_FACTORY = RGeo::Cartesian.factory

  def spatial_attribute
    RGeo::Feature.cast(self[:spatial_attribute], factory: CLASS_SPECIFIC_FACTORY)
  end
end

The method you mentioned predates my time with the gem and I'm not sure how different the structure of the adapters are between now and then, perhaps @teeparham has some insight about that.

waissbluth commented 3 years ago

there is not a way to set a factory per model.

Thank you @keithdoggett, that's too bad. I think doing that would make a lot of sense in my case.

@teeparham if you have a moment to chime in about this it would be great! Thanks.