Class: Apiwork::Representation::ModelDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/apiwork/representation/model_detector.rb

Instance Method Summary collapse

Constructor Details

#initialize(representation_class) ⇒ ModelDetector

Returns a new instance of ModelDetector.



6
7
8
# File 'lib/apiwork/representation/model_detector.rb', line 6

def initialize(representation_class)
  @representation_class = representation_class
end

Instance Method Details

#detectObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/apiwork/representation/model_detector.rb', line 10

def detect
  return nil if @representation_class.abstract?

  full_name = @representation_class.name
  return nil unless full_name

  representation_name = full_name.demodulize
  model_name = representation_name.delete_suffix('Representation')
  return nil if model_name.blank?

  resolve_model_class(full_name, model_name)
end

#sti_base?(model_class) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/apiwork/representation/model_detector.rb', line 23

def sti_base?(model_class)
  return false if model_class.abstract_class?

  column = model_class.inheritance_column
  return false unless column

  begin
    return false unless model_class.column_names.include?(column.to_s)
  rescue ActiveRecord::StatementInvalid, ActiveRecord::NoDatabaseError, ActiveRecord::ConnectionNotEstablished
    return false
  end

  model_class == model_class.base_class
end

#sti_subclass?(model_class) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
# File 'lib/apiwork/representation/model_detector.rb', line 38

def sti_subclass?(model_class)
  return false if model_class.abstract_class?

  model_class != model_class.base_class
end

#superclass_is_sti_base?(model_class) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
# File 'lib/apiwork/representation/model_detector.rb', line 44

def superclass_is_sti_base?(model_class)
  parent_model = @representation_class.superclass.model_class
  return false unless parent_model

  parent_model == model_class.base_class
end