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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//! The trait a table implements to be managed by the admin panel.
use std::future::Future;
use orbit_orm::{Database, Error};
use serde::Serialize;
use crate::condition::FieldConditions;
use crate::field::{Column, Field};
use crate::form::{FieldErrors, FormInput};
use crate::policy::ActingUser;
use crate::query::{ListQuery, Page};
use crate::widget::{LatestRecords, StatWidget};
/// Checks at compile time that `table` is `expected`. The derive uses it to check that the
/// `table` attribute names the same table as the Orbit model's `TABLE`.
pub const fn table_name_is(table: &str, expected: &str) -> bool {
let (a, b) = (table.as_bytes(), expected.as_bytes());
if a.len() != b.len() {
return false;
}
let mut i = 0;
while i < a.len() {
if a[i] != b[i] {
return false;
}
i += 1;
}
true
}
/// A table of records the panel can list, create, edit and delete.
///
/// Implement it by hand, or with `#[derive(IgniteResource)]`. The panel calls the query methods
/// with the app's [`Database`], and renders the forms from [`fields`](Self::fields) and the list
/// from [`columns`](Self::columns). The query methods are async, so each returns a future.
pub trait IgniteResource: Send + Sync + 'static {
/// One row, as the database returns it. It must serialize to JSON with
/// field names matching `fields()` and `columns()`.
type Record: Serialize + Send;
/// Validated form input for create and update.
type Input: Send;
/// URL segment: `"users"` mounts the resource at `/admin/users`.
const SLUG: &'static str;
/// Heading and navigation label: `"Users"`.
const TITLE: &'static str;
/// The name of one record, for headings such as "New User" (added in v1).
const SINGULAR: &'static str = "Record";
/// Rows per page on the list view.
const PER_PAGE: i64 = 20;
/// The label of the resource's navigation entry, as Filament's `$navigationLabel`. The
/// default, `None`, is the [`TITLE`](Self::TITLE).
const NAVIGATION_LABEL: Option<&'static str> = None;
/// The navigation group the resource is listed under, as Filament's `$navigationGroup`:
/// the group's name, shown as a heading in the sidebar. The panel's `navigation_groups`
/// orders the groups. The default, `None`, lists the resource before the groups, as
/// Filament lists ungrouped items first.
const NAVIGATION_GROUP: Option<&'static str> = None;
/// The resource's place in the navigation, as Filament's `$navigationSort`: a lower number
/// comes first. The default, `None`, comes before any number. In a group, resources with
/// the same place are listed by label; without a group, in the order they were added.
const NAVIGATION_SORT: Option<i32> = None;
/// The name of the resource's navigation icon, as Filament's `$navigationIcon`: a name
/// that says what the resource is, such as `"post"`, `"page"`, `"media"`, `"comment"`,
/// `"users"` or `"settings"`, or a name of the app's own, such as a custom post type's
/// `"book"`. It is not a CSS class: the active admin theme maps the name to its own icon
/// (the `ignite` theme to a Tabler icon, a WordPress-like theme to a Dashicon), and shows
/// its generic resource icon for a name it does not know. The default, `None`, shows the
/// theme's generic resource icon.
const NAVIGATION_ICON: Option<&'static str> = None;
/// Stat cards for the dashboard, in display order. The default is none.
/// Override it to show counts, such as the total number of rows.
fn widgets(db: &Database) -> impl Future<Output = Result<Vec<StatWidget>, Error>> + Send {
let _ = db;
async { Ok(Vec::new()) }
}
/// The dashboard table of the newest records, or `None` for no table (added in v1).
fn latest_records() -> Option<LatestRecords> {
None
}
/// Up to `limit` records, newest first (added in v1). The panel calls it with the
/// `limit` of [`latest_records`](Self::latest_records). The default is no records.
fn latest(
db: &Database,
limit: i64,
) -> impl Future<Output = Result<Vec<Self::Record>, Error>> + Send {
let _ = (db, limit);
async { Ok(Vec::new()) }
}
/// Form fields, in display order.
fn fields() -> &'static [Field];
/// Table columns, in display order. Each column's `key` is a field name.
fn columns() -> &'static [Column];
/// The conditions that show or hide the form's fields, and the live fields, as Filament's
/// `visible()`, `hidden()` and `live()` (see [`FieldConditions`]). The panel reads them
/// once, when it is built. The default is none: every field shows. `#[derive(IgniteResource)]`
/// writes it for `#[ignite(conditions = path)]`.
///
/// The panel removes a hidden field from the submitted form before it calls
/// [`validate`](Self::validate) or [`validate_update`](Self::validate_update), so a field
/// that may be hidden must be one `validate` accepts as missing, such as an `Option` field
/// of the derive: left out on update, it keeps its stored value.
fn conditions() -> FieldConditions {
FieldConditions::new()
}
/// The choices of a [`FieldKind::Lookup`](crate::FieldKind::Lookup) field, as
/// `(value, label)` pairs in display order (added in v1), and of every other field whose
/// kind [loads its options](crate::FieldKind::loads_options): a post object,
/// relationship, taxonomy or user field. The panel calls it when it
/// shows the form, to fill the select, and when it checks a submitted value. The
/// default is no choices. `#[derive(IgniteResource)]` writes it for `options = ...`
/// and `belongs_to = ...`.
fn options(
db: &Database,
field: &str,
) -> impl Future<Output = Result<Vec<(String, String)>, Error>> + Send {
let _ = (db, field);
async { Ok(Vec::new()) }
}
/// Returns one page of rows that match `query`.
fn list(
db: &Database,
query: &ListQuery,
) -> impl Future<Output = Result<Page<Self::Record>, Error>> + Send;
/// Returns the row with `id`, or `None` if there is no such row.
fn find(
db: &Database,
id: i64,
) -> impl Future<Output = Result<Option<Self::Record>, Error>> + Send;
/// Inserts a row from `input` and returns it.
fn create(
db: &Database,
input: Self::Input,
) -> impl Future<Output = Result<Self::Record, Error>> + Send;
/// Updates the row with `id` from `input` and returns it.
fn update(
db: &Database,
id: i64,
input: Self::Input,
) -> impl Future<Output = Result<Self::Record, Error>> + Send;
/// Deletes the row with `id` and returns the number of rows deleted.
fn delete(db: &Database, id: i64) -> impl Future<Output = Result<u64, Error>> + Send;
/// Turns the submitted form into `Input` for a create, or returns one message
/// per field. Call [`Field::check_required`] for required fields.
fn validate(form: &FormInput) -> Result<Self::Input, FieldErrors>;
/// Turns the submitted form into `Input` for an update. The default is
/// [`validate`](Self::validate). Override it when a field may be left blank
/// on edit, such as a password that keeps its stored value.
fn validate_update(form: &FormInput) -> Result<Self::Input, FieldErrors> {
Self::validate(form)
}
/// [`list`](Self::list) for the signed-in `user`: the panel's list calls it. Restrict the
/// rows to the user here, as a Filament resource does in `getEloquentQuery()`. The default
/// is [`list`](Self::list), for every user.
fn list_for(
db: &Database,
user: &ActingUser,
query: &ListQuery,
) -> impl Future<Output = Result<Page<Self::Record>, Error>> + Send {
let _ = user;
Self::list(db, query)
}
/// [`find`](Self::find) for the signed-in `user`: the panel finds the record of the edit
/// form, of its save and of the delete action with it. Return `None` for a row the user's
/// list leaves out, so the panel answers 404. The default is [`find`](Self::find).
fn find_for(
db: &Database,
user: &ActingUser,
id: i64,
) -> impl Future<Output = Result<Option<Self::Record>, Error>> + Send {
let _ = user;
Self::find(db, id)
}
/// [`create`](Self::create) for the signed-in `user`. The default is
/// [`create`](Self::create).
fn create_for(
db: &Database,
user: &ActingUser,
input: Self::Input,
) -> impl Future<Output = Result<Self::Record, Error>> + Send {
let _ = user;
Self::create(db, input)
}
/// [`update`](Self::update) for the signed-in `user`. The panel calls it only after
/// [`find_for`](Self::find_for) found the row and the [`Policy`](crate::Policy) allowed the
/// update. The default is [`update`](Self::update).
fn update_for(
db: &Database,
user: &ActingUser,
id: i64,
input: Self::Input,
) -> impl Future<Output = Result<Self::Record, Error>> + Send {
let _ = user;
Self::update(db, id, input)
}
/// [`delete`](Self::delete) for the signed-in `user`. The panel calls it only after
/// [`find_for`](Self::find_for) found the row and the [`Policy`](crate::Policy) allowed the
/// delete. The default is [`delete`](Self::delete).
fn delete_for(
db: &Database,
user: &ActingUser,
id: i64,
) -> impl Future<Output = Result<u64, Error>> + Send {
let _ = user;
Self::delete(db, id)
}
/// [`options`](Self::options) for the signed-in `user`: the choices the form shows and
/// checks a submitted value against. The default is [`options`](Self::options).
fn options_for(
db: &Database,
user: &ActingUser,
field: &str,
) -> impl Future<Output = Result<Vec<(String, String)>, Error>> + Send {
let _ = user;
Self::options(db, field)
}
}