township-agency / ex_teal

MIT License
1 stars 0 forks source link

Customize Index Fields in Has Many relationships #196

Closed staylorwr closed 1 year ago

staylorwr commented 1 year ago

Context

As an engineer, I want to configure a custom list of fields for a particular has many relationships. We consistently get client requests for the ability to change which fields end up on has many index table. eg with the following schemas:

User
  id
  name
  email
  has_many :posts, Post

Post
  id
  name
  body
  tags
  belongs_to :author, User

In a case like this we might define the PostResource fields list as something like:

def fields, do: [
  Text.make(:name),
  RichText.make(:body),
  MultiSelect.make(:tags),
  BelongsTo.make(:author, User)
]

What if I don't want the tags & author on the index of the User's posts? What if I want to add other fields like a published at or (insert something else) that makes more sense when viewing an author's list of posts?

Feature

Add a function with_index_fields/2 to has_many.ex that adds those fields to the private options of the `field struct. ie:

:posts |> HasMany.make() |> HasMany.with_index_fields([Text.make(:name), DateTime.make(:published_at)])

Use this list of fields in ExTeal.Resource.Index when a has many resource is queried. We can use relationship_type query param as a switch for which fields should be used.

There's some trickery in getting that list of fields available when sorting, filtering, and searching that will require a little more thought.

staylorwr commented 1 year ago

We could then use this functionality to add some nicer UI around many to many relationships. Currently many-to-many's only contain the belongs-to like resource link in the index view alongside any pivot field relationships. It would be nice to be able to add:

ManyToMany.make(:tags)
|> ManyToMany.with_index_fields([
  Text.make(:title),
  Boolean.make(:active),
  Select.make(:featured),
])
|> ManyToMany.with_pivot_fields([
  Text.make(:foo),
  Boolean.make(:bar)
])

so that the text field replaces the belongs-to like resource link