BanditHijo.dev

Merubah Nama Attribute yang Panjang pada Collection of Object di Rails

Created at: 2023-09-17
Author by: BanditHijo

Pendahuluan

Pada catatan kali ini saya ingin memodifikasi output dari nama attribute yang terlalu panjang menjadi lebih pendek di Ruby on Rails.

Masalah

Saya memiliki method geofences yang mengembalikan colllection of hash data dari model geofeonce.

Model geofence memiliki attributes,

:id, :created_at, :updated_at, :name, :description, :coordinates, :incoming_notification, :outgoing_notification

Saya hanya ingin mengambil attributes: :id, :incoming_notification, :outgoing_notification.

app/geofences_controller.rb
1def geofences
2 {
3 geofences: Geofence.all.order(id: :asc).as_json(only: [:id, :incoming_notification, :outgoing_notification])
4 }
5end

Outputnya,

1{
2 "geofences": [
3 {
4 "id": 1,
5 "incoming_notification": true,
6 "outgoing_notification": false
7 },
8 {
9 "id": 2,
10 "incoming_notification": false,
11 "outgoing_notification": true
12 },
13 {
14 "id": 3,
15 "incoming_notification": true,
16 "outgoing_notification": false
17 },
18 {
19 "id": 4,
20 "incoming_notification": false,
21 "outgoing_notification": true
22 }
23 ]
24}
25

Karena nama attribute :incoming_notification dan :outgoing_notification terlalu panjang, saya akan ganti menjadi :in dan :out.

Pemecahan Masalah

Algoritma Pertama

app/geofences_controller.rb
1def geofences
2 {
3 geofences: Geofence.all.order(id: :asc)
4 .as_json(only: [:id, :incoming_notification, :outgoing_notification])
5 .each { |attribute|
6 attribute.tap { |key|
7 key[:in] = key.delete('incoming_notification')
8 key[:out] = key.delete('outgoing_notification')
9 }
10 }
11 }
12end

Algoritma Kedua

app/geofences_controller.rb
1def geofences
2 geofences = Geofence.all.order(id: :asc)
3
4 transformed_geofences = geofences.map do |geofence|
5 {
6 id: geofence.id,
7 in: geofence.incoming_notification,
8 out: geofence.outgoing_notification
9 }
10 end
11
12 { geofences: transformed_geofences }
13end

Saya menulis 2 pendekatan. Namun, saya cenderung memilih algoritma kedua.

Alasannya adalah,

Database Query Optimization:

  1. algoritma pertama, diambil semua data, lalu dilakukan filtering dan tranformation dengan .as_json
  2. algoritman kedua, hanya mengambil attribute yang diperlukan :id, :incoming_notification, :outgoing_notification dengan .map(), hal ini berpotensi untuk meminimalisir query time ke database

Reduced Object Mutation:

  1. algoritman pertama, menggunakan .each dengan .tap untuk memodifikasi Hash Object, hal ini terkadang dapat menyebabkan behaviour yang tidak dapat diprediksi sehingga menimbulkan bug.
  2. algoritma kedua, langsung membuat Hash Object dengan attribute yang diperlukan tanpa meodifikasi data aslinya, tentu saja ini menjadi pendekatan yang cukup aman

Hasilnya,

1{
2 "geofences": [
3 {
4 "id": 1,
5 "in": true,
6 "out": false
7 },
8 {
9 "id": 2,
10 "in": false,
11 "out": true
12 },
13 {
14 "id": 3,
15 "in": true,
16 "out": false
17 },
18 {
19 "id": 4,
20 "in": false,
21 "out": true
22 }
23 ]
24}
25

Pesan Penulis

Terima kasih sudah mampir yaa.

Referensi

  1. stackoverflow.com: Change property name of as_json call