Skip to content

Commit

Permalink
Ecto 3, removed dependency on Phoenix
Browse files Browse the repository at this point in the history
  • Loading branch information
David Simon committed May 24, 2019
1 parent 4e527b3 commit d79eef9
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 59 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ defmodule MyProject.Post do

def changeset(model, params \\ %{}) do
model
|> cast(params, ~w(title body))
|> cast(params, ~w(title body)a)
|> validate_required(~w(title body)a)
|> prepare_version # add this
end
Expand Down Expand Up @@ -113,7 +113,7 @@ iex(2)> MyProject.Whatwasit.Version.versions post

In order to track deletes you need to pass a changeset to `Repo.delete` or `Repo.delete!`.

Note that this is not the default way phoneix.gen.html created the delete action.
Note that this is not the default way phoenix.gen.html created the delete action.

```elixir
defmodule MyProject.PostController do
Expand Down Expand Up @@ -150,7 +150,7 @@ defmodule MyProject.Post do
# ...
def changeset(model, params \\ %{}, opts \\ []) do
model
|> cast(params, ~w(title body))
|> cast(params, ~w(title body)a)
|> validate_required(~w(title body)a)
|> prepare_version(opts)
end
Expand Down
65 changes: 42 additions & 23 deletions lib/mix/tasks/whatwasit.install.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Mix.Tasks.Whatwasit.Install do
@moduledoc """
Setup the Whatwasit package for your Phoenix application.
Setup the Whatwasit package for your application.
Adds a migration for the Version model used to trackage changes to
the desired models.
Expand Down Expand Up @@ -85,21 +85,49 @@ defmodule Mix.Tasks.Whatwasit.Install do
|> print_instructions
end

defp gen_version_model(%{models: true, whodoneit_map: true, boilerplate: true, binding: binding} = config) do
defp gen_version_model(%{models: true, whodoneit_map: true, boilerplate: true, base: base} = config) do
changeset_fields = "~w(item_type item_id object action whodoneit)a"
schema_fields = "field :whodoneit, :map\n"
binding = binding ++ [
binding = [
base: base,
schema_fields: schema_fields,
changeset_fields: changeset_fields
]
Mix.Phoenix.copy_from paths(),
copy_from paths(),
"priv/templates/whatwasit.install/models/whatwasit", "", binding, [
{:eex, "version_map.ex", "web/models/whatwasit/version.ex"},
]
config
end

defp gen_version_model(%{models: true, boilerplate: true, binding: binding} = config) do
defp copy_from(apps, source_dir, target_dir, binding, mapping) when is_list(mapping) do
roots = Enum.map(apps, &to_app_source(&1, source_dir))

for {format, source_file_path, target_file_path} <- mapping do
source =
Enum.find_value(roots, fn root ->
source = Path.join(root, source_file_path)
if File.exists?(source), do: source
end) || raise "could not find #{source_file_path} in any of the sources"

target = Path.join(target_dir, target_file_path)

contents =
case format do
:text -> File.read!(source)
:eex -> EEx.eval_file(source, binding)
end

Mix.Generator.create_file(target, contents)
end
end

defp to_app_source(path, source_dir) when is_binary(path),
do: Path.join(path, source_dir)
defp to_app_source(app, source_dir) when is_atom(app),
do: Application.app_dir(app, source_dir)

defp gen_version_model(%{models: true, boilerplate: true, base: base} = config) do
name_field = "field :whodoneit_name, :string\n"
changeset_fields = "item_type item_id object action"
whodoneit_changeset_fields = " whodoneit_id whodoneit_name"
Expand All @@ -115,11 +143,12 @@ defmodule Mix.Tasks.Whatwasit.Install do
true ->
{"", ""}
end
binding = binding ++ [
binding = [
base: base,
schema_fields: schema_fields,
changeset_fields: "~w(#{changeset_fields}#{add_changeset_fields})a"
]
Mix.Phoenix.copy_from paths(),
copy_from paths(),
"priv/templates/whatwasit.install/models/whatwasit", "", binding, [
{:eex, "version.ex", "web/models/whatwasit/version.ex"},
]
Expand Down Expand Up @@ -181,7 +210,7 @@ defmodule Mix.Tasks.Whatwasit.Install do
path = case config[:migration_path] do
path when is_binary(path) -> path
_ ->
Path.relative_to(migrations_path(repo), Mix.Project.app_path)
Path.relative_to(Ecto.Migrator.migrations_path(repo), Mix.Project.app_path)
end
file = Path.join(path, "#{timestamp}_#{underscore(name)}.exs")
fun.(repo, path, file, name)
Expand Down Expand Up @@ -221,7 +250,7 @@ defmodule Mix.Tasks.Whatwasit.Install do
def changeset(model, params \\ %{}, opts \\ []) do
model
|> cast(params, ~w(title body))
|> cast(params, ~w(title body)a)
|> validate_required(~w(title body)a)
|> prepare_version(opts) # add this
end
Expand Down Expand Up @@ -255,7 +284,7 @@ defmodule Mix.Tasks.Whatwasit.Install do
def changeset(model, params \\ %{}) do
model
|> cast(params, ~w(title body))
|> cast(params, ~w(title body)a)
|> validate_required(~w(title body)a)
|> prepare_version # add this
end
Expand All @@ -282,19 +311,10 @@ defmodule Mix.Tasks.Whatwasit.Install do
# Config

defp do_config(opts, bin_opts) do
binding = Mix.Project.config
|> Keyword.fetch!(:app)
|> Atom.to_string
|> Mix.Phoenix.inflect

# IO.puts "binding: #{inspect binding}"

base = opts[:module] || binding[:base]
base = opts[:module] || (Mix.Project.config() |> Keyword.fetch!(:app) |> to_string |> Macro.camelize())
opts = Keyword.put(opts, :base, base)
repo = (opts[:repo] || "#{base}.Repo")

binding = Keyword.put binding ,:base, base

user_schema = parse_model(opts[:model], base, opts)

whodoneit = if opts[:whodoneit_map] || opts[:whodoneit_id] || opts[:whodoneit_id_type], do: true, else: opts[:whodoneit]
Expand All @@ -316,7 +336,6 @@ defmodule Mix.Tasks.Whatwasit.Install do
|> Map.put(:base, base)
|> Map.put(:user_schema, user_schema)
|> Map.put(:repo, repo)
|> Map.put(:binding, binding)
|> Map.put(:migration_path, opts[:migration_path])
|> Map.put(:module, opts[:module])
|> Map.put(:whodoneit, whodoneit)
Expand All @@ -333,8 +352,8 @@ defmodule Mix.Tasks.Whatwasit.Install do
end
opts_bin = Enum.uniq(opts_bin)
opts_names = Enum.map opts, &(elem(&1, 0))
with [] <- Enum.filter(opts_bin, &(not &1 in @switch_names)),
[] <- Enum.filter(opts_names, &(not &1 in @switch_names)) do
with [] <- Enum.filter(opts_bin, &(&1 not in @switch_names)),
[] <- Enum.filter(opts_names, &(&1 not in @switch_names)) do
{opts_bin, opts}
else
list -> raise_option_errors(list)
Expand Down
8 changes: 4 additions & 4 deletions lib/whatwasit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule Whatwasit do
def changeset(model, params \\ %{}) do
model
|> cast(params, ~w(title body))
|> cast(params, ~w(title body)a)
|> validate_required(~w(title body)a)
|> prepare_version # add this
end
Expand Down Expand Up @@ -58,7 +58,7 @@ defmodule Whatwasit do
# ...
def changeset(model, params \\ %{}, opts \\ []) do
model
|> cast(params, ~w(title body))
|> cast(params, ~w(title body)a)
|> validate_required(~w(title body)a)
|> prepare_version(opts)
end
Expand Down Expand Up @@ -106,8 +106,8 @@ defmodule Whatwasit do
%MyProject.Post{__meta__: #Ecto.Schema.Metadata<:loaded, "posts">,
body: "The answer is 42", id: 9,
inserted_at: #Ecto.DateTime<2016-07-22 01:49:25>, title: "What's the Question",
updated_at: #Ecto.DateTime<2016-07-22 01:49:55>}
inserted_at: #DateTime<2016-07-22 01:49:25>, title: "What's the Question",
updated_at: #DateTime<2016-07-22 01:49:55>}
iex(5)> MyProject.Whatwasit.Version.versions post
Expand Down
8 changes: 4 additions & 4 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ defmodule Whatwasit.Mixfile do
[applications: applications(Mix.env)]
end

defp applications(:test), do: [:logger, :ecto, :postgrex]
defp applications(_), do: [:logger, :ecto]
defp applications(:test), do: [:logger, :ecto, :ecto_sql, :postgrex]
defp applications(_), do: [:logger, :ecto, :ecto_sql]

defp elixirc_paths(:test), do: ["lib", "web", "test/support"]
defp elixirc_paths(_), do: ["lib", "web"]

defp deps do
[
{:ecto, "~> 2.0"},
{:phoenix, "~> 1.1"},
{:ecto_sql, "~> 3.0"},
{:jason, "~> 1.0"},
{:postgrex, ">= 0.0.0", only: :test},
{:ex_doc, "== 0.11.5", only: :dev},
{:earmark, "== 0.2.1", only: :dev, override: true},
Expand Down
20 changes: 10 additions & 10 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
%{"connection": {:hex, :connection, "1.0.3", "3145f7416be3df248a4935f24e3221dc467c1e3a158d62015b35bd54da365786", [:mix], []},
"db_connection": {:hex, :db_connection, "1.0.0-rc.3", "d9ceb670fe300271140af46d357b669983cd16bc0d01206d7d3222dde56cf038", [:mix], [{:sbroker, "~> 1.0.0-beta.3", [hex: :sbroker, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: true]}, {:connection, "~> 1.0.2", [hex: :connection, optional: false]}]},
"decimal": {:hex, :decimal, "1.1.2", "79a769d4657b2d537b51ef3c02d29ab7141d2b486b516c109642d453ee08e00c", [:mix], []},
%{
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm"},
"db_connection": {:hex, :db_connection, "2.0.6", "bde2f85d047969c5b5800cb8f4b3ed6316c8cb11487afedac4aa5f93fd39abfa", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm"},
"decimal": {:hex, :decimal, "1.7.0", "30d6b52c88541f9a66637359ddf85016df9eb266170d53105f02e4a67e00c5aa", [:mix], [], "hexpm"},
"earmark": {:hex, :earmark, "0.2.1", "ba6d26ceb16106d069b289df66751734802777a3cbb6787026dd800ffeb850f3", [:mix], []},
"ecto": {:hex, :ecto, "2.0.2", "b02331c1f20bbe944dbd33c8ecd8f1ccffecc02e344c4471a891baf3a25f5406", [:mix], [{:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: true]}, {:sbroker, "~> 1.0-beta", [hex: :sbroker, optional: true]}, {:mariaex, "~> 0.7.7", [hex: :mariaex, optional: true]}, {:postgrex, "~> 0.11.2", [hex: :postgrex, optional: true]}, {:db_connection, "~> 1.0-rc.2", [hex: :db_connection, optional: true]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: false]}]},
"ecto": {:hex, :ecto, "3.1.4", "69d852da7a9f04ede725855a35ede48d158ca11a404fe94f8b2fb3b2162cd3c9", [:mix], [{:decimal, "~> 1.6", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"},
"ecto_sql": {:hex, :ecto_sql, "3.1.3", "2c536139190492d9de33c5fefac7323c5eaaa82e1b9bf93482a14649042f7cd9", [], [{:db_connection, "~> 2.0", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.1.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.9.1", [hex: :mariaex, repo: "hexpm", optional: true]}, {:myxql, "~> 0.2.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.14.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.11.5", "0dc51cb84f8312162a2313d6c71573a9afa332333d8a332bb12540861b9834db", [:mix], [{:earmark, "~> 0.1.17 or ~> 0.2", [hex: :earmark, optional: true]}]},
"phoenix": {:hex, :phoenix, "1.2.0", "1bdeb99c254f4c534cdf98fd201dede682297ccc62fcac5d57a2627c3b6681fb", [:mix], [{:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: false]}, {:phoenix_pubsub, "~> 1.0", [hex: :phoenix_pubsub, optional: false]}, {:plug, "~> 1.1", [hex: :plug, optional: false]}, {:cowboy, "~> 1.0", [hex: :cowboy, optional: true]}]},
"phoenix_pubsub": {:hex, :phoenix_pubsub, "1.0.0", "c31af4be22afeeebfaf246592778c8c840e5a1ddc7ca87610c41ccfb160c2c57", [:mix], []},
"plug": {:hex, :plug, "1.1.6", "8927e4028433fcb859e000b9389ee9c37c80eb28378eeeea31b0273350bf668b", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, optional: true]}]},
"poison": {:hex, :poison, "2.2.0", "4763b69a8a77bd77d26f477d196428b741261a761257ff1cf92753a0d4d24a63", [:mix], []},
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], []},
"postgrex": {:hex, :postgrex, "0.11.2", "139755c1359d3c5c6d6e8b1ea72556d39e2746f61c6ddfb442813c91f53487e8", [:mix], [{:connection, "~> 1.0", [hex: :connection, optional: false]}, {:db_connection, "~> 1.0-rc", [hex: :db_connection, optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}]}}
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
"postgrex": {:hex, :postgrex, "0.14.3", "5754dee2fdf6e9e508cbf49ab138df964278700b764177e8f3871e658b345a1e", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.0", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"},
"telemetry": {:hex, :telemetry, "0.4.0", "8339bee3fa8b91cb84d14c2935f8ecf399ccd87301ad6da6b71c09553834b2ab", [], [], "hexpm"},
}
10 changes: 0 additions & 10 deletions test/mix_helpers.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,4 @@ defmodule MixHelper do
match.(File.read!(file))
end
end

def with_generator_env(new_env, fun) do
old = Application.get_env(:phoenix, :generators)
Application.put_env(:phoenix, :generators, new_env)
try do
fun.()
after
Application.put_env(:phoenix, :generators, old)
end
end
end
2 changes: 1 addition & 1 deletion test/schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule Whatwasit.Whatwasit.Version do
def changeset(model, params \\ %{}) do
params = update_in params, [:object], &(remove_fields(&1))
model
|> cast(params, ~w(item_type item_id object action whodoneit_id whodoneit_name))
|> cast(params, ~w(item_type item_id object action whodoneit_id whodoneit_name)a)
|> validate_required(~w(item_type item_id object)a)
end

Expand Down
2 changes: 1 addition & 1 deletion test/support/repo.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
defmodule TestWhatwasit.Repo do
use Ecto.Repo, otp_app: :whatwasit
use Ecto.Repo, otp_app: :whatwasit, adapter: Ecto.Adapters.Postgres
end
6 changes: 3 additions & 3 deletions test/support/schema.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule TestWhatwasit.User do

def changeset(model, params \\ %{}) do
model
|> cast(params, ~w(email name))
|> cast(params, ~w(email name)a)
|> validate_required(~w(email name)a)
end
end
Expand All @@ -27,7 +27,7 @@ defmodule TestWhatwasit.Account do

def changeset(model, params \\ %{}) do
model
|> cast(params, ~w(email full_name))
|> cast(params, ~w(email full_name)a)
|> validate_required(~w(email full_name)a)
end
end
Expand All @@ -44,7 +44,7 @@ defmodule TestWhatwasit.Post do

def changeset(model, params \\ %{}, opts \\ []) do
model
|> cast(params, ~w(title body))
|> cast(params, ~w(title body)a)
|> validate_required(~w(title body)a)
|> Whatwasit.Whatwasit.Version.prepare_version(opts)
end
Expand Down

0 comments on commit d79eef9

Please sign in to comment.