Skip to content

Commit

Permalink
Merge branch 'release/0.1.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed Oct 24, 2019
2 parents b96533e + 4803561 commit 43fade9
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 89 deletions.
4 changes: 2 additions & 2 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ config :eventful, Eventful.Test.Repo,
database: "eventful_test",
pool: Ecto.Adapters.SQL.Sandbox,
hostname: System.get_env("POSTGRES_HOST") || "localhost",
username: System.get_env("POSTGRES_USERNAME") || "postgres",
password: System.get_env("POSTGRES_PASSWORD") || "postgres",
username: System.get_env("POSTGRES_USERNAME") || "zacksiri",
password: System.get_env("POSTGRES_PASSWORD") || "",
priv: "test/support/"

config :eventful,
Expand Down
81 changes: 80 additions & 1 deletion lib/eventful.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,84 @@
defmodule Eventful do
@moduledoc """
Documentation for Eventful.
Sets up Event Tracking Schema
"""

defmacro __using__(options) do
caller = __CALLER__.module
binary_id = Keyword.get(options, :binary_id)
{parent_relation, parent_module} = Keyword.get(options, :parent)
{actor_relation, actor_module} = Keyword.get(options, :actor)

quote do
use Ecto.Schema
import Ecto.Changeset
import Eventful

if unquote(binary_id) do
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
end

schema "#{unquote(parent_relation)}_events" do
belongs_to(unquote(parent_relation), unquote(parent_module))
belongs_to(unquote(actor_relation), unquote(actor_module))

field(:domain, :string)
field(:metadata, :map, default: %{})
field(:name, :string)

timestamps(type: :utc_datetime_usec)
end

@doc false
def changeset(%unquote(caller){} = event, attrs) do
event
|> cast(attrs, [:name, :domain, :metadata])
|> cast_assoc(unquote(parent_relation))
|> cast_assoc(unquote(actor_relation))
|> validate_required([:name, :domain, unquote(parent_relation), unquote(actor_relation)])
end

def with_metadata(
%{changes: changes} = resource_changeset,
actor,
resource,
%{name: name, domain: domain} = _params
) do
event = %unquote(caller){
unquote(actor_relation) => actor,
unquote(parent_relation) => resource
}

{resource_changeset,
changeset(event, %{
domain: domain,
name: name,
metadata:
Enum.reduce(changes, %{}, fn {key, value}, acc ->
Map.merge(acc, %{
key => %{from: Map.get(resource, key), to: value}
})
end)
})}
end
end
end

defmacro handle(domain, options) do
using = Keyword.get(options, :using)
string_domain = Atom.to_string(domain)

quote do
def handle(
resource,
user,
%{domain: unquote(string_domain)} = event_params
) do
unquote(using).call(user, resource, event_params)
rescue
FunctionClauseError -> {:error, :invalid_transition_event}
end
end
end
end
84 changes: 0 additions & 84 deletions lib/eventful/events.ex

This file was deleted.

2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Eventful.MixProject do
def project do
[
app: :eventful,
version: "0.1.1",
version: "0.1.2",
elixir: "~> 1.8",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
Expand Down
2 changes: 1 addition & 1 deletion test/support/model/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Eventful.Test.Model.Event do
Model, Actor
}

use Eventful.Events,
use Eventful,
parent: {:model, Model},
actor: {:actor, Actor}

Expand Down

0 comments on commit 43fade9

Please sign in to comment.