Module: SpatialFeatures::Utils

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

Instance Method Summary collapse

Instance Method Details

#class_of(object) ⇒ Object

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



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/spatial_features/utils.rb', line 13

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

#id_sql(object) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/spatial_features/utils.rb', line 26

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, class_of(scope)]
end