module CruLib::GlobalRegistryRelationshipMethods::ClassMethods

Public Instance Methods

columns_to_push() click to toggle source
Calls superclass method
# File lib/cru_lib/global_registry_relationship_methods.rb, line 104
def columns_to_push
  super
end
push_structure_to_global_registry(base_type, related_type, relationship1_name, relationship2_name) click to toggle source

@param [Class] base_type @param [Class] related_type @param [String] relationship1_name @param [String] relationship2_name

# File lib/cru_lib/global_registry_relationship_methods.rb, line 65
def push_structure_to_global_registry(base_type, related_type, relationship1_name, relationship2_name)
  # A summer project application is a join table between people and projects
  base_type_cache_key = "#{base_type.global_registry_entity_type_name}_entity_type"
  base_entity_type = Rails.cache.fetch(base_type_cache_key, expires_in: 1.hour) do
    GlobalRegistry::EntityType.get({'filters[name]' => base_type.global_registry_entity_type_name})['entity_types'].first
  end

  related_type_cache_key = "#{related_type.global_registry_entity_type_name}_entity_type"
  related_entity_type = Rails.cache.fetch(related_type_cache_key, expires_in: 1.hour) do
    GlobalRegistry::EntityType.get({'filters[name]' => related_type.global_registry_entity_type_name})['entity_types'].first
  end

  relationship_type_cache_key = "#{base_type}_#{related_type}_#{relationship1_name}"
  relationship_type = Rails.cache.fetch(relationship_type_cache_key, expires_in: 1.hour) do
    GlobalRegistry::RelationshipType.get(
      {'filters[between]' => "#{base_entity_type['id']},#{related_entity_type['id']}"}
    )['relationship_types'].detect { |r| r['relationship1']['relationship_name'] == relationship1_name }
  end

  unless relationship_type
    relationship_type = GlobalRegistry::RelationshipType.post(relationship_type: {
      entity_type1_id: base_entity_type['id'],
      entity_type2_id: related_entity_type['id'],
      relationship1: relationship1_name,
      relationship2: relationship2_name
    })['relationship_type']
  end

  existing_fields = relationship_type['fields'].collect {|f| f['name']}

  (columns_to_push).each do |field|
    next if existing_fields.include?(field[:name])

    GlobalRegistry::RelationshipType.put(relationship_type['id'], relationship_type: {
      fields: [field]
    })
  end
end