AE
Ash Elixir•3y ago
rohan

How do I use relationship_display_fields

Could you please explain how to use relationship_display_fields? I couldn't figure it out from the docs or the tweet. Alternatively, is there a way to link the referencing id to the object in the admin panel? eg. if you have a user_id, clicking the user_id takes you to the page for that user
29 Replies
ZachDaniel
ZachDaniel•3y ago
Currently there is not a way to do that AFAIK. PRs welcome! Although its looking like we're going to reimagine that project 🙂 relationship_display_fields dictates how the value appears in the data tables i.e relationship_display_fields [:id, :email] would show something like 1 - [email protected]
rohan
rohanOP•3y ago
i still don't follow - does it show how it appears in other data tables? what is the relationship part referring to
ZachDaniel
ZachDaniel•3y ago
Ah, yeah so its referring to other things that are related to it when showing in their data tables, it will show those fields
rohan
rohanOP•3y ago
so if I have user and note and a note belongs to a user adding relationship_display_fields [:email] would make it automatically show the user's email when in note data table?
ZachDaniel
ZachDaniel•3y ago
yes, it should at least 🙂
rohan
rohanOP•3y ago
it doesn't seem to work 😦
ZachDaniel
ZachDaniel•3y ago
😢
rohan
rohanOP•3y ago
do i need to do anything on note as well?
ZachDaniel
ZachDaniel•3y ago
Do you see anything for the user at all in the note table? Or what are you seeing?
rohan
rohanOP•3y ago
just the user_id
ZachDaniel
ZachDaniel•3y ago
🤔 🤔 oh, okay try setting the table columns explicitly
admin do
table_columns [:the, :columns, :and, :user]
end
admin do
table_columns [:the, :columns, :and, :user]
end
rohan
rohanOP•3y ago
this is in note?
ZachDaniel
ZachDaniel•3y ago
So we do that logic when you ask for us to show a relationship in the table columns Yeah What its showing now is the attribute of user_id So asking it to display the :user specifically will trigger that logic
rohan
rohanOP•3y ago
ah that works
ZachDaniel
ZachDaniel•3y ago
I wonder if that is clickable
rohan
rohanOP•3y ago
nope haha
ZachDaniel
ZachDaniel•3y ago
damn 🙂 Honestly the admin UI is very useful but its still super raw
rohan
rohanOP•3y ago
oh but I didn't realize when I click into the table row it shows the relationships right there which is pretty much what i needed
ZachDaniel
ZachDaniel•3y ago
those might actually be clickable
rohan
rohanOP•3y ago
clicking show on there just refreshes the page
ZachDaniel
ZachDaniel•3y ago
🤔 well, thats a bug
rohan
rohanOP•3y ago
is there an easy way to have the table_columns be all the existing columns + user i don't want to enumerate my columns every time
ZachDaniel
ZachDaniel•3y ago
Depends on what you'd call easy 🙂 but the answer is probably no
rohan
rohanOP•3y ago
haha ok
ZachDaniel
ZachDaniel•3y ago
I could walk you through how we might add it though
rohan
rohanOP•3y ago
i guess i can write my own method using the introspection
ZachDaniel
ZachDaniel•3y ago
Yeah, the main problem is that within the DSL module there you can't really do introspection at that point what you'd have to do is write an extension
defmodule MyApp.Extensions.AllAdminColumns do
use Spark.Dsl.Transformer, transformers: [MyApp.Extensions.AllAdminColumns.Transformers.AdminColumns]
end

defmodule MyApp.Extensions.AllAdminColumns.Transformers.AdminColumns do
use Spark.Dsl.Transformer
alias Spark.Dsl.Transformer

def transform(dsl) do
attributes = dsl |> Ash.Resource.Info.attributes() |> Enum.map(&(&1.name))

current_admin_fields = AshAdmin.Resource.table_columns(dsl)
new_admin_fields = Enum.uniq(attributes ++ current_admin_fields)
{:ok, Transformer.set_option(dsl, [:admin], :table_columns, new_admin_fields)}
end
end
defmodule MyApp.Extensions.AllAdminColumns do
use Spark.Dsl.Transformer, transformers: [MyApp.Extensions.AllAdminColumns.Transformers.AdminColumns]
end

defmodule MyApp.Extensions.AllAdminColumns.Transformers.AdminColumns do
use Spark.Dsl.Transformer
alias Spark.Dsl.Transformer

def transform(dsl) do
attributes = dsl |> Ash.Resource.Info.attributes() |> Enum.map(&(&1.name))

current_admin_fields = AshAdmin.Resource.table_columns(dsl)
new_admin_fields = Enum.uniq(attributes ++ current_admin_fields)
{:ok, Transformer.set_option(dsl, [:admin], :table_columns, new_admin_fields)}
end
end
So then you could do
use Ash.Resource,
extensions: [MyApp.Extensions.AllAdminColumns]

admin do
table_columns [:user]
end
use Ash.Resource,
extensions: [MyApp.Extensions.AllAdminColumns]

admin do
table_columns [:user]
end
rohan
rohanOP•3y ago
haha ok i'm going to have to analyze this closely
ZachDaniel
ZachDaniel•3y ago
Yeah its a lot to grok 😆 but once you know how to write extensions you can do pretty much anything Transformers, which are provided by extensions, can make arbitrary transformations to resources So you can use that pattern to do all kinds of stuff, like modify any option/alter the attributes/relationships or whatever you want really.

Did you find this page helpful?