Class: ForestLiana::SchemaUtils

Inherits:
Object
  • Object
show all
Defined in:
app/services/forest_liana/schema_utils.rb

Class Method Summary collapse

Class Method Details

.associations(active_record_class) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/services/forest_liana/schema_utils.rb', line 4

def self.associations(active_record_class)
  active_record_class.reflect_on_all_associations.select do |association|
    begin
      if (ENV['ENABLE_SUPPORT_POLYMORPHISM'].present? && ENV['ENABLE_SUPPORT_POLYMORPHISM'].downcase == 'true')
        polymorphic?(association) ? true : !is_active_type?(association.klass)
      else
        !polymorphic?(association) && !is_active_type?(association.klass)
      end

    rescue
      FOREST_LOGGER.warn "Unknown association #{association.name} on class #{active_record_class.name}"
      false
    end
  end
end

.belongs_to_associations(active_record_class) ⇒ Object



26
27
28
29
30
# File 'app/services/forest_liana/schema_utils.rb', line 26

def self.belongs_to_associations(active_record_class)
  self.associations(active_record_class).select do |x|
    [:belongs_to].include?(x.macro)
  end
end

.find_model_from_collection_name(collection_name, logs = false) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/services/forest_liana/schema_utils.rb', line 38

def self.find_model_from_collection_name(collection_name, logs = false)
  model_found = nil
  ForestLiana.models.each do |model|
    if model.abstract_class?
      model_found = self.find_model_from_abstract_class(model, collection_name)
    elsif ForestLiana.name_for(model) == collection_name
      model_found = model
    end

    break if model_found
  end

  if logs && model_found.nil?
    FOREST_LOGGER.warn "No model found for collection #{collection_name}"
  end

  model_found
end

.klass(association) ⇒ Object



65
66
67
68
69
# File 'app/services/forest_liana/schema_utils.rb', line 65

def self.klass(association)
  return association.klass unless polymorphic?(association)


end

.many_associations(active_record_class) ⇒ Object



32
33
34
35
36
# File 'app/services/forest_liana/schema_utils.rb', line 32

def self.many_associations(active_record_class)
  self.associations(active_record_class).select do |x|
    [:has_many, :has_and_belongs_to_many].include?(x.macro)
  end
end

.one_associations(active_record_class) ⇒ Object



20
21
22
23
24
# File 'app/services/forest_liana/schema_utils.rb', line 20

def self.one_associations(active_record_class)
  self.associations(active_record_class).select do |x|
    [:has_one, :belongs_to].include?(x.macro)
  end
end

.polymorphic?(association) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'app/services/forest_liana/schema_utils.rb', line 61

def self.polymorphic?(association)
  association.options[:polymorphic]
end

.polymorphic_models(relation) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'app/services/forest_liana/schema_utils.rb', line 71

def self.polymorphic_models(relation)
  models = []
  ForestLiana.models.each do |model|
    unless model.reflect_on_all_associations.select { |association| association.options[:as] == relation.name.to_sym }.empty?
      models << model
    end
  end

  models
end

.tables_namesObject



57
58
59
# File 'app/services/forest_liana/schema_utils.rb', line 57

def self.tables_names
  ActiveRecord::Base.connection.tables
end