Module: SpatialFeatures::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/spatial_features/utils.rb

Instance Method Summary collapse

Instance Method Details

#base_class(klass) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/spatial_features/utils.rb', line 20

def base_class(klass)
  case klass
  when String
    klass.constantize.base_class.to_s
  when ActiveRecord::Base
    klass.class.base_class
  when Class
    klass.base_class
  end
end

#base_class_of(object) ⇒ Object



16
17
18
# File 'lib/spatial_features/utils.rb', line 16

def base_class_of(object)
  base_class(class_of(object))
end

#class_name_with_ancestors(object) ⇒ Object



12
13
14
# File 'lib/spatial_features/utils.rb', line 12

def class_name_with_ancestors(object)
  class_of(object).ancestors.select {|k| k < ActiveRecord::Base }.map(&:to_s)
end

#class_of(object) ⇒ Object

Returns the class for the given, class, scope, or record



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/spatial_features/utils.rb', line 32

def class_of(object)
  case object
  when ActiveRecord::Base
    object.class
  when ActiveRecord::Relation
    object.klass
  when String
    object.constantize
  else
    object
  end
end

#geom_from_json(geometry) ⇒ Object

Convert a hash of GeoJSON data into a PostGIS geometry object



61
62
63
# File 'lib/spatial_features/utils.rb', line 61

def geom_from_json(geometry)
  RGeo::GeoJSON.decode(geometry.to_json).as_text
end

#id_sql(object) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/spatial_features/utils.rb', line 45

def id_sql(object)
  case object
  when ActiveRecord::Base
    object.id || '0'
  when String
    id_sql(object.constantize)
  else
    object.unscope(:select).select(:id).to_sql
  end
end

#polymorphic_condition(scope, column_name) ⇒ Object



5
6
7
8
9
10
# File 'lib/spatial_features/utils.rb', line 5

def polymorphic_condition(scope, column_name)
  sql = "#{column_name}_type = ?"
  sql << " AND #{column_name}_id IN (#{id_sql(scope)})" unless scope.is_a?(Class)

  return class_of(scope).send :sanitize_sql, [sql, base_class_of(scope)]
end

#select_db_value(query) ⇒ Object



56
57
58
# File 'lib/spatial_features/utils.rb', line 56

def select_db_value(query)
  ActiveRecord::Base.connection.select_value(query)
end