Skip to content

Commit

Permalink
Add OID extractor for Tera templates
Browse files Browse the repository at this point in the history
  • Loading branch information
corybuecker committed Oct 4, 2024
1 parent eb02487 commit badc288
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/authenticated/accounts/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub async fn action(
)
.into_response())
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
19 changes: 19 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ impl FromRef<SharedState> for Key {
}
}

pub fn extract_id() -> impl tera::Filter {
return |value: &tera::Value,
args: &HashMap<String, tera::Value>|
-> tera::Result<tera::Value> {
debug!("{}", value);
debug!("{:#?}", args);

let id = value.get("_id");

match id {
None => Err(tera::Error::msg("could not find id field".to_string())),
Some(id) => Ok(tera::Value::String(
id["$oid"].to_string().replace("\"", ""),
)),
}
};
}

pub fn digest_asset() -> impl tera::Function {
let key = SystemTime::now();
let key = key
Expand Down Expand Up @@ -168,6 +186,7 @@ async fn main() {

let mut tera = Tera::new("src/templates/**/*.html").expect("cannot initialize Tera");
tera.register_function("digest_asset", digest_asset());
tera.register_filter("oid", extract_id());

let mongo = mongo_client().await.expect("cannot create Mongo client");

Expand Down
2 changes: 1 addition & 1 deletion src/templates/accounts/delete.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<turbo-stream action="replace" target="confirmation-modal">
<template></template>
</turbo-stream>
<turbo-stream action="remove" target="account_{{ account._id["$oid"] }}"></turbo-stream>
<turbo-stream action="remove" target="account_{{ account | oid }}"></turbo-stream>
2 changes: 1 addition & 1 deletion src/templates/accounts/delete/confirm.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "delete_confirmation.html" %}
{% block prompt %}Are you sure you want to delete the "{{ account.name }}" account?{% endblock %}
{% block entity %}account{% endblock %}
{% block action %}/accounts/{{ account._id["$oid"] }}{% endblock %}
{% block action %}/accounts/{{ account | oid }}{% endblock %}
6 changes: 3 additions & 3 deletions src/templates/accounts/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<tbody>
{% for account in accounts %}
<tr class="odd:bg-white even:bg-gray-50 border-b"
id="account_{{ account._id["$oid"] }}">
id="account_{{ account | oid }}">
<th scope="row" class="px-6 @sm:px-3 py-2 font-medium text-gray-900">{{ account.name }}</th>
<td class="px-6 @sm:px-3 py-2"
data-controller="formatter"
Expand All @@ -33,10 +33,10 @@
</td>
<td class="px-6 @sm:px-3 py-2">
<div class="flex @lg:flex-row flex-col items-center gap-4">
<a href="/accounts/{{ account._id["$oid"] }}"
<a href="/accounts/{{ account | oid }}"
class="font-medium text-blue-600 hover:underline">Edit</a>
<a data-turbo-frame="confirmation-modal"
href="/accounts/{{ account._id["$oid"] }}/delete">Delete</a>
href="/accounts/{{ account | oid }}/delete">Delete</a>
</div>
</td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion src/templates/envelopes/delete.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<turbo-stream action="replace" target="confirmation-modal">
<template></template>
</turbo-stream>
<turbo-stream action="remove" target="envelope_{{ envelope._id["$oid"] }}"></turbo-stream>
<turbo-stream action="remove" target="envelope_{{ envelope | oid }}"></turbo-stream>
2 changes: 1 addition & 1 deletion src/templates/envelopes/delete/confirm.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "delete_confirmation.html" %}
{% block prompt %}Are you sure you want to delete the "{{ envelope.name }}" envelope?{% endblock %}
{% block entity %}envelope{% endblock %}
{% block action %}/envelopes/{{ envelope._id["$oid"] }}{% endblock %}
{% block action %}/envelopes/{{ envelope | oid }}{% endblock %}
6 changes: 3 additions & 3 deletions src/templates/envelopes/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<tbody>
{% for envelope in envelopes %}
<tr class="odd:bg-white even:bg-gray-50 border-b"
id="envelope_{{ envelope._id["$oid"] }}">
id="envelope_{{ envelope | oid }}">
<th scope="row"
class="px-6 @sm:px-3 py-2 font-medium text-gray-900 whitespace-nowrap">
{{ envelope.name }}
Expand All @@ -28,10 +28,10 @@
data-formatter-currency-value="{{ envelope.amount }}">{{ envelope.amount }}</td>
<td class="px-6 @sm:px-3 py-2">
<div class="flex @lg:flex-row flex-col items-center gap-4">
<a href="/envelopes/{{ envelope._id["$oid"] }}"
<a href="/envelopes/{{ envelope | oid }}"
class="font-medium text-blue-600 hover:underline">Edit</a>
<a data-turbo-frame="confirmation-modal"
href="/envelopes/{{ envelope._id["$oid"] }}/delete">Delete</a>
href="/envelopes/{{ envelope | oid }}/delete">Delete</a>
</div>
</td>
</tr>
Expand Down
6 changes: 3 additions & 3 deletions src/templates/goals/_table.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<tbody>
{% for goal in goals %}
<tr class="odd:bg-white even:bg-gray-50 border-b"
id="goal_{{ goal._id["$oid"] }}">
id="goal_{{ goal | oid }}">
<th scope="row" class="px-6 @sm:px-3 py-2 font-medium text-gray-900">{{ goal.name }}</th>
<td class="px-6 @sm:px-3 py-2"
data-controller="formatter"
Expand All @@ -53,11 +53,11 @@
{% endif %}
<td class="px-6 @sm:px-3 py-2">
<div class="flex @lg:flex-row flex-col items-center gap-4">
<a href="/goals/{{ goal._id["$oid"] }}"
<a href="/goals/{{ goal | oid }}"
data-turbo-frame="_top"
class="font-medium text-blue-600 hover:underline">Edit</a>
<a data-turbo-frame="confirmation-modal"
href="/goals/{{ goal._id["$oid"] }}/delete">Delete</a>
href="/goals/{{ goal | oid }}/delete">Delete</a>
</div>
</td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion src/templates/goals/delete.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<turbo-stream action="replace" target="confirmation-modal">
<template></template>
</turbo-stream>
<turbo-stream action="remove" target="goal_{{ goal._id["$oid"] }}"></turbo-stream>
<turbo-stream action="remove" target="goal_{{ goal | oid }}"></turbo-stream>
2 changes: 1 addition & 1 deletion src/templates/goals/delete/confirm.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "delete_confirmation.html" %}
{% block prompt %}Are you sure you want to delete the "{{ goal.name }}" goal?{% endblock %}
{% block entity %}goal{% endblock %}
{% block action %}/goals/{{ goal._id["$oid"] }}{% endblock %}
{% block action %}/goals/{{ goal | oid }}{% endblock %}
2 changes: 2 additions & 0 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#[cfg(test)]
use crate::extract_id;
use crate::{authenticated::UserExtension, digest_asset, mongo_client, Broker, SharedState};
use axum::Extension;
use axum_extra::extract::cookie::Key;
Expand All @@ -10,6 +11,7 @@ pub async fn state_for_tests() -> SharedState {
let mut tera = tera::Tera::new("src/templates/**/*.html").unwrap();

tera.register_function("digest_asset", digest_asset());
tera.register_filter("oid", extract_id());

SharedState {
mongo: client,
Expand Down

0 comments on commit badc288

Please sign in to comment.