From 43239aff490a6f57cb39e2bb7403d0a06cd61657 Mon Sep 17 00:00:00 2001 From: Shuhei Kitagawa Date: Wed, 14 Mar 2018 09:48:13 +0900 Subject: [PATCH] Enable to set customized id column --- lib/fast_jsonapi/serialization_core.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/fast_jsonapi/serialization_core.rb b/lib/fast_jsonapi/serialization_core.rb index db6a2cf..3c2e9b7 100644 --- a/lib/fast_jsonapi/serialization_core.rb +++ b/lib/fast_jsonapi/serialization_core.rb @@ -14,6 +14,7 @@ module FastJsonapi :cachable_relationships_to_serialize, :uncachable_relationships_to_serialize, :record_type, + :record_id, :cache_length, :cached end @@ -74,7 +75,8 @@ module FastJsonapi def record_hash(record) if cached record_hash = Rails.cache.fetch(record.cache_key, expires_in: cache_length) do - temp_hash = id_hash(record.id, record_type) || { id: nil, type: record_type } + id = record_id ? record.send(record_id) : record.id + temp_hash = id_hash(id, record_type) || { id: nil, type: record_type } temp_hash[:attributes] = attributes_hash(record) if attributes_to_serialize.present? temp_hash[:relationships] = {} temp_hash[:relationships] = relationships_hash(record, cachable_relationships_to_serialize) if cachable_relationships_to_serialize.present? @@ -83,7 +85,8 @@ module FastJsonapi record_hash[:relationships] = record_hash[:relationships].merge(relationships_hash(record, uncachable_relationships_to_serialize)) if uncachable_relationships_to_serialize.present? record_hash else - record_hash = id_hash(record.id, record_type) || { id: nil, type: record_type } + id = record_id ? record.send(record_id) : record.id + record_hash = id_hash(id, record_type) || { id: nil, type: record_type } record_hash[:attributes] = attributes_hash(record) if attributes_to_serialize.present? record_hash[:relationships] = relationships_hash(record) if relationships_to_serialize.present? record_hash