Skip to content

Commit

Permalink
Use polymorphic entities in liveview
Browse files Browse the repository at this point in the history
It's a little too bespoke to the structure of the gita now, I should
make it more generic so that other texts can easily work with the same
seeding function.
  • Loading branch information
rtshkmr committed Feb 1, 2024
1 parent dbd2c14 commit c716a36
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 24 deletions.
60 changes: 55 additions & 5 deletions docs/initial_db_helpers.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ defmodule G do
body: chapter["chapter_summary"],
source_id: source_id
}
|> dbg()
end

def create_chapter(%{} = chapter, [%{} = _head | _rest] = _verses, source_id) do
Expand Down Expand Up @@ -137,7 +138,9 @@ defmodule G do
Chapter.changeset(
%Chapter{
no: chap["chapter_number"],
source_id: src_id
source_id: src_id,
title: chap["name"],
body: chap["chapter_summary"]
},
%{
verses: verses
Expand All @@ -152,8 +155,10 @@ defmodule G do
lang: "en",
body: chap["chapter_summary"],
target: %{
title: chap["name"],
translit_title: chap["name_transliterated"]
title: chap["name_meaning"],
translit_title: chap["name_transliterated"],
body: chap["chapter_summary"],
body_meant: chap["chapter_summary_hindi"]
}
},
inserted_chap
Expand Down Expand Up @@ -246,8 +251,6 @@ defmodule DBHelper do
DBHelper.insert_chapters(uuid) |> dbg()
end
end

alias Vyasa.Written.{Source}
```

#### Testing the purging and seeding of the db:
Expand All @@ -258,6 +261,53 @@ DBHelper.purge_db()
DBHelper.seed_db()
```

```elixir
### checking the query functions

alias Vyasa.Written
alias Vyasa.Repo

R.recompile()

src =
Written.list_sources()
|> Enum.find(fn src -> src.title == "Gita" end)

# src.id

# Repo.get_by(Chapter, no: 1, source_id: src.id)

# src = Written.get_source_by_title("Gita")
# foo = src.id
# # h_v = hd(src.verses)
# # h_v.translations
# # foo = src.id
# chap = Written.get_chapter(12, foo)
# hd(chap.translations)

# source_title = "Gita"

# src = Written.list_sources()
# |>Enum.find(fn src -> src.title == source_title end)
# no = 2

# chap = Repo.get_by(Chapter, no: no, source_id: src.id)
# |> Repo.preload([:verses])

# first_verse = hd(chap.verses)

# R.recompile()
# chap3 = Written.get_chapter(3, "Gita")
# verses = Written.get_verses_in_chapter(3, src.id)
# first_v = hd(verses)
# first_v.translations
# Written.get_verse_via_url_params(3, 1, "Gita")

# hd(Repo.all(Translation))

# chap3.translations
```

##### Bala's working example of using the polymorphic translations

<!-- livebook:{"break_markdown":true} -->
Expand Down
10 changes: 5 additions & 5 deletions lib/vyasa/written.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,31 +73,31 @@ defmodule Vyasa.Written do
def get_source_by_title(title) do
list_sources()
|>Enum.find(fn src -> src.title == title end)
|> Repo.preload([:chapters, :verses])
|> Repo.preload([verses: [:translations], chapters: [:translations]])

end

def get_chapter(no, source_title) do
src = list_sources()
|>Enum.find(fn src -> src.title == source_title end)
Repo.get_by(Chapter, no: no, source_id: src.id)
|> Repo.preload([:verses])
|> Repo.preload([:translations, verses: [:translations]])
end

def get_verses_in_chapter(no, source_id) do
chapter = Repo.get_by(Chapter, no: no, source_id: source_id)
|> Repo.preload([:verses])
|> Repo.preload([:verses, :translations])

chapter.verses
end

def get_verse_via_url_params(verse_no, chap_no, source_title) do
chapter = get_chapter(chap_no, source_title)
|> Repo.preload([:verses, :source])
|> Repo.preload([:source, :translations], verses: [:translations])

chapter.verses
|> Enum.find(fn verse -> verse.no == verse_no end)
|> Repo.preload([:chapter, :source])
|> Repo.preload([:chapter, :source, :translations])
end

@doc """
Expand Down
4 changes: 2 additions & 2 deletions lib/vyasa/written/chapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Vyasa.Written.Chapter do
use Ecto.Schema
import Ecto.Changeset

alias Vyasa.Written.{Source, Verse}
alias Vyasa.Written.{Source, Verse, Translation}

@primary_key false
schema "chapters" do
Expand All @@ -16,8 +16,8 @@ defmodule Vyasa.Written.Chapter do
# field :indic_name_transliteration, :string

belongs_to :source, Source, references: :id, foreign_key: :source_id, type: :binary_id, primary_key: :true

has_many :verses, Verse, references: :no, foreign_key: :chapter_no
has_many :translations, Translation, references: :no, foreign_key: :chapter_no
end

@doc false
Expand Down
5 changes: 3 additions & 2 deletions lib/vyasa_web/live/source_live/chapter/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@ defmodule VyasaWeb.SourceLive.Chapter.Index do
verses: verses,
title: title,
body: body,
# indic_name: indic_name,
# indic_name_meaning: indic_name_meaning,
translations: translations,
} = Written.get_chapter(chap_no, source_title)

en_translation = translations |> Enum.find(fn t -> t.lang == "en" end)

socket
|> stream(:verses, verses)
|> assign(:source_title, source_title)
|> assign(:chap_no, chap_no)
|> assign(:chap_body, body)
|> assign(:chap_title, title)
|> assign(:en_translation, en_translation)
# |> assign(:chap_indic_name, indic_name)
# |> assign(:chap_indic_name_meaning, indic_name_meaning)
|> assign(:page_title, "#{source_title} Chapter #{chap_no} | #{title}")
Expand Down
22 changes: 17 additions & 5 deletions lib/vyasa_web/live/source_live/chapter/index.html.heex
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
<.header>
<div class="font-dn text-4xl mb-4">
STUB FOR INDIC NAME
<%= @en_translation.target.translit_title %> | <%= @chap_title %>
</div>
<div class="font-dn text-2xl mb-4">
Chapter <%= @chap_no%> - <%= @chap_title %>
Chapter <%= @chap_no%> - <%= @en_translation.target.title %>
</div>


<:subtitle>
<%= @chap_body %>
<%= @en_translation.target.body %>
</:subtitle>
</.header>

<.verse_list :for={{_dom_id, verse} <- @streams.verses}>
<:item title={"#{verse.chapter_no}.#{verse.no}"}
navigate={~p"/explore/#{@source_title}/#{@chap_no}/#{verse.no}"} >
<p class="font-dn text-xl"><%= verse.body |> String.split("।।") |> List.first() %></p>

<p class="font-dn text-xl">
<%= verse.body |> String.split("।।") |> List.first() %>
</p>
</:item>
<:item title={"Transliteration"}>
<p class="font-dn text-lg">
<%= hd(verse.translations).target.body_translit %>
</p>
</:item>
<:item title={"Transliteration Meaning"}>
<p class="font-dn text-lg">
<%= hd(verse.translations).target.body_translit_meant %>
</p>
</:item>
</.verse_list>

Expand Down
3 changes: 3 additions & 0 deletions lib/vyasa_web/live/source_live/chapter/show_verse.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ defmodule VyasaWeb.SourceLive.Chapter.ShowVerse do
def handle_params(%{"source_title" => source_title, "chap_no" => chap_no, "verse_no" => verse_no}, _, socket) do
verse = Written.get_verse_via_url_params(String.to_integer(verse_no), chap_no, source_title) |> dbg()

en_translation = verse.translations |> Enum.find(fn t -> t.lang == "en" end)

{:noreply,
socket
|> assign(:source_title, source_title)
|> assign(:chap_no, chap_no)
|> assign(:verse_no, String.to_integer(verse_no))
|> assign(:verse, verse)
|> assign(:en_translation, en_translation)
# |> assign_meta()
}
# |> assign(:chapter, Written.get_chapter(chap_no, source_id))
Expand Down
21 changes: 18 additions & 3 deletions lib/vyasa_web/live/source_live/chapter/show_verse.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,25 @@
id="verseContainer"
>
<.header>
<:subtitle><%= @verse.chapter_no %>:<%= @verse.no %></:subtitle>
<p class="font-dn text-2xl">
<%= @verse.body %></p>
<:subtitle>
<%= @verse.chapter_no %>:<%= @verse.no %>
</:subtitle>
<p class="font-dn text-2xl">
<%= @verse.body %>
</p>
</.header>

<p class="font-dn text-2xl m-4">
<%= @en_translation.target.body_translit %>
</p>

<p class="font-dn text-2xl m-4">
<%= @en_translation.target.body_translit_meant %>
</p>




<br />
<br />
<br />
Expand Down
1 change: 1 addition & 0 deletions lib/vyasa_web/live/source_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ defmodule VyasaWeb.SourceLive.Show do
title: title
} = Written.get_source_by_title(source_title)


{
:noreply,
socket
Expand Down
5 changes: 3 additions & 2 deletions lib/vyasa_web/live/source_live/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ row_click={fn {_id, chap} -> JS.navigate(~p"/explore/#{@title}/#{chap.no}") end}
>
<:col :let={{_id, chap}} label="Chapter">
<div class="font-dn text-lg">
<%= chap.no %>. <%= chap.indic_name_translation %>
<%= chap.no %>. <%= hd(chap.translations).target.translit_title %>
</div>
</:col>
<:col :let={{_id, chap}} label="Description">
<div class="font-dn text-md" >
<%= chap.title %>
</div>
<div class="font-dn text-md" >
<%= chap.indic_name %>
<%= hd(chap.translations).target.title %>
</div>
</:col>

</.table>

<.back navigate={~p"/explore/"}>Back to All Sources</.back>

0 comments on commit c716a36

Please sign in to comment.