armature-admin 0.3.1

Auto-generated CRUD admin dashboard for Armature framework
Documentation
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
//! View structures for admin pages

use crate::{
    ListParams,
    config::AdminConfig,
    data::value_to_plain_string,
    field::{FieldDefinition, FieldType},
    model::ModelDefinition,
    ui::{Breadcrumb, CellType, FilterDef, Pagination, TableCell, TableColumn, TableRow},
};
// Single shared HTML-escape helper lives in `render`; alias it here so the
// existing call sites keep reading naturally.
use crate::render::escape as html_escape;
use serde::{Deserialize, Serialize};

/// List view for a model
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ListView {
    /// Model name
    pub model_name: String,
    /// Verbose name (plural)
    pub verbose_name: String,
    /// Page title
    pub title: String,
    /// Breadcrumbs
    pub breadcrumbs: Vec<Breadcrumb>,
    /// Table columns
    pub columns: Vec<TableColumn>,
    /// Table rows
    pub rows: Vec<TableRow>,
    /// Pagination
    pub pagination: Pagination,
    /// Filters
    pub filters: Vec<FilterDef>,
    /// Current search query
    pub search_query: Option<String>,
    /// Can add new records?
    pub can_add: bool,
    /// Can delete records?
    pub can_delete: bool,
    /// Can export?
    pub can_export: bool,
    /// Add URL
    pub add_url: String,
    /// Search placeholder
    pub search_placeholder: String,
    /// Has search enabled?
    pub has_search: bool,
    /// Has filters?
    pub has_filters: bool,
}

impl ListView {
    /// Create a new list view.
    ///
    /// `per_page` is the already-resolved page size (see
    /// [`ListParams::resolve_per_page`]); rows are filled in separately from a
    /// [`crate::data::DataSource`] via [`ListView::with_json_rows`].
    pub fn new(model: &ModelDefinition, params: ListParams, per_page: usize) -> Self {
        let columns = model
            .display_fields()
            .iter()
            .map(|f| TableColumn {
                field: f.name.clone(),
                label: f.label.clone(),
                sortable: f.sortable,
                sort_direction: if params.sort.as_deref() == Some(&f.name) {
                    Some(match params.order {
                        Some(crate::SortOrder::Desc) => crate::ui::SortDirection::Desc,
                        _ => crate::ui::SortDirection::Asc,
                    })
                } else {
                    None
                },
                css_class: None,
                width: None,
            })
            .collect();

        let filters = model
            .filterable_fields()
            .iter()
            .map(|f| FilterDef {
                field: f.name.clone(),
                label: f.label.clone(),
                filter_type: match f.field_type {
                    crate::field::FieldType::Boolean => crate::ui::FilterType::Boolean,
                    crate::field::FieldType::Enum => crate::ui::FilterType::Select,
                    crate::field::FieldType::Date | crate::field::FieldType::DateTime => {
                        crate::ui::FilterType::DateRange
                    }
                    _ => crate::ui::FilterType::Text,
                },
                choices: f
                    .choices
                    .as_ref()
                    .map(|choices| {
                        choices
                            .iter()
                            .map(|c| crate::ui::FilterChoice {
                                value: c.value.clone(),
                                label: c.label.clone(),
                                count: None,
                            })
                            .collect()
                    })
                    .unwrap_or_default(),
                current: params.filters.get(&f.name).cloned(),
            })
            .collect();

        Self {
            model_name: model.name.clone(),
            verbose_name: model.verbose_name.clone(),
            title: model.verbose_name.clone(),
            breadcrumbs: vec![
                Breadcrumb::new("Dashboard").url("/admin"),
                Breadcrumb::new(&model.verbose_name),
            ],
            columns,
            rows: Vec::new(), // Populated from the data source via `with_json_rows`.
            pagination: Pagination::new(params.page(), per_page.max(1), 0),
            filters,
            search_query: params.search,
            can_add: model.can_add,
            can_delete: model.can_delete,
            can_export: model.can_export,
            add_url: format!("/admin/{}/add", model.name),
            search_placeholder: format!("Search {}...", model.search_fields.join(", ")),
            has_search: !model.search_fields.is_empty(),
            has_filters: !model.list_filter.is_empty(),
        }
    }

    /// Set pre-built rows (from a data source), recomputing pagination.
    pub fn with_rows(mut self, rows: Vec<TableRow>, total: usize) -> Self {
        self.rows = rows;
        self.pagination = Pagination::new(self.pagination.page, self.pagination.per_page, total);
        self
    }

