Browse Source

Initial translator implementation

main
Victor Roest 2 years ago
parent
commit
2c1aaa4648
Signed by: 0x76 GPG Key ID: A3923C699D1A3BDA
  1. 12
      lib/dps/poem.ex
  2. 6
      lib/dps_web/templates/poem/edit.html.eex
  3. 6
      lib/dps_web/templates/poem/new.html.eex
  4. 5
      lib/dps_web/templates/poem/show.html.eex
  5. 9
      priv/repo/migrations/20210324205830_add_translator.exs
  6. 29
      priv/repo/seeds.exs
  7. 12
      priv/static/css/app.css

12
lib/dps/poem.ex

@ -4,13 +4,14 @@ defmodule Dps.Poem do
alias Dps.{Author, Cache, Repo}
alias __MODULE__
@derive {Jason.Encoder, only: [:id, :title, :epigraph, :content, :author, :author_id]}
@derive {Jason.Encoder, only: [:id, :title, :epigraph, :content, :author, :author_id, :translator, :translator_id]}
schema "poems" do
field :title, :string
field :epigraph, :string
field :content, :string
belongs_to :author, Author
belongs_to :translator, Author
timestamps()
end
@ -18,10 +19,11 @@ defmodule Dps.Poem do
@doc false
def changeset(poem, attrs \\ %{}) do
poem
|> cast(attrs, [:title, :epigraph, :content, :author_id])
|> cast(attrs, [:title, :epigraph, :content, :author_id, :translator_id])
|> validate_required([:title, :content, :author_id])
|> unique_constraint([:title, :author_id])
|> foreign_key_constraint(:author_id)
|> foreign_key_constraint(:translator_id)
end
def list_poems(search \\ "") do
@ -38,8 +40,8 @@ defmodule Dps.Poem do
def list_poems_by_author(author_id) do
from(p in Poem,
select: %Poem{id: p.id, author_id: p.author_id, title: p.title},
where: p.author_id == ^author_id,
select: %Poem{id: p.id, author_id: p.author_id, translator_id: p.translator_id, title: p.title},
where: p.author_id == ^author_id or p.translator_id == ^author_id,
order_by: [asc: p.title]
)
|> Repo.all()
@ -53,7 +55,7 @@ defmodule Dps.Poem do
poem =
Poem
|> Repo.get(id)
|> Repo.preload(:author)
|> Repo.preload([:author, :translator])
Cache.put({:poem, id}, poem)

6
lib/dps_web/templates/poem/edit.html.eex

@ -20,6 +20,12 @@
<%= select f, :author_id, @authors %>
</div>
<div>
<%= label f, :translator_id %>
<%= error_tag f, :translator_id %>
<%= select f, :translator_id, [{"None", ""} | @authors] %>
</div>
<div>
<%= label f, :content %>
<%= error_tag f, :content %>

6
lib/dps_web/templates/poem/new.html.eex

@ -20,6 +20,12 @@
<%= select f, :author_id, @authors, [selected: @select] %>
</div>
<div>
<%= label f, :translator_id %>
<%= error_tag f, :translator_id %>
<%= select f, :translator_id, [{"None", ""} | @authors] %>
</div>
<div>
<%= label f, :content %>
<%= error_tag f, :content %>

5
lib/dps_web/templates/poem/show.html.eex

@ -10,6 +10,11 @@
<h3 class="author">
<%= link @poem.author.name, to: Routes.author_path(@conn, :show, @poem.author.id) %>
</h3>
<%= if @poem.translator do %>
<h3 class="translator">
<%= link @poem.translator.name, to: Routes.author_path(@conn, :show, @poem.translator.id) %>
</h3>
<% end %>
</header>
<%= if @poem.epigraph do %>

9
priv/repo/migrations/20210324205830_add_translator.exs

@ -0,0 +1,9 @@
defmodule Dps.Repo.Migrations.AddTranslator do
use Ecto.Migration
def change do
alter table(:poems) do
add :translator_id, references(:authors)
end
end
end

29
priv/repo/seeds.exs

@ -7,6 +7,8 @@ alias Dps.Author
coleridge = Repo.insert!(%Author{name: "Samuel Taylor Coleridge"}).id
robert_frost = Repo.insert!(%Author{name: "Robert Frost"}).id
edgar_allan_poe = Repo.insert!(%Author{name: "Edgar Allan Poe"}).id
clare_cavanagh = Repo.insert!(%Author{name: "Clare Cavanagh"}).id
adam_zagajewski = Repo.insert!(%Author{name: "Adam Zagajewski"}).id
Repo.insert!(%Poem{
author_id: coleridge,
@ -227,3 +229,30 @@ On the pallid bust of Pallas just above my chamber door;
And my soul from out that shadow that lies floating on the floor
Shall be liftednevermore!"
})
Repo.insert!(%Poem{
author_id: adam_zagajewski,
translator_id: clare_cavanagh,
title: "Try to Praise the Mutilated World",
content: "Try to praise the mutilated world.
Remember June's long days,
and wild strawberries, drops of rosé wine.
The nettles that methodically overgrow
the abandoned homesteads of exiles.
You must praise the mutilated world.
You watched the stylish yachts and ships;
one of them had a long trip ahead of it,
while salty oblivion awaited others.
You've seen the refugees going nowhere,
you've heard the executioners sing joyfully.
You should praise the mutilated world.
Remember the moments when we were together
in a white room and the curtain fluttered.
Return in thought to the concert where music flared.
You gathered acorns in the park in autumn
and leaves eddied over the earth's scars.
Praise the mutilated world
and the gray feather a thrush lost,
and the gentle light that strays and vanishes
and returns."
})

12
priv/static/css/app.css

@ -128,7 +128,17 @@ h3.author::before {
}
h3.author {
margin-top: 0;
margin: 0;
}
h3.translator::before {
content: "translated by ";
font-style: italic;
}
h3.translator {
margin: 0;
padding: 0;
}
blockquote {

Loading…
Cancel
Save