1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//! Computed fields — Django-style computed columns on the admin list view.
//!
//! Models declare a computed field by name in `admin(list_display = "…")`
//! alongside the regular column names; the renderer dispatches to a
//! user-supplied closure via the inventory registry. The closure
//! receives the row as a `serde_json::Value` (a `{ field_name: value }`
//! map produced by [`crate::sql::select_rows_as_json`] — works
//! the same on Postgres / MySQL / SQLite) so it can pull any column
//! it wants and produce pre-escaped display HTML.
//!
//! ## v0.36 breaking change
//!
//! Pre-v0.36 the closure received `&sqlx::postgres::PgRow`. v0.36
//! switched to `&serde_json::Value` so admin's row-rendering path is
//! tri-dialect (the closure no longer cares which backend produced
//! the row). Migration: replace
//! `let body: String = row.try_get("body").unwrap_or_default();`
//! with `let body = row.get("body").and_then(|v| v.as_str()).unwrap_or_default();`.
//!
//! ## Example
//!
//! ```ignore
//! #[derive(rustango::Model)]
//! #[rustango(table = "cms_post", admin(list_display = "title, word_count, updated_at"))]
//! pub struct Post {
//! #[rustango(primary_key)]
//! pub id: rustango::sql::Auto<i64>,
//! pub title: String,
//! pub body: String,
//! pub updated_at: chrono::DateTime<chrono::Utc>,
//! }
//!
//! rustango::register_admin_computed!(
//! "cms_post",
//! "word_count",
//! "Words",
//! |row| {
//! let body = row.get("body").and_then(|v| v.as_str()).unwrap_or_default();
//! body.split_whitespace().count().to_string()
//! }
//! );
//! ```
//!
//! The list view will show a "Words" column populated by the closure.
//! Names that collide with declared fields lose — the column takes
//! precedence, the computed field is ignored.
/// Function signature a computed field implements. Receives the row
/// as a `serde_json::Value` (a `{ field_name → value }` map) and
/// returns the pre-escaped HTML to drop into the cell.
///
/// v0.36: switched from `fn(&PgRow) -> String` to
/// `fn(&serde_json::Value) -> String` for tri-dialect admin. The
/// JSON map is produced by [`crate::sql::row_to_json`] /
/// `row_to_json_my` / `row_to_json_sqlite` per the active backend.
pub type ComputedFieldRenderFn = fn ;
/// Function signature a computed field's optional link callable
/// implements. Receives the same row JSON the renderer does and
/// returns the cell's link target URL — `None` to render the cell
/// inline. Issue #349 — Django parity for `list_display` callables
/// that advertise a click target (e.g. an FK detail page).
pub type ComputedFieldLinkFn = fn ;
/// One computed-field registration. Inventory-collected; submit one
/// per `register_admin_computed!` invocation.
collect!;
/// Return every computed field registered for `table`. Cheap; the
/// inventory iterator is `O(N)` over all registrations but `N` is
/// small (bounded by the number of computed columns declared across
/// the whole binary).
/// Lookup a single computed field by `(table, name)`.
/// Register an admin computed field. Pair with a `#[derive(Model)]`
/// type whose `admin(list_display = "…")` names this field.
///
/// ```ignore
/// rustango::register_admin_computed!(
/// "cms_post", // ModelSchema::table
/// "word_count", // identifier in list_display
/// "Words", // column header
/// |row| {
/// let body = row.get("body").and_then(|v| v.as_str()).unwrap_or_default();
/// body.split_whitespace().count().to_string()
/// }
/// );
/// ```
///
/// Issue #349 — pass `link = |row| Option<String>` to advertise a
/// per-row click target. When `Some(url)` comes back, the admin
/// list view wraps the rendered cell in `<a href="{url}">…</a>`.
///
/// ```ignore
/// rustango::register_admin_computed!(
/// "cms_post",
/// "author_link",
/// "Author",
/// |row| {
/// row.get("author")
/// .and_then(|a| a.get("name"))
/// .and_then(|v| v.as_str())
/// .unwrap_or("—")
/// .to_string()
/// },
/// link = |row| {
/// row.get("author")
/// .and_then(|a| a.get("id"))
/// .and_then(|v| v.as_i64())
/// .map(|id| format!("/__admin/auth_user/{id}"))
/// },
/// );
/// ```