Skip to content

Commit 9bd5317

Browse files
paradox460mhanberg
authored andcommitted
Prune old posts when in server mode
1 parent 7cca6ba commit 9bd5317

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

lib/tableau/extensions/post_extension.ex

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,9 @@ defmodule Tableau.PostExtension do
9090

9191
posts =
9292
for post <- apply(Tableau.PostExtension.Posts, :posts, []) do
93-
post_id =
94-
post.id ||
95-
post.title |> String.replace(~r/[^[:alnum:]]+/u, "_") |> Macro.camelize() |> then(&("Post." <> &1))
96-
9793
{:module, _module, _binary, _term} =
9894
Module.create(
99-
Module.concat([post_id]),
95+
Module.concat([post.id]),
10096
quote do
10197
use Tableau.Page, unquote(Macro.escape(Keyword.new(post)))
10298

@@ -111,6 +107,25 @@ defmodule Tableau.PostExtension do
111107
post
112108
end
113109

110+
generated_posts = Enum.map(posts, & &1.id)
111+
112+
loaded_posts =
113+
Enum.reduce(:code.all_loaded(), [], fn {mod, _}, acc ->
114+
mod
115+
|> to_string()
116+
|> String.split(".")
117+
|> then(fn
118+
[_, "AutogenPostID" | _] -> mod |> Macro.to_string() |> then(&[&1 | acc])
119+
_ -> acc
120+
end)
121+
end)
122+
123+
for post <- loaded_posts, post not in generated_posts do
124+
post = Module.concat([post])
125+
:code.purge(post)
126+
:code.delete(post)
127+
end
128+
114129
{:ok, Map.put(token, :posts, posts)}
115130
end,
116131
[Node.self()],

lib/tableau/extensions/post_extension/posts/post.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ defmodule Tableau.PostExtension.Posts.Post do
1818
_ -> nil
1919
end
2020
end)
21+
|> maybe_calculate_id()
2122
|> Map.put(
2223
:date,
2324
DateTime.from_naive!(
@@ -67,4 +68,15 @@ defmodule Tableau.PostExtension.Posts.Post do
6768
|> String.replace(~r/[^[:alnum:]\/\-]/, "")
6869
|> String.downcase()
6970
end
71+
72+
defp maybe_calculate_id(%{id: _} = attrs), do: attrs
73+
74+
defp maybe_calculate_id(attrs) do
75+
attrs.title
76+
|> String.downcase()
77+
|> String.replace(~r/[^[:alnum:]]+/u, "_")
78+
|> Macro.camelize()
79+
|> then(&"AutogenPostID.#{&1}")
80+
|> then(&Map.put(attrs, :id, &1))
81+
end
7082
end

0 commit comments

Comments
 (0)