kaffy icon indicating copy to clipboard operation
kaffy copied to clipboard

[FEATURE-REQUEST] Support schemas that implement custom Phoenix.Param protocol

Open jtormey opened this issue 5 years ago • 0 comments

Describe the problem you're proposing to solve

When a schema implements Phoenix.Param, links in the admin panel go to a path containing the custom param. However, Kaffy tries to use this custom param as an :id when querying the database. This results in the error:

deps/kaffy/lib/kaffy/resource_query.ex:49: value `"my-custom-param"` cannot be dumped to type :binary_id in query:

Describe the solution you'd like

How I've solved this in other admin panels (specifically, using Torch) is to add a virtual field to the schema definition (something along the lines of :param_context) that gets populated to :admin when the admin backend queries for records of that schema.

Then, in the defimpl for the struct, the library user can match on this virtual field. In the case that the :param_context is set to :admin, the implementing function can return the :id, allowing the admin backend to use the result like it would any other.

Describe alternatives you've considered

An alternative could be to offer full support for custom params, potentially via a new protocol: Kaffy.Param, which requires implementing a from_param/1 function and returns the correct database field to query and a value to query for. Although, I like the idea of admin panels using database IDs exclusively instead of custom user-facing ones.

Additional context

  • Phoenix.Param
  • Related, on the handling of non-standard IDs: https://github.com/aesmail/kaffy/issues/6

jtormey avatar Oct 21 '20 16:10 jtormey