Module: RGeo::ActiveRecord::ActiveRecordBaseFactorySettings

Defined in:
lib/rgeo/active_record/ar_factory_settings.rb

Overview

Additional class methods on ::ActiveRecord::Base that provide a way to control the RGeo factory used for ActiveRecord objects.

Instance Method Summary collapse

Instance Method Details

#rgeo_factory_for_column(column_name, params = nil) ⇒ Object

Returns the factory generator or specific factory to use for this ActiveRecord class and the given column name. If an explicit factory was set for the given column, returns it. Otherwise, if a params hash is given, passes that hash to the rgeo_factory_generator for this class, and returns the resulting factory. Otherwise, if no params hash is given, just returns the rgeo_factory_generator for this class.



116
117
118
# File 'lib/rgeo/active_record/ar_factory_settings.rb', line 116

def rgeo_factory_for_column(column_name, params = nil)
  rgeo_factory_settings.get_column_factory(table_name, column_name, params)
end

#rgeo_factory_generatorObject

The value of this attribute is a RGeo::Feature::FactoryGenerator that is used to generate the proper factory when loading geometry objects from the database. For example, if the data being loaded has M but not Z coordinates, and an embedded SRID, then this FactoryGenerator is called with the appropriate configuration to obtain a factory with those properties. This factory is the one associated with the actual geometry properties of the ActiveRecord object. The result of this generator can be overridden by setting an explicit factory for a given class and column using the column_rgeo_factory method.



87
88
89
# File 'lib/rgeo/active_record/ar_factory_settings.rb', line 87

def rgeo_factory_generator
  rgeo_factory_settings.get_factory_generator(table_name)
end

#rgeo_factory_generator=(generator) ⇒ Object

Set the rgeo_factory_generator attribute



92
93
94
# File 'lib/rgeo/active_record/ar_factory_settings.rb', line 92

def rgeo_factory_generator=(generator)
  rgeo_factory_settings.set_factory_generator(table_name, generator)
end

#rgeo_factory_settingsObject

Return the RGeoFactorySettings object associated with this class’s connection.



67
68
69
70
71
72
73
74
# File 'lib/rgeo/active_record/ar_factory_settings.rb', line 67

def rgeo_factory_settings
  pool = begin
    connection_pool
  rescue ::ActiveRecord::ConnectionNotEstablished
    nil
  end
  pool ? pool.rgeo_factory_settings : RGeoFactorySettings::DEFAULT
end

#set_rgeo_factory_for_column(column_name, factory) ⇒ Object

Set a specific factory for this ActiveRecord class and the given column name. This setting, if present, overrides the result of the rgeo_factory_generator.



105
106
107
# File 'lib/rgeo/active_record/ar_factory_settings.rb', line 105

def set_rgeo_factory_for_column(column_name, factory)
  rgeo_factory_settings.set_column_factory(table_name, column_name, factory)
end

#to_generate_rgeo_factory(&block) ⇒ Object

This is a convenient way to set the rgeo_factory_generator by passing a block.



98
99
100
# File 'lib/rgeo/active_record/ar_factory_settings.rb', line 98

def to_generate_rgeo_factory(&block)
  rgeo_factory_settings.set_factory_generator(table_name, block)
end