Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
waissbluth opened this issue Aug 10, 2021 · 3 comments
Open

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

waissbluth opened this issue Aug 10, 2021 · 3 comments

Comments

@waissbluth
Copy link

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
Copy link
Author

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
Copy link
Member

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants