fix(cabana): show to-many relation list columns joined with commas

This commit is contained in:
Jakub Zych
2026-10-07 18:43:17 +02:00
parent b4cc6f88e4
commit fa25b4e000
3 changed files with 72 additions and 2 deletions

View File

@@ -84,3 +84,41 @@ func TestFieldByColumnTagBeatsGoName(t *testing.T) {
t.Fatalf("a tagged field matched by its Go name: %v", got)
}
}
type listTag struct {
ID uint `gorm:"primarykey"`
Name *string `gorm:"column:name"`
}
type listTagged struct {
ID uint `gorm:"primarykey"`
Tags []listTag `gorm:"many2many:tagged_tags"`
Pointers []*listTag `gorm:"many2many:tagged_pointer_tags"`
}
// TestProjectRowJoinsToManyRelation pins a to-many relation column (relation:
// tags, select: name): the list shows every related record's selected value
// joined with ", " like Winter, and leaves the key unset when there are none.
func TestProjectRowJoinsToManyRelation(t *testing.T) {
news, guides := "News", "Guides"
row := &listTagged{
ID: 3,
Tags: []listTag{{ID: 1, Name: &news}, {ID: 2, Name: &guides}, {ID: 4}},
Pointers: []*listTag{nil, {ID: 2, Name: &guides}},
}
cols := []ListColumn{
{Key: "tags", Relation: "tags", Select: "name"},
{Key: "pointers", Relation: "pointers", Select: "name"},
}
projected := projectRow(row, nil, cols)
if projected["tags"] != "News, Guides" {
t.Fatalf("tags = %#v, want %q", projected["tags"], "News, Guides")
}
if projected["pointers"] != "Guides" {
t.Fatalf("pointers = %#v, want %q", projected["pointers"], "Guides")
}
empty := projectRow(&listTagged{ID: 5}, nil, cols)
if _, ok := empty["tags"]; ok {
t.Fatalf("empty relation projected %#v, want the key unset", empty)
}
}