app/models/maestrano/connector/rails/concerns/entity.rb in maestrano-connector-rails-0.3.1 vs app/models/maestrano/connector/rails/concerns/entity.rb in maestrano-connector-rails-0.3.2

- old
+ new

@@ -106,23 +106,40 @@ # Return a string representing the object from an external entity hash def object_name_from_external_entity_hash(entity) raise "Not implemented" end + + # [{reference_class: Entities::.., connec_field: 'account_id', external_field: 'account/something/id'}] + # ledger_account_idmap = Entities::Account.find_idmap({connec_id: entity['account_id'], organization_id: organization.id}) + # ledger_account_id = ledger_account_idmap && ledger_account_idmap.external_id + def references + [] + end end # ---------------------------------------------- # Mapper methods # ---------------------------------------------- # Map a Connec! entity to the external format def map_to_external(entity, organization) - self.class.mapper_class.normalize(entity) + ref_hash = {} + self.class.references.each do |ref| + ref_hash.merge! ref[:external_field].split('/').reverse.inject(self.class.id_from_ref(entity, ref, false, organization)) { |a, n| { n.to_sym => a } } + end + + self.class.mapper_class.normalize(entity).merge(ref_hash) end # Map an external entity to Connec! format def map_to_connec(entity, organization) - self.class.mapper_class.denormalize(entity) + ref_hash = {} + self.class.references.each do |ref| + ref_hash.merge! ref[:connec_field].split('/').reverse.inject(self.class.id_from_ref(entity, ref, true, organization)) { |a, n| { n.to_sym => a } } + end + + self.class.mapper_class.denormalize(entity).merge(ref_hash) end # ---------------------------------------------- # Connec! methods # ---------------------------------------------- @@ -381,9 +398,27 @@ nil end else entity_instance.map_external_entity_with_idmap(external_entity, connec_entity_name, idmap, organization) + end + end + + def id_from_ref(entity, ref, is_external, organization) + # field can be address/billing/country_id + field = is_external ? ref[:external_field] : ref[:connec_field] + field = field.split('/') + id = entity + field.each do |f| + id &&= id[f] + end + + if is_external + idmap = ref[:reference_class].find_idmap({external_id: id, organization_id: organization.id}) + idmap && idmap.connec_id + else + idmap = ref[:reference_class].find_idmap({connec_id: id, organization_id: organization.id}) + idmap && idmap.external_id end end end def map_external_entity_with_idmap(external_entity, connec_entity_name, idmap, organization) \ No newline at end of file