Class: ForestAdminAgent::Utils::Id
- Inherits:
-
Object
- Object
- ForestAdminAgent::Utils::Id
- Includes:
- ForestAdminDatasourceToolkit, ForestAdminDatasourceToolkit::Utils
- Defined in:
- lib/forest_admin_agent/utils/id.rb
Class Method Summary collapse
- .pack_id(schema, record) ⇒ Object
- .pack_ids(schema, records) ⇒ Object
- .parse_selection_ids(collection, params, with_key: false) ⇒ Object
- .unpack_id(collection, packed_id, with_key: false) ⇒ Object
- .unpack_ids(collection, packed_ids, with_key: false) ⇒ Object
Class Method Details
.pack_id(schema, record) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/forest_admin_agent/utils/id.rb', line 11 def self.pack_id(schema, record) pk_names = ForestAdminDatasourceToolkit::Utils::Schema.primary_keys(schema) raise Exceptions::ForestException, 'This collection has no primary key' if pk_names.empty? pk_names.map { |pk| record[pk].to_s }.join('|') end |
.pack_ids(schema, records) ⇒ Object
7 8 9 |
# File 'lib/forest_admin_agent/utils/id.rb', line 7 def self.pack_ids(schema, records) records.map { |packed_id| pack_id(schema, packed_id) } end |
.parse_selection_ids(collection, params, with_key: false) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/forest_admin_agent/utils/id.rb', line 42 def self.parse_selection_ids(collection, params, with_key: false) attributes = begin params.dig(:data, :attributes) rescue StandardError nil end are_excluded = attributes&.key?(:all_records) ? attributes[:all_records] : false input_ids = attributes&.key?(:ids) ? attributes[:ids] : params[:data].map { |item| item['id'] } ids = unpack_ids( collection, are_excluded ? attributes[:all_records_ids_excluded] : input_ids, with_key: with_key ) { are_excluded: are_excluded, ids: ids } end |
.unpack_id(collection, packed_id, with_key: false) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/forest_admin_agent/utils/id.rb', line 19 def self.unpack_id(collection, packed_id, with_key: false) primary_keys = ForestAdminDatasourceToolkit::Utils::Schema.primary_keys(collection) primary_key_values = packed_id.to_s.split('|') if (nb_pks = primary_keys.size) != (nb_values = primary_key_values.size) raise Exceptions::ForestException, "Expected #{nb_pks} primary keys, found #{nb_values}" end result = primary_keys.map.with_index do |pk_name, index| field = collection.schema[:fields][pk_name] value = primary_key_values[index] casted_value = field.column_type == 'Number' ? value.to_i : value ForestAdminDatasourceToolkit::Validations::FieldValidator.validate_value(pk_name, field, casted_value) [pk_name, casted_value] end.to_h with_key ? result : result.values end |
.unpack_ids(collection, packed_ids, with_key: false) ⇒ Object
38 39 40 |
# File 'lib/forest_admin_agent/utils/id.rb', line 38 def self.unpack_ids(collection, packed_ids, with_key: false) packed_ids.map { |item| unpack_id(collection, item, with_key: with_key) } end |