Module: DBViewCTI::Model::CTI::TypeConversion

Included in:
DBViewCTI::Model::CTI
Defined in:
lib/db_view_cti/model/cti/type_conversion.rb

Instance Method Summary collapse

Instance Method Details

#convert_to(type) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/db_view_cti/model/cti/type_conversion.rb', line 33

def convert_to(type)
  return nil unless persisted?
  type_string = type.to_s
  type_string = type_string.camelize if type.is_a?(Symbol)
  return self if type_string == self.class.name
  query = self.class.cti_inner_join_sql(id, type_string)
  # query is nil when we try to cenvert to a descendant class (instead of an ascendant),
  # or when we try to convert to a class outside of the hierarchy
  if query.nil?
    specialized = specialize
    return nil if specialized == self
    return specialized.convert_to(type_string)
  end
  result = self.class.connection.execute(query).first
  id = result[ DBViewCTI::Names.foreign_key(type.to_s) ]
  return nil if id.nil?
  type_string.constantize.find(id.to_i)
end

#specializeObject



6
7
8
9
10
# File 'lib/db_view_cti/model/cti/type_conversion.rb', line 6

def specialize
  class_name, id = type(true)
  return self if class_name == self.class.name
  class_name.constantize.find(id)
end

#type(return_id = false) ⇒ Object

Return the ‘true’ (i.e. most specialized) classname of this object When return_id is true, the ‘specialized’ database id is also returned



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/db_view_cti/model/cti/type_conversion.rb', line 14

def type(return_id = false)
  query, levels = self.class.cti_outer_join_sql(id)
  result = self.class.connection.execute(query).first
  # replace returned ids with the levels corresponding to their classes
  result_levels = result.inject({}) do |hash, (k,v)|
    hash[k] = levels[k] unless v.nil?
    hash
  end
  # find class with maximum level value
  foreign_key = result_levels.max_by { |k,v| v }.first
  class_name = DBViewCTI::Names.table_to_class_name(foreign_key[0..-4])
  if return_id
    id_ = result[foreign_key].to_i
    [class_name, id_]
  else
    class_name
  end
end