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

feat: add a way to redact specific keys from the struct #185

Closed
wants to merge 1 commit into from
Closed
Changes from all 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
15 changes: 14 additions & 1 deletion lib/paper_trail/serializer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ defmodule PaperTrail.Serializer do
@spec serialize(nil | Ecto.Changeset.t() | struct()) :: nil | map()
def serialize(nil), do: nil
def serialize(%Ecto.Changeset{data: data}), do: serialize(data)
def serialize(%_schema{} = model), do: Ecto.embedded_dump(model, :json)

def serialize(%schema{} = model),
do:
Ecto.embedded_dump(model, :json)
|> redact_hash(schema)

@doc """
Dumps changes using Ecto fields
Expand Down Expand Up @@ -193,4 +197,13 @@ defmodule PaperTrail.Serializer do
end
end)
end

defp redact_hash(struct, schema) do
if function_exported?(schema, :redacted_keys, 0) do
struct
|> Map.drop(schema.redacted_keys)
else
struct
end
end
end