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

Only drop autogenerated ids and not domain ids #147

Merged
merged 3 commits into from
Jul 5, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 10 additions & 1 deletion lib/ex_machina/ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,18 @@ defmodule ExMachina.Ecto do
|> Map.from_struct
|> Map.delete(:__meta__)
|> Map.drop(struct.__schema__(:associations))
|> Map.drop(struct.__schema__(:primary_key))
|> drop_autogenerated_ids(struct)
end
defp drop_ecto_fields(record) do
raise ArgumentError, "#{inspect record} is not an Ecto model. Use `build` instead."
end

# Don't want to drop non-autogenrated ids because those are meaningful to the
# domain in some way and will be expected to be set explicitly
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the test explains this well and the comment can be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment hater! :) I went ahead and removed it...

defp drop_autogenerated_ids(map, struct) do
case struct.__schema__(:autogenerate_id) do
nil -> map
{name, _type} -> Map.delete(map, name)
end
end
end
7 changes: 7 additions & 0 deletions test/ex_machina/ecto_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ defmodule ExMachina.EctoTest do
}
end

test "params_for/2 leaves ids that are not auto-generated" do
assert TestFactory.params_for(:custom) == %{
custom_id: 1,
name: "Testing"
}
end

test "params_for/2 raises when passed a map" do
assert_raise ArgumentError, fn ->
TestFactory.params_for(:user_map)
Expand Down
9 changes: 9 additions & 0 deletions test/support/models/custom.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule ExMachina.Custom do
use Ecto.Schema

@primary_key {:custom_id, :integer, []}
schema "customs" do
field :name, :string
end
end

6 changes: 6 additions & 0 deletions test/support/test_factory.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
defmodule ExMachina.TestFactory do
use ExMachina.Ecto, repo: ExMachina.TestRepo

def custom_factory do
%ExMachina.Custom{
custom_id: 1,
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe non_autogenerated_id to make this more clear?

name: "Testing"
}
end
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a newline underneath this?

def user_factory do
%ExMachina.User{
name: "John Doe",
Expand Down