Class: ForestAdminDatasourceMongoid::Utils::Schema::RelationGenerator
- Inherits:
-
Object
- Object
- ForestAdminDatasourceMongoid::Utils::Schema::RelationGenerator
- Extended by:
- Helpers
- Includes:
- ForestAdminDatasourceToolkit::Schema::Relations
- Defined in:
- lib/forest_admin_datasource_mongoid/utils/schema/relation_generator.rb
Class Method Summary collapse
- .add_implicit_relations(collections) ⇒ Object
-
.add_many_to_one_inverse(collection, name, schema) ⇒ Object
Given any many to one relation, generated while parsing mongoose schema, generate the inverse relationship on the foreignCollection.
Methods included from Helpers
compare_ids, deep_merge, escape, group_ids_by_path, recursive_delete, recursive_set, reformat_patch, replace_mongo_types, split_id, unflatten_record, unnest
Class Method Details
.add_implicit_relations(collections) ⇒ Object
8 9 10 11 12 13 14 15 16 |
# File 'lib/forest_admin_datasource_mongoid/utils/schema/relation_generator.rb', line 8 def self.add_implicit_relations(collections) collections.each_value do |collection| many_to_ones = collection.schema[:fields].select { |_, f| f.type == 'ManyToOne' } many_to_ones.each do |(name, field)| add_many_to_one_inverse(collection, name, field) end end end |
.add_many_to_one_inverse(collection, name, schema) ⇒ Object
Given any many to one relation, generated while parsing mongoose schema, generate the inverse relationship on the foreignCollection. /!\ The inverse can be a OneToOne, or a ManyToOne
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/forest_admin_datasource_mongoid/utils/schema/relation_generator.rb', line 21 def self.add_many_to_one_inverse(collection, name, schema) if name == 'parent' # Create inverse of 'parent' relationship so that the relation name matches the actual name # of the data which is stored in the database. stack = collection.stack prefix = stack[stack.length - 1][:prefix] is_array = MongoidSchema.from_model(collection.model).apply_stack(stack).is_array type = is_array ? OneToManySchema : OneToOneSchema inverse_name = escape(prefix) if stack.length > 2 previous_length = stack[stack.length - 2][:prefix].length + 1 inverse_name = prefix[previous_length..] end else inverse_name = escape("#{collection.name}_#{name}__inverse") type = OneToManySchema end other_collection = collection.datasource.get_collection(schema.foreign_collection) other_collection.schema[:fields][inverse_name] = type.new( foreign_collection: collection.name, origin_key: schema.foreign_key, origin_key_target: schema.foreign_key_target ) end |