Created
October 3, 2019 21:37
-
-
Save gullitmiranda/a9f1d1f308461a339f500651873009e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git lib/hive/acquisitions/document_template.ex lib/hive/acquisitions/document_template.ex | |
index 411b0d8a..2ce227ee 100644 | |
--- lib/hive/acquisitions/document_template.ex | |
+++ lib/hive/acquisitions/document_template.ex | |
@@ -21,7 +21,21 @@ defmodule Hive.Acquisitions.DocumentTemplate do | |
has_many( | |
:processes_templates, | |
- through: [:processes_templates_documents_templates, :process_template] | |
+ through: [:processes_templates_documents_templates, :process_template], | |
+ where: [archived_at: nil] | |
+ ) | |
+ | |
+ has_many( | |
+ :processes_templates_latest_versions, | |
+ through: [:processes_templates_documents_templates, :process_template, :last_version] | |
+ ) | |
+ | |
+ # é o mesmo id da primeira versão criada | |
+ field(:template_id, Ecto.UUID) | |
+ | |
+ has_one(:last_version, __MODULE__, | |
+ foreign_key: :id, | |
+ references: :template_id | |
) | |
# FIXME: @deprecated fields | |
diff --git lib/hive/acquisitions/process_template.ex lib/hive/acquisitions/process_template.ex | |
index 5bd6c0ce..2f70d390 100644 | |
--- lib/hive/acquisitions/process_template.ex | |
+++ lib/hive/acquisitions/process_template.ex | |
@@ -35,14 +35,46 @@ defmodule Hive.Acquisitions.ProcessTemplate do | |
has_many(:companies, through: [:companies_groups, :company]) | |
has_many(:processes_templates_documents_templates, ProcessTemplateDocumentTemplate) | |
+ # é o mesmo id da primeira versão criada | |
+ field(:template_id, :binary_id) | |
+ | |
+ has_one(:last_version, __MODULE__, | |
+ foreign_key: :id, | |
+ references: :template_id | |
+ ) | |
+ | |
has_many( | |
:documents_templates, | |
through: [:processes_templates_documents_templates, :document_template] | |
) | |
+ has_many( | |
+ :documents_templates_latest_versions, | |
+ through: [:processes_templates_documents_templates, :document_template, :last_version] | |
+ ) | |
+ | |
+ # to support order_by, need the fork: https://github.com/elixir-ecto/ecto/pull/2859 | |
+ # has_many(:processes_templates_documents_templates, ProcessTemplateDocumentTemplate, | |
+ # order_by: [asc: :order] | |
+ # ) | |
+ | |
timestamps() | |
end | |
+ @doc """ | |
+ """ | |
+ def preload_last_documents_templates(query) do | |
+ query |> Repo.preload(documents_templates: preload_last_documents_templates_query()) | |
+ end | |
+ | |
+ def preload_last_documents_templates_query do | |
+ from( | |
+ dt in DocumentTemplate, | |
+ distinct: dt.template_id, | |
+ order_by: [desc: dt.created_at] | |
+ ) | |
+ end | |
+ | |
@doc """ | |
Builds a changeset based on the `struct` and `params`. | |
""" | |
diff --git lib/hive/acquisitions/process_template_document_template.ex lib/hive/acquisitions/process_template_document_template.ex | |
index bc4af256..da538b7e 100644 | |
--- lib/hive/acquisitions/process_template_document_template.ex | |
+++ lib/hive/acquisitions/process_template_document_template.ex | |
@@ -6,8 +6,8 @@ defmodule Hive.Acquisitions.ProcessTemplateDocumentTemplate do | |
schema "processes_templates_documents_templates" do | |
field(:order, :integer) | |
- belongs_to(:document_template, DocumentTemplate, foreign_key: :document_template_id) | |
- belongs_to(:process_template, ProcessTemplate, foreign_key: :process_template_id) | |
+ belongs_to(:document_template, DocumentTemplate) | |
+ belongs_to(:process_template, ProcessTemplate) | |
timestamps() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment