Class: ForestLiana::SchemaUtils
- Inherits:
-
Object
- Object
- ForestLiana::SchemaUtils
- Defined in:
- app/services/forest_liana/schema_utils.rb
Class Method Summary collapse
- .associations(active_record_class) ⇒ Object
- .belongs_to_associations(active_record_class) ⇒ Object
- .disable_filter_and_sort_if_cross_db!(opts, name, collection_name) ⇒ Object
- .find_column_schema_by_name(collection_name, field_name) ⇒ Object
- .find_model_from_abstract_class(abstract_class, collection_name) ⇒ Object
- .find_model_from_collection_name(collection_name, logs = false) ⇒ Object
- .habtm?(model) ⇒ Boolean
-
.is_active_type?(model) ⇒ Boolean
NOTICE: Ignores ActiveType::Object association during introspection and interactions.
- .klass(association) ⇒ Object
- .many_associations(active_record_class) ⇒ Object
- .model_included?(model) ⇒ Boolean
- .one_associations(active_record_class) ⇒ Object
- .polymorphic?(association) ⇒ Boolean
- .polymorphic_models(relation) ⇒ Object
- .tables_names ⇒ Object
Class Method Details
.associations(active_record_class) ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# 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 polymorphic?(association) ? true : !is_active_type?(association.klass) 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
21 22 23 24 25 |
# File 'app/services/forest_liana/schema_utils.rb', line 21 def self.belongs_to_associations(active_record_class) self.associations(active_record_class).select do |x| [:belongs_to].include?(x.macro) end end |
.disable_filter_and_sort_if_cross_db!(opts, name, collection_name) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'app/services/forest_liana/schema_utils.rb', line 130 def self.disable_filter_and_sort_if_cross_db!(opts, name, collection_name) return unless opts[:reference] assoc_name = opts[:reference].split('.').first&.underscore&.to_sym || name model = find_model_from_collection_name(collection_name) return unless model association = model.reflect_on_association(assoc_name) return unless association return if polymorphic?(association) model_db = model.connection_db_config.database assoc_db = association.klass.connection_db_config.database if model_db != assoc_db opts[:is_filterable] = false opts[:is_sortable] = false end rescue => e FOREST_LOGGER.warn("Could not evaluate cross-db association for #{name}: #{e.}") end |
.find_column_schema_by_name(collection_name, field_name) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'app/services/forest_liana/schema_utils.rb', line 52 def self.find_column_schema_by_name(collection_name, field_name) schema = ForestLiana.apimap.find { |collection| collection.name == collection_name } if field_name.include?(':') relation, field_name = field_name.split(':') relation_schema = schema.fields.find do |field| field[:field].to_s == relation end foreign_collection_name, = relation_schema[:reference].split('.') return find_column_schema_by_name(foreign_collection_name, field_name) else return schema.fields.find do |field| field[:field].to_s == field_name end end end |
.find_model_from_abstract_class(abstract_class, collection_name) ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'app/services/forest_liana/schema_utils.rb', line 97 def self.find_model_from_abstract_class(abstract_class, collection_name) abstract_class.subclasses.find do |subclass| if subclass.abstract_class? return self.find_model_from_collection_name(subclass, collection_name) else ForestLiana.name_for(subclass) == collection_name end end end |
.find_model_from_collection_name(collection_name, logs = false) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'app/services/forest_liana/schema_utils.rb', line 33 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 |
.habtm?(model) ⇒ Boolean
120 121 122 |
# File 'app/services/forest_liana/schema_utils.rb', line 120 def self.habtm?(model) model.name.starts_with?('HABTM') end |
.is_active_type?(model) ⇒ Boolean
NOTICE: Ignores ActiveType::Object association during introspection and interactions.
See the gem documentation: https://github.com/makandra/active_type
126 127 128 |
# File 'app/services/forest_liana/schema_utils.rb', line 126 def self.is_active_type? model Object.const_defined?('ActiveType::Object') && model < ActiveType::Object end |
.klass(association) ⇒ Object
77 78 79 80 81 |
# File 'app/services/forest_liana/schema_utils.rb', line 77 def self.klass(association) return association.klass unless polymorphic?(association) end |
.many_associations(active_record_class) ⇒ Object
27 28 29 30 31 |
# File 'app/services/forest_liana/schema_utils.rb', line 27 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 |
.model_included?(model) ⇒ Boolean
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'app/services/forest_liana/schema_utils.rb', line 107 def self.model_included?(model) # NOTICE: all models are included by default. return true if ForestLiana.included_models.empty? && ForestLiana.excluded_models.empty? model_name = ForestLiana.name_for(model) if ForestLiana.included_models.any? ForestLiana.included_models.include?(model_name) else ForestLiana.excluded_models.exclude?(model_name) end end |
.one_associations(active_record_class) ⇒ Object
15 16 17 18 19 |
# File 'app/services/forest_liana/schema_utils.rb', line 15 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
73 74 75 |
# File 'app/services/forest_liana/schema_utils.rb', line 73 def self.polymorphic?(association) association.[:polymorphic] end |
.polymorphic_models(relation) ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'app/services/forest_liana/schema_utils.rb', line 83 def self.polymorphic_models(relation) models = [] ForestLiana.models.each do |model| unless model.reflect_on_all_associations.select { |association| association.[:as] == relation.name.to_sym }.empty? models << model end end models end |
.tables_names ⇒ Object
69 70 71 |
# File 'app/services/forest_liana/schema_utils.rb', line 69 def self.tables_names ActiveRecord::Base.connection.tables end |