11module FastJsonapi
22 class Relationship
3- attr_reader :key , :name , :id_method_name , :record_type , :object_method_name , :object_block , :serializer , :relationship_type , :cached , :polymorphic , :conditional_proc
3+ attr_reader :key , :name , :id_method_name , :record_type , :object_method_name , :object_block , :serializer , :relationship_type , :cached , :polymorphic , :conditional_proc , :transform_method
44
55 def initialize (
66 key :,
@@ -13,7 +13,8 @@ def initialize(
1313 relationship_type :,
1414 cached : false ,
1515 polymorphic :,
16- conditional_proc :
16+ conditional_proc :,
17+ transform_method :
1718 )
1819 @key = key
1920 @name = name
@@ -26,6 +27,7 @@ def initialize(
2627 @cached = cached
2728 @polymorphic = polymorphic
2829 @conditional_proc = conditional_proc
30+ @transform_method = transform_method
2931 end
3032
3133 def serialize ( record , serialization_params , output_hash )
@@ -68,7 +70,7 @@ def ids_hash_from_record_and_relationship(record, params = {})
6870
6971 def id_hash_from_record ( record , record_types )
7072 # memoize the record type within the record_types dictionary, then assigning to record_type:
71- associated_record_type = record_types [ record . class ] ||= record . class . name . demodulize . underscore . to_sym
73+ associated_record_type = record_types [ record . class ] ||= run_key_transform ( record . class . name . demodulize . underscore )
7274 id_hash ( record . id , associated_record_type )
7375 end
7476
@@ -93,5 +95,13 @@ def fetch_id(record, params)
9395 end
9496 record . public_send ( id_method_name )
9597 end
98+
99+ def run_key_transform ( input )
100+ if self . transform_method . present?
101+ input . to_s . send ( *self . transform_method ) . to_sym
102+ else
103+ input . to_sym
104+ end
105+ end
96106 end
97- end
107+ end
0 commit comments