Add the brief_description for Book and Author.

dev
KKlochko 2 months ago
parent 6021d3e8c8
commit af11f13942

@ -9,6 +9,9 @@ defmodule DecentralisedBookIndex.Metadata.Author do
require Ash.Query require Ash.Query
alias DecentralisedBookIndex.Metadata alias DecentralisedBookIndex.Metadata
@brief_description_length 20
@brief_description_max_length @brief_description_length + 3
json_api do json_api do
type "author" type "author"
end end
@ -241,4 +244,16 @@ defmodule DecentralisedBookIndex.Metadata.Author do
has_many :author_role, Metadata.AuthorRole has_many :author_role, Metadata.AuthorRole
end end
calculations do
calculate :brief_description,
:string,
expr(
if(string_length(description) <= @brief_description_max_length) do
description
else
fragment("substr(?, 1, ?)", description, @brief_description_length) <> "..."
end
)
end
end end

@ -9,6 +9,9 @@ defmodule DecentralisedBookIndex.Metadata.Book do
require Ash.Query require Ash.Query
alias DecentralisedBookIndex.Metadata alias DecentralisedBookIndex.Metadata
@brief_description_length 20
@brief_description_max_length @brief_description_length + 3
json_api do json_api do
type "book" type "book"
end end
@ -366,4 +369,16 @@ defmodule DecentralisedBookIndex.Metadata.Book do
public? true public? true
end end
end end
calculations do
calculate :brief_description,
:string,
expr(
if(string_length(description) <= @brief_description_max_length) do
description
else
fragment("substr(?, 1, ?)", description, @brief_description_length) <> "..."
end
)
end
end end

@ -63,6 +63,7 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Index do
page = page =
Metadata.search_author!( Metadata.search_author!(
search_query, search_query,
load: [:brief_description],
query: [sort_input: sort_by], query: [sort_input: sort_by],
page: page_params ++ [count: true], page: page_params ++ [count: true],
actor: socket.assigns.current_user actor: socket.assigns.current_user

@ -189,6 +189,7 @@ defmodule DecentralisedBookIndexWeb.BookLive.Index do
"title" -> "title" ->
Metadata.search_book!( Metadata.search_book!(
search_query, search_query,
load: [:brief_description],
query: [sort_input: sort_by], query: [sort_input: sort_by],
page: page_params ++ [count: true], page: page_params ++ [count: true],
actor: actor actor: actor
@ -198,6 +199,7 @@ defmodule DecentralisedBookIndexWeb.BookLive.Index do
Metadata.search_book_by_bid!( Metadata.search_book_by_bid!(
type, type,
search_query, search_query,
load: [:brief_description],
query: [sort_input: sort_by], query: [sort_input: sort_by],
page: page_params ++ [count: true], page: page_params ++ [count: true],
actor: actor actor: actor

@ -128,4 +128,21 @@ defmodule DecentralisedBookIndex.Metadata.AuthorTest do
assert author.avatar_url == avatar_url assert author.avatar_url == avatar_url
end end
end end
describe "calculate a brief description" do
test "a short description is already enough" do
description = "Amazing!"
assert description == Ash.calculate!(Metadata.Author, :brief_description,
refs: %{description: description})
end
test "a long description is too much" do
description = "Etiam laoreet quam sed arcu. Donec at pede. Pellentesque tristique imperdiet tortor."
brief = "Etiam laoreet quam s..."
assert String.length(brief) == 23
assert brief == Ash.calculate!(Metadata.Author, :brief_description,
refs: %{description: description})
end
end
end end

@ -273,4 +273,21 @@ defmodule DecentralisedBookIndex.Metadata.BookTest do
assert book.cover_image_url == cover_image_url assert book.cover_image_url == cover_image_url
end end
end end
describe "calculate a brief description" do
test "a short description is already enough" do
description = "Amazing!"
assert description == Ash.calculate!(Metadata.Book, :brief_description,
refs: %{description: description})
end
test "a long description is too much" do
description = "Etiam laoreet quam sed arcu. Donec at pede. Pellentesque tristique imperdiet tortor."
brief = "Etiam laoreet quam s..."
assert String.length(brief) == 23
assert brief == Ash.calculate!(Metadata.Book, :brief_description,
refs: %{description: description})
end
end
end end

Loading…
Cancel
Save