Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to delete multiple features and to initialise existing record #97

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/paper_trail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ defmodule PaperTrail do
defdelegate get_versions(model, id, options), to: PaperTrail.VersionQueries
defdelegate get_current_model(version), to: PaperTrail.VersionQueries

@doc """
Intialise paper_trail for existing record
Copy link
Owner

@izelnakri izelnakri Jul 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this? "Explicitly inserts an already non-versioned existing record into versions table"

"""
def initialise(model, options \\ [origin: nil, meta: nil, originator: nil, prefix: nil, version_key: :version]) do
case get_version(model) do
nil ->
repo = RepoClient.repo()

make_version_struct(%{event: "insert"}, model, options)
|> repo.insert!()

_ ->
:error
end
end

@doc """
Inserts a record to the database with a related version insertion in one transaction
"""
Expand Down
9 changes: 6 additions & 3 deletions lib/paper_trail/multi.ex
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,14 @@ defmodule PaperTrail.Multi do
def delete(
%Ecto.Multi{} = multi,
struct,
options \\ [origin: nil, meta: nil, originator: nil, prefix: nil]
options \\ [origin: nil, meta: nil, originator: nil, prefix: nil, model_key: :model, version_key: :version]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call, thanks!

) do
model_key = options[:model_key] || :model
version_key = options[:version_key] || :version

multi
|> Ecto.Multi.delete(:model, struct, options)
|> Ecto.Multi.run(:version, fn repo, %{} ->
|> Ecto.Multi.delete(model_key, struct, options)
|> Ecto.Multi.run(version_key, fn repo, %{} ->
version = make_version_struct(%{event: "delete"}, struct, options)
repo.insert(version, options)
end)
Expand Down