Closed vtno closed 5 years ago
Adding a custom name resolver seems to solve the problem but it'd still be nice if this could be config via render
method as an extra attribute.
# initializer/jsonapi.rb
# Set a default serializable class mapping.
config.jsonapi_class = Hash.new { |h, k|
names = k.to_s.split('::')
klass = names.pop
h[k] = [*names, "Serializable#{klass}"].join('::').safe_constantize
}.tap { |h|
h[:CharacterQualityBadge] = SerializableQualityBadge
}
You can override it by:
render jsonapi: post, class: jsonapi_class.merge(CharacterQualityBadge: SerializableQualityBadge)
I have this
Course
model which hashas_many: quality_badges
relation but the actual class is namedCharacterQualityBadge
In the controller I cannot do
It'll throw
undefined method new' for nil:NilClass
because there is noQualityBadge
model class.I need to add an alias method in the Model and use it in the serializable class in order to make it work. ( so: quality_badge -> character_quality_badge)
It'd be great if I can specify the mapping of the class name inside
linkage
method or any other proper place like what you guys did for mapping resource with it serializer class.render jsonapi: post, class: { Post: SerializableCustomPost }