    /// Populate rows from raw JSON records returned by a
    /// [`crate::data::DataSource`], rendering each display column's cell and
    /// honoring the configured date/datetime formats.
    pub fn with_json_rows(
        self,
        model: &ModelDefinition,
        config: &AdminConfig,
        records: &[serde_json::Value],
        total: usize,
    ) -> Self {
        let rows = records
            .iter()
            .map(|record| build_table_row(model, config, record))
            .collect();
        self.with_rows(rows, total)
    }
}

/// Build a [`TableRow`] for one record, one cell per display field.
fn build_table_row(
    model: &ModelDefinition,
    config: &AdminConfig,
    record: &serde_json::Value,
) -> TableRow {
    let id = record
        .get(&model.primary_key)
        .map(value_to_plain_string)
        .unwrap_or_default();

    let cells = model
        .display_fields()
        .iter()
        .map(|field| {
            let value = record
                .get(&field.name)
                .cloned()
                .unwrap_or(serde_json::Value::Null);
            let (rendered, cell_type) = render_cell(field, config, &value);
            TableCell {
                field: field.name.clone(),
                value,
                rendered,
                cell_type,
            }
        })
        .collect();

    TableRow {
        id,
        cells,
        selected: false,
        css_class: None,
    }
}

/// Render a single cell's HTML + classify its cell type, applying the
/// configured date/datetime formats for temporal fields.
fn render_cell(
    field: &FieldDefinition,
    config: &AdminConfig,
    value: &serde_json::Value,
) -> (String, CellType) {
    match field.field_type {
        FieldType::Date => {
            let raw = value_to_plain_string(value);
            (html_escape(&config.format_date(&raw)), CellType::Date)
        }
        FieldType::DateTime => {
            let raw = value_to_plain_string(value);
            (
                html_escape(&config.format_datetime(&raw)),
                CellType::DateTime,
            )
        }
        FieldType::Boolean => (render_value(value), CellType::Boolean),
        FieldType::Integer | FieldType::BigInteger | FieldType::Float | FieldType::Decimal => {
            (render_value(value), CellType::Number)
        }
        FieldType::Email => (html_escape(&value_to_plain_string(value)), CellType::Email),
        FieldType::Url => (html_escape(&value_to_plain_string(value)), CellType::Url),
        _ => (render_value(value), CellType::Text),
    }
}

/// Detail view for a model record
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DetailView {
    /// Model name
    pub model_name: String,
    /// Verbose name (singular)
    pub verbose_name: String,
    /// Page title
    pub title: String,
    /// Breadcrumbs
    pub breadcrumbs: Vec<Breadcrumb>,
    /// Record ID
    pub id: String,
    /// Field values
    pub fields: Vec<FieldValue>,
    /// Fieldsets
    pub fieldsets: Vec<ViewFieldset>,
    /// Can edit?
    pub can_edit: bool,
    /// Can delete?
    pub can_delete: bool,
    /// Edit URL
    pub edit_url: String,
    /// Delete URL
    pub delete_url: String,
    /// List URL
    pub list_url: String,
    /// Inlines (related data)
    pub inlines: Vec<InlineView>,
}

impl DetailView {
    /// Create a new detail view
    pub fn new(model: &ModelDefinition, id: String) -> Self {
        Self {
            model_name: model.name.clone(),
            verbose_name: model.verbose_name_singular.clone(),
            title: format!("{} #{}", model.verbose_name_singular, id),
            breadcrumbs: vec![
                Breadcrumb::new("Dashboard").url("/admin"),
                Breadcrumb::new(&model.verbose_name).url(format!("/admin/{}", model.name)),
                Breadcrumb::new(&id),
            ],
            id: id.clone(),
            fields: Vec::new(), // Would be populated from database
            fieldsets: Vec::new(),
            can_edit: model.can_edit,
            can_delete: model.can_delete,
            edit_url: format!("/admin/{}/{}/edit", model.name, id),
            delete_url: format!("/admin/{}/{}/delete", model.name, id),
            list_url: format!("/admin/{}", model.name),
            inlines: Vec::new(),
        }
    }

    /// Set field values
    pub fn with_data(mut self, data: serde_json::Value) -> Self {
        if let Some(obj) = data.as_object() {
            self.fields = obj
                .iter()
                .map(|(k, v)| FieldValue {
                    name: k.clone(),
                    label: k.replace('_', " "),
                    value: v.clone(),
                    rendered: render_value(v),
                    readonly: false,
                })
                .collect();
        }
        self
    }
}

/// Create view for adding a new record
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateView {
    /// Model name
    pub model_name: String,
    /// Verbose name (singular)
    pub verbose_name: String,
    /// Page title
    pub title: String,
    /// Breadcrumbs
    pub breadcrumbs: Vec<Breadcrumb>,
    /// Form fields
    pub fields: Vec<FormField>,
    /// Fieldsets
    pub fieldsets: Vec<ViewFieldset>,
    /// Submit URL
    pub submit_url: String,
    /// Cancel URL
    pub cancel_url: String,
    /// Inlines
    pub inlines: Vec<InlineView>,
}

