JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

How to filter by polymorphic resource association filter?

Aperta
#1,324 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

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

This issue is a (choose one):

  • Problem/bug report.

Checklist before submitting:

  • I've searched for an existing issue.

Description

Bug reports:

If for example I have such setup, I have Document model that have many DocumentsDocumentables, which have documentable as polymorphic association and can have either Employee or Comment in it. I set up :

class Document < ApplicationRecord
  has_many :documents_documentables, dependent: :destroy
end

class DocumentsDocumentable < ApplicationRecord
  belongs_to :document
  belongs_to :documentable, polymorphic: true
end

class Employee < ApplicationRecord
  has_many :documents_documentables, as: :documentable
end

class Comment < ApplicationRecord
  has_many :documents_documentables, as: :documentable
end

module Api
  module V1
    class DocumentResource < BaseResource
      attributes :file_name

      has_many :documents_documentables

      filter :id
    end
  end
end

module Api
  module V1
    class DocumentsDocumentableResource < BaseResource
      attributes :documentable_id, :documentable_type, :document_id

      has_one :document
      has_one :documentable, polymorphic: true, eager_load_on_include: false
    end
  end
end

module Api
  module V1
    class EmployeeResource < BaseResource
      filter :custom_employee_filter, apply: lambda { |records, value, _options|
                                  puts "custom employee filter"
                                }
    end
  end
end

module Api
  module V1
    class CommentResource < BaseResource
      filter :custom_comment_filter, apply: lambda { |records, value, _options|
                                  puts "custom comment filter"
                                }
    end
  end
end

Having this setup, how can I filter DocumentDocumentable by documentable.custom_<employee | comment>_filter then?

I tried smth like that:

/api/v1/documents-documentables?filter=documentable.customEmployeeFilter=filterValue , or
/api/v1/documents-documentables?filter=documentable.customCommentFilter=filterValue

Both gives me error:

{
  error: {
    code: "102",
    detail: "documentable.customEmployeeFilter is not allowed.",
    status: "400",
    title: "Filter not allowed"
  }
}

Is it possible at all somehow to call both filters, and if yes - how should I change my resources, is there any param I can pass to further specialize polymorpthic resource's type?

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 con la dichiarazione dell’associazione polimorfica documentable di DocumentsDocumentableResource e con i due esempi di richieste di filtro. Traccia il modo in cui JSONAPI Resources convalida i filtri per le relazioni polimorfiche e determina se è supportato il dispatch specifico per tipo. Il lavoro è considerato completato quando esiste un modo documentato e testato per invocare il filtro appropriato, oppure una spiegazione chiaramente circoscritta della limitazione.

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

Valutazione

Stack tecnologico
rails, ruby
Ambito
api, backend
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.