Module: CruLib::GlobalRegistryMethods::ClassMethods

Defined in:
lib/cru_lib/global_registry_methods.rb

Instance Method Summary collapse

Instance Method Details

#async_delete_from_global_registry(registry_id) ⇒ Object



128
129
130
131
132
133
134
# 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_pushObject



109
110
111
112
113
114
115
# 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_nameObject



136
137
138
# 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) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# 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) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# 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_grObject



140
141
142
# 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