From 82c7533b4c2615cb5825060624d8eedaee41fd4d Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Sun, 21 Jul 2024 15:09:57 +0200 Subject: [PATCH] Refactor ItemName::new to take strings directly This loosens the dependency on always querying full Items and ItemClasses. --- src/frontend/item.rs | 12 ++++++------ src/frontend/item_class.rs | 2 +- src/frontend/labels.rs | 2 +- src/frontend/templates/datalist.rs | 5 ++++- src/frontend/templates/helpers.rs | 10 +++++----- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/frontend/item.rs b/src/frontend/item.rs index d031581..1c39fbd 100644 --- a/src/frontend/item.rs +++ b/src/frontend/item.rs @@ -66,7 +66,7 @@ async fn show( let item_class = item_classes.get(&item.class).unwrap(); - let item_name = ItemName::new(&item, item_class); + let item_name = ItemName::new(item.name.as_ref(), &item_class.name); let mut title = item_name.to_string(); title.push_str(" – Item Details"); @@ -133,7 +133,7 @@ async fn show( @if let Some(original_packaging) = original_packaging { a href={ "/item/" (original_packaging.id) } - { (ItemName::new(&original_packaging, &item_classes.get(&original_packaging.class).unwrap())) } + { (ItemName::new(original_packaging.name.as_ref(), &item_classes.get(&original_packaging.class).unwrap().name)) } } @else { "-" } @@ -160,7 +160,7 @@ async fn show( ul { @for child in children { li { - a href={ "/item/" (child.id) } { (ItemName::new(&child, &item_classes.get(&child.class).unwrap())) } + a href={ "/item/" (child.id) } { (ItemName::new(child.name.as_ref(), &item_classes.get(&child.class).unwrap().name)) } } } } @@ -172,7 +172,7 @@ async fn show( ul { @for item in original_packaging_of { li { - a href={ "/item/" (item.id) } { (ItemName::new(&item, &item_classes.get(&item.class).unwrap())) } + a href={ "/item/" (item.id) } { (ItemName::new(item.name.as_ref(), &item_classes.get(&item.class).unwrap().name)) } } } } @@ -246,7 +246,7 @@ async fn list(pool: web::Data, user: Identity) -> actix_web::Result, user: Identity) -> actix_web::Result Result { .iter() .map(|i| DatalistOption { value: i.id.to_string(), - text: Box::new(ItemName::new(i, item_classes.get(&i.class).unwrap())), + text: Box::new(ItemName::new( + i.name.as_ref(), + &item_classes.get(&i.class).unwrap().name, + )), }) .collect(), }) diff --git a/src/frontend/templates/helpers.rs b/src/frontend/templates/helpers.rs index 5ac66a7..ee936b1 100644 --- a/src/frontend/templates/helpers.rs +++ b/src/frontend/templates/helpers.rs @@ -93,11 +93,11 @@ pub enum ItemName { } impl ItemName { - pub fn new(item: &Item, class: &ItemClass) -> Self { - if let Some(ref name) = item.name { + pub fn new(item_name: Option<&String>, class_name: &String) -> Self { + if let Some(ref name) = item_name { Self::Item(name.to_string()) } else { - Self::Class(class.name.clone()) + Self::Class(class_name.to_string()) } } } @@ -236,10 +236,10 @@ pub fn parents_breadcrumb( }; @for parent in parents { li .breadcrumb-item { - a href={ "/item/" (parent.id) } { (ItemName::new(parent, parents_item_classes.get(&parent.class).unwrap()) )} + a href={ "/item/" (parent.id) } { (ItemName::new(parent.name.as_ref(), &parents_item_classes.get(&parent.class).unwrap().name)) } } } - li .breadcrumb-item.active { (ItemName::new(item, item_class)) } + li .breadcrumb-item.active { (ItemName::new(item.name.as_ref(), &item_class.name)) } } } }