Add Translations
mix phx.gen.live Translations Translation translations \ lang:string title:string teaser:text content:map \ date:utc_datetime user_id:references:users \ public:boolean unauthorized:boolean article_id:references:articles
This commit is contained in:
@ -11,6 +11,7 @@ defmodule Outlook.Articles.Article do
|
||||
field :title, :string
|
||||
field :url, :string
|
||||
belongs_to :author, Author
|
||||
has_many :translations, Translation
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
104
lib/outlook/translations.ex
Normal file
104
lib/outlook/translations.ex
Normal file
@ -0,0 +1,104 @@
|
||||
defmodule Outlook.Translations do
|
||||
@moduledoc """
|
||||
The Translations context.
|
||||
"""
|
||||
|
||||
import Ecto.Query, warn: false
|
||||
alias Outlook.Repo
|
||||
|
||||
alias Outlook.Translations.Translation
|
||||
|
||||
@doc """
|
||||
Returns the list of translations.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list_translations()
|
||||
[%Translation{}, ...]
|
||||
|
||||
"""
|
||||
def list_translations do
|
||||
Repo.all(Translation)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a single translation.
|
||||
|
||||
Raises `Ecto.NoResultsError` if the Translation does not exist.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_translation!(123)
|
||||
%Translation{}
|
||||
|
||||
iex> get_translation!(456)
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
def get_translation!(id), do: Repo.get!(Translation, id)
|
||||
|
||||
@doc """
|
||||
Creates a translation.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> create_translation(%{field: value})
|
||||
{:ok, %Translation{}}
|
||||
|
||||
iex> create_translation(%{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def create_translation(attrs \\ %{}) do
|
||||
%Translation{}
|
||||
|> Translation.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a translation.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> update_translation(translation, %{field: new_value})
|
||||
{:ok, %Translation{}}
|
||||
|
||||
iex> update_translation(translation, %{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def update_translation(%Translation{} = translation, attrs) do
|
||||
translation
|
||||
|> Translation.changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a translation.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> delete_translation(translation)
|
||||
{:ok, %Translation{}}
|
||||
|
||||
iex> delete_translation(translation)
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def delete_translation(%Translation{} = translation) do
|
||||
Repo.delete(translation)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an `%Ecto.Changeset{}` for tracking translation changes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_translation(translation)
|
||||
%Ecto.Changeset{data: %Translation{}}
|
||||
|
||||
"""
|
||||
def change_translation(%Translation{} = translation, attrs \\ %{}) do
|
||||
Translation.changeset(translation, attrs)
|
||||
end
|
||||
end
|
||||
28
lib/outlook/translations/translation.ex
Normal file
28
lib/outlook/translations/translation.ex
Normal file
@ -0,0 +1,28 @@
|
||||
defmodule Outlook.Translations.Translation do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
alias Outlook.Accounts.User
|
||||
alias Outlook.Articles.Article
|
||||
|
||||
schema "translations" do
|
||||
field :content, :map
|
||||
field :date, :utc_datetime
|
||||
field :lang, :string
|
||||
field :public, :boolean, default: false
|
||||
field :teaser, :string
|
||||
field :title, :string
|
||||
field :unauthorized, :boolean, default: false
|
||||
belongs_to :user, User
|
||||
belongs_to :article, Article
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(translation, attrs) do
|
||||
translation
|
||||
|> cast(attrs, [:lang, :title, :teaser, :content, :date, :public, :unauthorized])
|
||||
|> validate_required([:lang, :title, :teaser, :content, :date, :public, :unauthorized])
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user