impl CreateView {
    /// Create a new create view
    pub fn new(model: &ModelDefinition) -> Self {
        let fields = model
            .form_fields()
            .iter()
            .map(|f| FormField::from_definition(f))
            .collect();

        Self {
            model_name: model.name.clone(),
            verbose_name: model.verbose_name_singular.clone(),
            title: format!("Add {}", model.verbose_name_singular),
            breadcrumbs: vec![
                Breadcrumb::new("Dashboard").url("/admin"),
                Breadcrumb::new(&model.verbose_name).url(format!("/admin/{}", model.name)),
                Breadcrumb::new("Add"),
            ],
            fields,
            fieldsets: Vec::new(),
            submit_url: format!("/admin/{}/add", model.name),
            cancel_url: format!("/admin/{}", model.name),
            inlines: Vec::new(),
        }
    }
}

/// Edit view for modifying a record
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EditView {
    /// Model name
    pub model_name: String,
    /// Verbose name
    pub verbose_name: String,
    /// Page title
    pub title: String,
    /// Breadcrumbs
    pub breadcrumbs: Vec<Breadcrumb>,
    /// Record ID
    pub id: String,
    /// Form fields
    pub fields: Vec<FormField>,
    /// Fieldsets
    pub fieldsets: Vec<ViewFieldset>,
    /// Submit URL
    pub submit_url: String,
    /// Cancel URL
    pub cancel_url: String,
    /// Delete URL
    pub delete_url: String,
    /// Can delete?
    pub can_delete: bool,
    /// Inlines
    pub inlines: Vec<InlineView>,
}

impl EditView {
    /// Create a new edit view (form fields default to empty values).
    pub fn new(model: &ModelDefinition, id: String) -> Self {
        let fields = model
            .form_fields()
            .iter()
            .map(|f| FormField::from_definition(f))
            .collect();

        Self {
            model_name: model.name.clone(),
            verbose_name: model.verbose_name_singular.clone(),
            title: format!("Edit {} #{}", model.verbose_name_singular, id),
            breadcrumbs: vec![
                Breadcrumb::new("Dashboard").url("/admin"),
                Breadcrumb::new(&model.verbose_name).url(format!("/admin/{}", model.name)),
                Breadcrumb::new(&id).url(format!("/admin/{}/{}", model.name, id)),
                Breadcrumb::new("Edit"),
            ],
            id: id.clone(),
            fields,
            fieldsets: Vec::new(),
            submit_url: format!("/admin/{}/{}/edit", model.name, id),
            cancel_url: format!("/admin/{}/{}", model.name, id),
            delete_url: format!("/admin/{}/{}/delete", model.name, id),
            can_delete: model.can_delete,
            inlines: Vec::new(),
        }
    }

    /// Populate the form fields with the current record's values.
    pub fn with_data(mut self, data: serde_json::Value) -> Self {
        if let Some(obj) = data.as_object() {
            for field in &mut self.fields {
                if let Some(v) = obj.get(&field.name) {
                    field.value = v.clone();
                }
            }
        }
        self
    }
}

/// Field value for display
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FieldValue {
    /// Field name
    pub name: String,
    /// Display label
    pub label: String,
    /// Raw value
    pub value: serde_json::Value,
    /// Rendered HTML
    pub rendered: String,
    /// Is readonly?
    pub readonly: bool,
}

/// Form field for editing
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FormField {
    /// Field name
    pub name: String,
    /// Display label
    pub label: String,
    /// Widget type
    pub widget: String,
    /// Current value
    pub value: serde_json::Value,
    /// Is required?
    pub required: bool,
    /// Is readonly?
    pub readonly: bool,
    /// Help text
    pub help_text: Option<String>,
    /// Placeholder
    pub placeholder: Option<String>,
    /// Choices (for select)
    pub choices: Option<Vec<(String, String)>>,
    /// Validation errors
    pub errors: Vec<String>,
    /// HTML attributes
    pub attrs: std::collections::HashMap<String, String>,
}

impl FormField {
    /// Create from field definition
    pub fn from_definition(field: &FieldDefinition) -> Self {
        let mut attrs = std::collections::HashMap::new();

        if let Some(max_len) = field.max_length {
            attrs.insert("maxlength".to_string(), max_len.to_string());
        }
        if let Some(min) = field.min_value {
            attrs.insert("min".to_string(), min.to_string());
        }
        if let Some(max) = field.max_value {
            attrs.insert("max".to_string(), max.to_string());
        }

        Self {
            name: field.name.clone(),
            label: field.label.clone(),
            widget: format!("{:?}", field.widget).to_lowercase(),
            value: serde_json::Value::Null,
            required: field.required,
            readonly: field.readonly,
            help_text: field.help_text.clone(),
            placeholder: field.placeholder.clone(),
            choices: field.choices.as_ref().map(|c| {
                c.iter()
                    .map(|ch| (ch.value.clone(), ch.label.clone()))
                    .collect()
            }),
            errors: Vec::new(),
            attrs,
        }
    }

