Class: ActiveRecord::ConnectionAdapters::PostGISAdapter::TableDefinition

Inherits:
ConnectionAdapters::PostgreSQLAdapter::TableDefinition
  • Object
show all
Defined in:
lib/active_record/connection_adapters/postgis_adapter/rails4/spatial_table_definition.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(types_, name_, temporary_, options_, as_, base_) ⇒ TableDefinition

Returns a new instance of TableDefinition.



10
11
12
13
14
# File 'lib/active_record/connection_adapters/postgis_adapter/rails4/spatial_table_definition.rb', line 10

def initialize(types_, name_, temporary_, options_, as_, base_)
  @base = base_
  @spatial_columns_hash = {}
  super(types_, name_, temporary_, options_, as_)
end

Instance Method Details

#column(name_, type_, options_ = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/active_record/connection_adapters/postgis_adapter/rails4/spatial_table_definition.rb', line 16

def column(name_, type_, options_={})
  if (info_ = @base.spatial_column_constructor(type_.to_sym))
    type_ = options_[:type] || info_[:type] || type_
    if type_.to_s == 'geometry' &&
      (options_[:no_constraints] ||
       options_[:limit].is_a?(::Hash) && options_[:limit][:no_constraints])
    then
      options_.delete(:limit)
    else
      options_[:type] = type_
      type_ = :spatial
    end
  end
  if type_ == :spatial
    if (limit_ = options_.delete(:limit))
      options_.merge!(limit_) if limit_.is_a?(::Hash)
    end
    if options_[:geographic]
      type_ = :geography
      spatial_type_ = (options_[:type] || 'geometry').to_s.upcase.gsub('_', '')
      spatial_type_ << 'Z' if options_[:has_z]
      spatial_type_ << 'M' if options_[:has_m]
      options_[:limit] = "#{spatial_type_},#{options_[:srid] || 4326}"
    end
    name_ = name_.to_s
    if primary_key_column_name == name_
      raise ArgumentError, "you can't redefine the primary key column '#{name_}'. To define a custom primary key, pass { id: false } to create_table."
    end
    col_ = new_column_definition(name_, type_, options_)
    col_.set_spatial_type(options_[:type])
    col_.set_geographic(options_[:geographic])
    col_.set_srid(options_[:srid])
    col_.set_has_z(options_[:has_z])
    col_.set_has_m(options_[:has_m])
    (col_.geographic? ? @columns_hash : @spatial_columns_hash)[name_] = col_
  else
    super(name_, type_, options_)
  end
  self
end

#create_column_definition(name_, type_) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/active_record/connection_adapters/postgis_adapter/rails4/spatial_table_definition.rb', line 57

def create_column_definition(name_, type_)
  if type_ == :spatial || type_ == :geography
    PostGISAdapter::ColumnDefinition.new(name_, type_)
  else
    super
  end
end

#non_geographic_spatial_columnsObject



65
66
67
# File 'lib/active_record/connection_adapters/postgis_adapter/rails4/spatial_table_definition.rb', line 65

def non_geographic_spatial_columns
  @spatial_columns_hash.values
end