Add links in footer to article and author history

This commit is contained in:
Magnus Hoff 2017-10-23 16:10:19 +02:00
parent d7d555d4d5
commit f097313e53
4 changed files with 26 additions and 11 deletions

View file

@ -13,6 +13,8 @@ use site::Layout;
use state::State; use state::State;
use web::{Resource, ResponseFuture}; use web::{Resource, ResponseFuture};
use super::changes_resource::QueryParameters;
pub struct ArticleResource { pub struct ArticleResource {
state: State, state: State,
article_id: i32, article_id: i32,
@ -26,17 +28,27 @@ impl ArticleResource {
} }
} }
pub fn last_updated(created: &DateTime<Local>, author: Option<&str>) -> String { pub fn last_updated(article_id: i32, created: &DateTime<Local>, author: Option<&str>) -> String {
struct Author<'a> {
author: &'a str,
history: String,
}
#[derive(BartDisplay)] #[derive(BartDisplay)]
#[template_string = "Last updated {{created}}{{#author}} by {{.}}{{/author}}"] #[template_string = "Last updated <a href=\"{{article_history}}\">{{created}}</a>{{#author}} by <a href=\"{{.history}}\">{{.author}}</a>{{/author}}"]
struct Template<'a> { struct Template<'a> {
created: &'a str, created: &'a str,
author: Option<&'a str>, article_history: &'a str,
author: Option<Author<'a>>,
} }
Template { Template {
created: &created.to_rfc2822(), created: &created.to_rfc2822(),
author article_history: &format!("_changes{}", QueryParameters::default().article_id(Some(article_id)).into_link()),
author: author.map(|author| Author {
author: &author,
history: format!("_changes{}", QueryParameters::default().author(Some(author.to_owned())).into_link()),
}),
}.to_string() }.to_string()
} }
@ -82,6 +94,7 @@ impl Resource for ArticleResource {
body: &Template { body: &Template {
revision: data.revision, revision: data.revision,
last_updated: Some(&last_updated( last_updated: Some(&last_updated(
data.article_id,
&Local.from_utc_datetime(&data.created), &Local.from_utc_datetime(&data.created),
data.author.as_ref().map(|x| &**x) data.author.as_ref().map(|x| &**x)
)), )),
@ -148,6 +161,7 @@ impl Resource for ArticleResource {
rendered: render_markdown(&updated.body), rendered: render_markdown(&updated.body),
}.to_string(), }.to_string(),
last_updated: &last_updated( last_updated: &last_updated(
updated.article_id,
&Local.from_utc_datetime(&updated.created), &Local.from_utc_datetime(&updated.created),
updated.author.as_ref().map(|x| &**x) updated.author.as_ref().map(|x| &**x)
), ),

View file

@ -26,7 +26,7 @@ pub struct ChangesLookup {
} }
#[derive(Serialize, Deserialize, Default)] #[derive(Serialize, Deserialize, Default)]
struct QueryParameters { pub struct QueryParameters {
after: Option<i32>, after: Option<i32>,
before: Option<i32>, before: Option<i32>,
@ -37,7 +37,7 @@ struct QueryParameters {
} }
impl QueryParameters { impl QueryParameters {
fn pagination(self, pagination: Pagination<i32>) -> Self { pub fn pagination(self, pagination: Pagination<i32>) -> Self {
Self { Self {
after: if let Pagination::After(x) = pagination { Some(x) } else { None }, after: if let Pagination::After(x) = pagination { Some(x) } else { None },
before: if let Pagination::Before(x) = pagination { Some(x) } else { None }, before: if let Pagination::Before(x) = pagination { Some(x) } else { None },
@ -45,22 +45,22 @@ impl QueryParameters {
} }
} }
fn article_id(self, article_id: Option<i32>) -> Self { pub fn article_id(self, article_id: Option<i32>) -> Self {
Self { article_id, ..self } Self { article_id, ..self }
} }
fn author(self, author: Option<String>) -> Self { pub fn author(self, author: Option<String>) -> Self {
Self { author, ..self } Self { author, ..self }
} }
fn limit(self, limit: i32) -> Self { pub fn limit(self, limit: i32) -> Self {
Self { Self {
limit: if limit != DEFAULT_LIMIT { Some(limit) } else { None }, limit: if limit != DEFAULT_LIMIT { Some(limit) } else { None },
..self ..self
} }
} }
fn into_link(self) -> String { pub fn into_link(self) -> String {
let args = serde_urlencoded::to_string(self).expect("Serializing to String cannot fail"); let args = serde_urlencoded::to_string(self).expect("Serializing to String cannot fail");
if args.len() > 0 { if args.len() > 0 {
format!("?{}", args) format!("?{}", args)

View file

@ -150,6 +150,7 @@ impl Resource for NewArticleResource {
rendered: render_markdown(&updated.body), rendered: render_markdown(&updated.body),
}.to_string(), }.to_string(),
last_updated: &super::article_resource::last_updated( last_updated: &super::article_resource::last_updated(
updated.article_id,
&Local.from_utc_datetime(&updated.created), &Local.from_utc_datetime(&updated.created),
updated.author.as_ref().map(|x| &**x) updated.author.as_ref().map(|x| &**x)
), ),

View file

@ -33,7 +33,7 @@
<footer> <footer>
<ul class="dense" <ul class="dense"
><li class="last-updated {{^last_updated}}missing{{/last_updated}}">{{#last_updated}}{{.}}{{/last_updated}}</li ><li class="last-updated {{^last_updated}}missing{{/last_updated}}">{{#last_updated}}{{{.}}}{{/last_updated}}</li
><li><a id="openEditor" href="?edit">Edit</a></li ><li><a id="openEditor" href="?edit">Edit</a></li
></ul> ></ul>
{{>footer/items.html}} {{>footer/items.html}}