mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-24 04:27:59 +01:00
35 lines
964 B
Elixir
35 lines
964 B
Elixir
|
defmodule PhilomenaWeb.Page.HistoryController do
|
||
|
use PhilomenaWeb, :controller
|
||
|
|
||
|
alias Philomena.StaticPages.StaticPage
|
||
|
alias Philomena.StaticPages.Version
|
||
|
alias Philomena.Repo
|
||
|
import Ecto.Query
|
||
|
|
||
|
plug :load_resource, model: StaticPage, id_field: "slug", persisted: true
|
||
|
|
||
|
def index(conn, _params) do
|
||
|
page = conn.assigns.static_page
|
||
|
|
||
|
versions =
|
||
|
Version
|
||
|
|> where(static_page_id: ^page.id)
|
||
|
|> preload(:user)
|
||
|
|> order_by(desc: :created_at)
|
||
|
|> Repo.all()
|
||
|
|> generate_differences(page.body)
|
||
|
|
||
|
render(conn, "index.html", layout_class: "layout--wide", versions: versions)
|
||
|
end
|
||
|
|
||
|
defp generate_differences(pages, current_body) do
|
||
|
Enum.map_reduce(pages, current_body, fn page, previous_body ->
|
||
|
difference = List.myers_difference(split(previous_body), split(page.body))
|
||
|
|
||
|
%{page | difference: difference}
|
||
|
end)
|
||
|
end
|
||
|
|
||
|
defp split(nil), do: ""
|
||
|
defp split(body), do: String.split(body, "\n")
|
||
|
end
|