mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-01-19 14:17:59 +01:00
static page versions
This commit is contained in:
parent
2d9b85d686
commit
b56485f13a
4 changed files with 100 additions and 0 deletions
27
lib/philomena/static_pages/version.ex
Normal file
27
lib/philomena/static_pages/version.ex
Normal 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
|
35
lib/philomena_web/controllers/page/history_controller.ex
Normal file
35
lib/philomena_web/controllers/page/history_controller.ex
Normal 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
|
35
lib/philomena_web/templates/page/history/index.html.slime
Normal file
35
lib/philomena_web/templates/page/history/index.html.slime
Normal 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"
|
3
lib/philomena_web/views/page/history_view.ex
Normal file
3
lib/philomena_web/views/page/history_view.ex
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
defmodule PhilomenaWeb.Page.HistoryView do
|
||||||
|
use PhilomenaWeb, :view
|
||||||
|
end
|
Loading…
Reference in a new issue