|
| 1 | +module ForestAdminDatasourceRpc |
| 2 | + class ReconciliateRpc < ForestAdminDatasourceCustomizer::Plugins::Plugin |
| 3 | + def run(datasource_customizer, _collection_customizer = nil, _options = {}) |
| 4 | + datasource_customizer.datasources.each do |datasource| |
| 5 | + next unless datasource.is_a?(ForestAdminDatasourceRpc::Datasource) |
| 6 | + |
| 7 | + # Disable search for non-searchable collections |
| 8 | + datasource.collections.each do |_name, collection| |
| 9 | + unless collection.schema[:searchable] |
| 10 | + cz = datasource_customizer.get_collection(collection.name) |
| 11 | + cz.disable_search |
| 12 | + end |
| 13 | + end |
| 14 | + |
| 15 | + # Add relations from rpc_relations |
| 16 | + (datasource.rpc_relations || {}).each do |collection_name, relations| |
| 17 | + cz = datasource_customizer.get_collection(collection_name) |
| 18 | + |
| 19 | + relations.each do |relation_name, relation_definition| |
| 20 | + add_relation(cz, relation_name, relation_definition) |
| 21 | + end |
| 22 | + end |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + private |
| 27 | + |
| 28 | + def add_relation(collection_customizer, relation_name, relation_definition) |
| 29 | + type = relation_definition[:type] || relation_definition['type'] |
| 30 | + foreign_collection = relation_definition[:foreign_collection] || relation_definition['foreign_collection'] |
| 31 | + |
| 32 | + case type |
| 33 | + when 'ManyToMany' |
| 34 | + through_collection = relation_definition[:through_collection] || relation_definition['through_collection'] |
| 35 | + collection_customizer.add_many_to_many_relation( |
| 36 | + relation_name, |
| 37 | + foreign_collection, |
| 38 | + through_collection, |
| 39 | + { |
| 40 | + foreign_key: relation_definition[:foreign_key] || relation_definition['foreign_key'], |
| 41 | + foreign_key_target: relation_definition[:foreign_key_target] || relation_definition['foreign_key_target'], |
| 42 | + origin_key: relation_definition[:origin_key] || relation_definition['origin_key'], |
| 43 | + origin_key_target: relation_definition[:origin_key_target] || relation_definition['origin_key_target'] |
| 44 | + } |
| 45 | + ) |
| 46 | + when 'OneToMany' |
| 47 | + collection_customizer.add_one_to_many_relation( |
| 48 | + relation_name, |
| 49 | + foreign_collection, |
| 50 | + { |
| 51 | + origin_key: relation_definition[:origin_key] || relation_definition['origin_key'], |
| 52 | + origin_key_target: relation_definition[:origin_key_target] || relation_definition['origin_key_target'] |
| 53 | + } |
| 54 | + ) |
| 55 | + when 'OneToOne' |
| 56 | + collection_customizer.add_one_to_one_relation( |
| 57 | + relation_name, |
| 58 | + foreign_collection, |
| 59 | + { |
| 60 | + origin_key: relation_definition[:origin_key] || relation_definition['origin_key'], |
| 61 | + origin_key_target: relation_definition[:origin_key_target] || relation_definition['origin_key_target'] |
| 62 | + } |
| 63 | + ) |
| 64 | + else # ManyToOne |
| 65 | + collection_customizer.add_many_to_one_relation( |
| 66 | + relation_name, |
| 67 | + foreign_collection, |
| 68 | + { |
| 69 | + foreign_key: relation_definition[:foreign_key] || relation_definition['foreign_key'], |
| 70 | + foreign_key_target: relation_definition[:foreign_key_target] || relation_definition['foreign_key_target'] |
| 71 | + } |
| 72 | + ) |
| 73 | + end |
| 74 | + end |
| 75 | + end |
| 76 | +end |
0 commit comments