Module: CruLib::GlobalRegistryRelationshipMethods::ClassMethods

Defined in:
lib/cru_lib/global_registry_relationship_methods.rb

Instance Method Summary collapse

Instance Method Details

#columns_to_pushObject



104
105
106
# 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) ⇒ Object

Parameters:

  • base_type (Class)
  • related_type (Class)
  • relationship1_name (String)
  • relationship2_name (String)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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