static page versions

This commit is contained in:
byte[] 2019-12-06 12:59:17 -05:00
parent 2d9b85d686
commit b56485f13a
4 changed files with 100 additions and 0 deletions

View file

@ -0,0 +1,27 @@
defmodule Philomena.StaticPages.Version do
use Ecto.Schema
import Ecto.Changeset
alias Philomena.StaticPages.StaticPage
alias Philomena.Users.User
schema "static_page_versions" do
belongs_to :static_page, StaticPage
belongs_to :user, User
field :title, :string
field :slug, :string
field :body, :string
field :difference, :any, virtual: true
timestamps(inserted_at: :created_at)
end
@doc false
def changeset(version, attrs) do
version
|> cast(attrs, [])
|> validate_required([])
end
end

View file

@ -0,0 +1,35 @@
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

View file

@ -0,0 +1,35 @@
h1
' Revision history for
= link @static_page.title, to: Routes.page_path(@conn, :show, @static_page)
table.table
thead
tr
th User
th Date
th Body
tbody
= for version <- @versions do
tr
td = link version.user.name, to: Routes.profile_path(@conn, :show, version.user)
td = pretty_time(version.created_at)
td.static-page__diff
= for diff <- version.difference do
= case diff do
- {:eq, lines} ->
= for line <- lines do
= line
= "\n"
- {:ins, lines} ->
ins.differ
= for line <- lines do
= line
= "\n"
- {:del, lines} ->
del.differ
= for line <- lines do
= line
= "\n"

View file

@ -0,0 +1,3 @@
defmodule PhilomenaWeb.Page.HistoryView do
use PhilomenaWeb, :view
end