Steve Pallen
unread,Jul 21, 2016, 12:09:18 PM7/21/16Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to elixi...@googlegroups.com
Thanks José.
Yes, I’m doing paper trail (not sure how you knew I was doing that LOL). Are you suggesting that I build 1 changeset that does the update and inserts a Version record? If so, can I do that without altering the original schema for the versioned model? Or are you suggesting that I add the Version insert in the controller’s update action.
Furthermore, I want to hook into the delete action to insert a Version record on delete. Normally, delete is called with the schema(model) and not a changeset.
Here is my Version schema:
```elixir
defmodule Whatwasit.Version do
use Ecto.Schema
import Ecto
import Ecto.Changeset
schema "versions" do
field :item_type, :string
field :item_id, :integer
field :action, :string # ~w(update delete)
field :object, :map # versioned schema stored as a map
field :whodoneit_name, :string # store name also to track if user is later deleted
belongs_to :whodoneit, Admin.User # TODO: need to get Admin.User from config
timestamps
end
def changeset(model, params \\ %{}) do
params = update_in params, [:object], &(Map.delete(&1, :__meta__) |> Map.delete(:__struct__))
model
|> cast(params, ~w(item_type item_id object whodoneit_id action whodoneit_name))
|> validate_required(~w(item_type item_id object)a)
end
end
```
Steve