JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources
Polymorphic code in serializer makes some AR assumptions
Nessuno ha ancora preso questa issue.
- Lingua principale
- Ruby
- Stelle
- 2.3k
- Fork
- 546
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
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.
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia in resource_serializer.rb, in foreign_key_types_and_values, e segui il modo in cui le relazioni polimorfiche ottengono i loro valori. Identifica il punto di estensione a livello di risorsa necessario per evitare le assunzioni di Active Record su id/type, quindi verifica che una risorsa possa fornire una relazione polimorfica has-many-through senza che il serializer interroghi direttamente il modello.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- rails, ruby
- Ambito
- api, backend
- Tipo di issue
- Refactoring
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100