Add creating and basic editing of translation
This commit is contained in:
@ -9,9 +9,9 @@ defmodule Outlook.InternalTree do
|
||||
|> Html.to_html()
|
||||
end
|
||||
|
||||
def render_html_preview(tree) do
|
||||
def render_html_preview(tree, target \\ "1") do
|
||||
tree
|
||||
|> Html.to_html_preview("1")
|
||||
|> Html.to_html_preview(target)
|
||||
end
|
||||
|
||||
require Logger
|
||||
|
||||
@ -35,7 +35,10 @@ defmodule Outlook.Translations do
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
def get_translation!(id), do: Repo.get!(Translation, id)
|
||||
def get_translation!(id) do
|
||||
Repo.get!(Translation, id)
|
||||
|> Repo.preload([:article])
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a translation.
|
||||
|
||||
25
lib/outlook/translations/basic.ex
Normal file
25
lib/outlook/translations/basic.ex
Normal file
@ -0,0 +1,25 @@
|
||||
defmodule Outlook.Translations.Basic do
|
||||
|
||||
alias Outlook.InternalTree.InternalNode
|
||||
alias Outlook.InternalTree.TranslationUnit
|
||||
|
||||
def internal_tree_to_tunit_map(tree) do
|
||||
collect_translation_units(tree)
|
||||
|> Enum.map(fn tunit -> {tunit.uuid, tunit} end)
|
||||
|> Enum.into(%{})
|
||||
end
|
||||
|
||||
defp collect_translation_units([%InternalNode{type: :element} = node | rest]) do
|
||||
collect_translation_units(node.content) ++ collect_translation_units(rest)
|
||||
end
|
||||
|
||||
defp collect_translation_units([%TranslationUnit{} = tunit | rest]) do
|
||||
[tunit | collect_translation_units(rest)]
|
||||
end
|
||||
|
||||
defp collect_translation_units([_|rest]) do
|
||||
[] ++ collect_translation_units(rest)
|
||||
end
|
||||
|
||||
defp collect_translation_units([]), do: []
|
||||
end
|
||||
@ -24,7 +24,7 @@ defmodule Outlook.Translations.Translation do
|
||||
def changeset(translation, attrs) do
|
||||
translation
|
||||
|> cast(attrs, [:lang, :title, :teaser, :date, :public, :unauthorized, :article_id])
|
||||
|> cast(attrs, [:content], force_changes: true)
|
||||
|> cast(attrs, [:content])
|
||||
|> validate_required([:lang, :title, :content, :date, :public, :unauthorized, :article_id])
|
||||
|> unique_constraint([:lang, :article_id],
|
||||
message: "translation for this language already exists",
|
||||
|
||||
Reference in New Issue
Block a user