JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Could _add_relationship generated methods use resource_for_model instead of resource_for?

Aperta
#757 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Type: Bug
Lingua principale
Ruby
Stelle
2.3k
Fork
546
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

In an application I'm working on, we had an issue where we had a model with a polymorphic belongs to association, that used different resource subclasses based on the type of the association.

For the basic index, show, etc. actions this worked fine (you can override resource_type_for or use model hints), but when we tried to access one of these models using a get_related_resource route, we found that it used the superclass resource.

This seems to be because of this method in resource.rb:

define_method relationship_name do |options = {}|
  relationship = self.class._relationships[relationship_name]

  if relationship.polymorphic?
    associated_model = public_send(associated_records_method_name)
    resource_klass = self.class.resource_for_model(associated_model) if associated_model
    return resource_klass.new(associated_model, @context) if resource_klass
  else
    resource_klass = relationship.resource_klass
    if resource_klass
      associated_model = public_send(associated_records_method_name)
      return associated_model ? resource_klass.new(associated_model, @context) : nil
    end
  end
end unless method_defined?(relationship_name)

Using relationship.resource_klass looks up the resource class using the class of the model, which doesn't allow for the resource class to be changed based on attributes of the model. At this point, the model instance is known, so it should be possible to adapt it to look up the resource class based on the model instance instead of the model class.

Currently, we're getting around this by overriding .new on the superclass relationship like this, but it's pretty hacky:

def self.new(model, *args)
  return super if self < V1::QuestionResource || model.content_type.nil?
  resource_for_model(model).new(model, *args)
end

Is this something there'd be interest in changing?

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia in lib/jsonapi/resource.rb, nel metodo di relazione generato mostrato nell’issue, confrontando i rami polimorfico e non polimorfico. Segui il percorso con cui get_related_resource raggiunge questo metodo e verifica che l’istanza del modello possa selezionare la sottoclasse di risorsa prevista senza compromettere il comportamento esistente delle relazioni.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
ruby
Ambito
api, backend
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.