Module: ForestLiana::QueryHelper

Defined in:
app/helpers/forest_liana/query_helper.rb

Class Method Summary collapse

Class Method Details

.get_one_association_names_string(resource) ⇒ Object



20
21
22
# File 'app/helpers/forest_liana/query_helper.rb', line 20

def self.get_one_association_names_string(resource)
  self.get_one_associations(resource).map { |association| association.name.to_s }
end

.get_one_association_names_symbol(resource) ⇒ Object



16
17
18
# File 'app/helpers/forest_liana/query_helper.rb', line 16

def self.get_one_association_names_symbol(resource)
  self.get_one_associations(resource).map(&:name)
end

.get_one_associations(resource) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'app/helpers/forest_liana/query_helper.rb', line 3

def self.get_one_associations(resource)
  associations = SchemaUtils.one_associations(resource)
    .select do |association|
      if SchemaUtils.polymorphic?(association)
        SchemaUtils.polymorphic_models(association).all? { |model| SchemaUtils.model_included?(model) }
      else
        SchemaUtils.model_included?(association.klass)
      end
    end

  associations
end

.get_tables_associated_to_relations_name(resource) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/helpers/forest_liana/query_helper.rb', line 24

def self.get_tables_associated_to_relations_name(resource)
  tables_associated_to_relations_name = {}
  associations_has_one = self.get_one_associations(resource)

  associations_has_one.each do |association|
    if SchemaUtils.polymorphic?(association)
      SchemaUtils.polymorphic_models(association).each do |model|
        if tables_associated_to_relations_name[model.table_name].nil?
          tables_associated_to_relations_name[model.table_name] = []
        end
        tables_associated_to_relations_name[model.table_name] << association.name
      end
    else
      if tables_associated_to_relations_name[association.try(:table_name)].nil?
        tables_associated_to_relations_name[association.table_name] = []
      end
      tables_associated_to_relations_name[association.table_name] << association.name
    end
  end

  tables_associated_to_relations_name
end