JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

How to filter by polymorphic resource association filter?

Offen
#1,324 0 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
Ruby
Sterne
2.3k
Forks
546
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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?

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit der Deklaration der polymorphen documentable-Assoziation in DocumentsDocumentableResource und den beiden Beispielen für Filteranfragen. Verfolge, wie JSONAPI Resources Filter für polymorphe Beziehungen validiert, und ermittle, ob typspezifisches Dispatching unterstützt wird. Als erledigt gilt eine dokumentierte, getestete Möglichkeit, den entsprechenden Filter aufzurufen, oder eine klar abgegrenzte Erklärung der Einschränkung.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
rails, ruby
Bereich
api, backend
Issue-Typ
Bug
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.