Simplify template selection queries
This commit is contained in:
1
AUDIT.md
1
AUDIT.md
@@ -17,6 +17,7 @@ after each item.
|
||||
- [x] `rendering/filters.ex`
|
||||
- [x] `rendering/post_rendering.ex`
|
||||
- [x] `rendering/list_archive.ex`
|
||||
- [x] `rendering/template_selection.ex`
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -70,40 +70,39 @@ defmodule BDS.Rendering.TemplateSelection do
|
||||
end
|
||||
end
|
||||
|
||||
defp select_template(project_id, kind, slug) when is_binary(slug) and slug != "" do
|
||||
Repo.one(
|
||||
defp select_template(project_id, kind, slug) do
|
||||
project_id
|
||||
|> published_template_query(kind)
|
||||
|> select_template_scope(kind, slug)
|
||||
|> Repo.one()
|
||||
end
|
||||
|
||||
defp published_template_query(project_id, kind) do
|
||||
from template in Template,
|
||||
where:
|
||||
template.project_id == ^project_id and template.kind == ^kind and
|
||||
template.status == :published and
|
||||
template.enabled == true and template.slug == ^slug,
|
||||
limit: 1
|
||||
)
|
||||
template.enabled == true
|
||||
end
|
||||
|
||||
defp select_template(project_id, :post, nil) do
|
||||
defp select_template_scope(query, _kind, slug) when is_binary(slug) and slug != "" do
|
||||
from template in query,
|
||||
where: template.slug == ^slug,
|
||||
limit: 1
|
||||
end
|
||||
|
||||
defp select_template_scope(query, :post, nil) do
|
||||
default_slug = StarterTemplates.default_slug(:post)
|
||||
|
||||
Repo.one(
|
||||
from template in Template,
|
||||
where:
|
||||
template.project_id == ^project_id and template.kind == :post and
|
||||
template.status == :published and
|
||||
template.enabled == true and template.slug == ^default_slug,
|
||||
from template in query,
|
||||
where: template.slug == ^default_slug,
|
||||
limit: 1
|
||||
)
|
||||
end
|
||||
|
||||
defp select_template(project_id, kind, nil) do
|
||||
Repo.one(
|
||||
from template in Template,
|
||||
where:
|
||||
template.project_id == ^project_id and template.kind == ^kind and
|
||||
template.status == :published and
|
||||
template.enabled == true,
|
||||
defp select_template_scope(query, _kind, nil) do
|
||||
from template in query,
|
||||
order_by: [desc: template.created_at, desc: template.slug],
|
||||
limit: 1
|
||||
)
|
||||
end
|
||||
|
||||
defp published_template_body(%Template{content: content}) when is_binary(content),
|
||||
|
||||
Reference in New Issue
Block a user