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
//! Computed columns for the admin list view.
//!
//! A model names a computed field in `admin(list_display = "…")` next
//! to its real columns, and the renderer calls a closure from the
//! inventory registry. The closure gets the row as a
//! `serde_json::Value` map of `{ field_name: value }`, the same on
//! every backend, and returns display HTML that is already escaped.
//!
//! ## 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 then shows a "Words" column filled by the closure. A
//! declared field wins any name clash: the computed field is ignored.
/// Renderer for a computed field. Takes the row as a
/// `serde_json::Value` map and returns the cell's HTML, already
/// escaped.
pub type ComputedFieldRenderFn = fn ;
/// Optional link callable for a computed field. Takes the same row
/// JSON as the renderer and returns the cell's link target, or `None`
/// to render the cell without a link.
pub type ComputedFieldLinkFn = fn ;
/// One computed-field registration, collected by inventory. Each
/// `register_admin_computed!` submits one.
collect!;
/// Every computed field registered for `table`. The scan is `O(N)`
/// over all registrations in the binary, which stays small.
/// Find one 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()
/// }
/// );
/// ```
///
/// Pass `link = |row| Option<String>` to give the cell a click target.
/// On `Some(url)` the list view wraps the 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}"))
/// },
/// );
/// ```