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