JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

How to filter by polymorphic resource association filter?

Open
#1,324 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
2.3k
Forks
546
PR merge metrics
No merged PRs in 30d

Description

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?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the DocumentsDocumentableResource declaration of the polymorphic documentable association and the two filter request examples. Trace how JSONAPI Resources validates filters for polymorphic relationships and determine whether type-specific dispatch is supported. Done means a documented, tested way to invoke the appropriate filter, or a clearly scoped explanation of the limitation.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
api, backend
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.