JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources
Polymorphic code in serializer makes some AR assumptions
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Ruby
- Estrellas
- 2.3k
- Forks
- 546
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
In the following code in resource_serializer.rb
def foreign_key_types_and_values(source, relationship)
if relationship.is_a?(JSONAPI::Relationship::ToMany)
if relationship.polymorphic?
assoc = source._model.public_send(relationship.name)
# Avoid hitting the database again for values already pre-loaded
if assoc.respond_to?(:loaded?) and assoc.loaded?
assoc.map do |obj|
[obj.type.underscore.pluralize, @id_formatter.format(obj.id)]
end
else
assoc.pluck(:type, :id).map do |type, id|
[type.underscore.pluralize, @id_formatter.format(id)]
end
end
else
source.public_send(relationship.name).map do |value|
[relationship.type, @id_formatter.format(value.id)]
end
end
end
end
It makes the assumption that if you specify a polymorphic relationship that it should get the data straight from the model rather than allowing this to be changed via the resource.
It also assumes that we are using "id" and "type" which is very much an active record thing.
In my application, I am using active record, but have a "has_many" relationship to a class which then has a has_many polymorphic. I effectively want to do a has_many through the middle class straight to the polymorphic data which I cannot find a way of doing.
If the above code allowed the resource to intervene here, I could have overriden something.
Guía de contribución
No hay ninguna guía de contribución indexada para este repositorio
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza en resource_serializer.rb, en foreign_key_types_and_values, y sigue cómo las relaciones polimórficas obtienen sus valores. Identifica el punto de extensión a nivel de recurso necesario para evitar las suposiciones de Active Record sobre id/type y verifica después que un recurso pueda proporcionar una relación polimórfica has-many-through sin que el serializador consulte directamente el modelo.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- rails, ruby
- Área
- api, backend
- Tipo de issue
- Refactorización
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100