module CruLib::GlobalRegistryMethods::ClassMethods

Public Instance Methods

async_delete_from_global_registry(registry_id) click to toggle source
# File lib/cru_lib/global_registry_methods.rb, line 128
def async_delete_from_global_registry(registry_id)
  begin
    GlobalRegistry::Entity.delete(registry_id)
  rescue RestClient::ResourceNotFound
    # If the record doesn't exist, we don't care
  end
end
columns_to_push() click to toggle source
# File lib/cru_lib/global_registry_methods.rb, line 109
def columns_to_push
  @columns_to_push ||= columns.select { |c|
    !skip_fields_for_gr.include?(c.name.underscore)
  }.collect {|c|
    { name: c.name.underscore, field_type: normalize_column_type(c.type, c.name.underscore) }
  }
end
global_registry_entity_type_name() click to toggle source
# File lib/cru_lib/global_registry_methods.rb, line 136
def global_registry_entity_type_name
  to_s.underscore
end
gr_value(column_name, value) click to toggle source
# File lib/cru_lib/global_registry_methods.rb, line 72
def gr_value(column_name, value)
  return value if value.blank?

  column = columns_to_push.detect { |c| c[:name] == column_name }
  return unless column

  case column[:field_type].to_s
  when 'datetime', 'date'
    value.to_s(:db)
  when 'boolean'
    value ? 'true' : 'false'
  else
    value.to_s.strip
  end
end
normalize_column_type(column_type, name) click to toggle source
# File lib/cru_lib/global_registry_methods.rb, line 117
def normalize_column_type(column_type, name)
  case
  when column_type.to_s == 'text'
    'string'
  when name.ends_with?('_id')
    'uuid'
  else
    column_type
  end
end
push_structure_to_global_registry(parent_id = nil) click to toggle source
# File lib/cru_lib/global_registry_methods.rb, line 88
def push_structure_to_global_registry(parent_id = nil)
  # Make sure all columns exist
  entity_type = Rails.cache.fetch(global_registry_entity_type_name, expires_in: 1.hour) do
    GlobalRegistry::EntityType.get(
      {'filters[name]' => global_registry_entity_type_name, 'filters[parent_id]' => parent_id}
    )['entity_types'].first
  end
  if entity_type
    existing_fields = entity_type['fields'].collect {|f| f['name']}
  else
    entity_type = GlobalRegistry::EntityType.post(entity_type: {name: global_registry_entity_type_name, parent_id: parent_id, field_type: 'entity'})['entity_type']
    existing_fields = []
  end

  columns_to_push.each do |column|
    unless existing_fields.include?(column[:name])
      GlobalRegistry::EntityType.post(entity_type: {name: column[:name], parent_id: entity_type['id'], field_type: column[:field_type]})
    end
  end
end
skip_fields_for_gr() click to toggle source
# File lib/cru_lib/global_registry_methods.rb, line 140
def skip_fields_for_gr
  %w(id global_registry_id created_at updated_at)
end