    /// Set value
    pub fn with_value(mut self, value: serde_json::Value) -> Self {
        self.value = value;
        self
    }

    /// Add error
    pub fn add_error(&mut self, error: impl Into<String>) {
        self.errors.push(error.into());
    }
}

/// Fieldset for organizing form fields
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ViewFieldset {
    /// Fieldset name
    pub name: Option<String>,
    /// Description
    pub description: Option<String>,
    /// Fields in this fieldset
    pub fields: Vec<String>,
    /// Is collapsible?
    pub collapsible: bool,
    /// Is collapsed?
    pub collapsed: bool,
}

/// Inline view for related data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InlineView {
    /// Model name
    pub model_name: String,
    /// Verbose name
    pub verbose_name: String,
    /// Rows
    pub rows: Vec<InlineRow>,
    /// Extra empty rows
    pub extra: usize,
    /// Can delete?
    pub can_delete: bool,
    /// Fields to display
    pub fields: Vec<String>,
}

/// Inline row
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InlineRow {
    /// Row ID (if existing)
    pub id: Option<String>,
    /// Field values
    pub fields: Vec<FormField>,
    /// Is new?
    pub is_new: bool,
    /// Delete marker
    pub delete: bool,
}

/// Render a value for display
fn render_value(value: &serde_json::Value) -> String {
    match value {
        serde_json::Value::Null => "".to_string(),
        serde_json::Value::Bool(b) => {
            if *b {
                r#"<span class="badge badge-success">Yes</span>"#.to_string()
            } else {
                r#"<span class="badge badge-error">No</span>"#.to_string()
            }
        }
        serde_json::Value::Number(n) => n.to_string(),
        serde_json::Value::String(s) => html_escape(s),
        serde_json::Value::Array(arr) => {
            format!("[{} items]", arr.len())
        }
        serde_json::Value::Object(_) => "[Object]".to_string(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::field::FieldType;

    #[test]
    fn test_create_list_view() {
        let model = ModelDefinition::builder("user")
            .id_field()
            .field(FieldDefinition::new("name", FieldType::String).searchable())
            .field(FieldDefinition::new("email", FieldType::Email))
            .list_display(["id", "name", "email"])
            .search_fields(["name", "email"])
            .build();

        let view = ListView::new(&model, ListParams::default(), 25);

        assert_eq!(view.model_name, "user");
        assert_eq!(view.columns.len(), 3);
        assert!(view.has_search);
        assert_eq!(view.pagination.per_page, 25);
    }

    #[test]
    fn test_list_view_json_rows() {
        let model = ModelDefinition::builder("user")
            .id_field()
            .field(FieldDefinition::new("name", FieldType::String))
            .list_display(["id", "name"])
            .build();

        let records = vec![
            serde_json::json!({ "id": 1, "name": "Alice" }),
            serde_json::json!({ "id": 2, "name": "Bob" }),
        ];
        let view = ListView::new(&model, ListParams::default(), 25).with_json_rows(
            &model,
            &AdminConfig::default(),
            &records,
            2,
        );

        assert_eq!(view.rows.len(), 2);
        assert_eq!(view.rows[0].id, "1");
        assert_eq!(view.rows[0].cells[1].rendered, "Alice");
        assert_eq!(view.pagination.total_items, 2);
    }

    #[test]
    fn test_edit_view_with_data() {
        let model = ModelDefinition::builder("user")
            .id_field()
            .field(FieldDefinition::new("name", FieldType::String))
            .build();

        let view = EditView::new(&model, "5".to_string())
            .with_data(serde_json::json!({ "name": "Carol" }));

        assert_eq!(view.id, "5");
        let name = view.fields.iter().find(|f| f.name == "name").unwrap();
        assert_eq!(name.value, serde_json::json!("Carol"));
    }

    #[test]
    fn test_render_value() {
        assert_eq!(render_value(&serde_json::Value::Null), "");
        assert!(render_value(&serde_json::Value::Bool(true)).contains("Yes"));
        assert_eq!(render_value(&serde_json::json!(42)), "42");
        assert_eq!(render_value(&serde_json::json!("test")), "test");
    }

    #[test]
    fn test_html_escape() {
        assert_eq!(html_escape("<script>"), "&lt;script&gt;");
        assert_eq!(html_escape("a & b"), "a &amp; b");
    }
}