-
Notifications
You must be signed in to change notification settings - Fork 146
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
Drop fields that has nil values #148
Conversation
|
||
defp drop_nil_values(map) do | ||
map | ||
|> Enum.filter(fn({_, v}) -> v != nil end) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this 👍
I feel that using Enum.reject
would make the intention a bit clearer. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I agree on that. I will update it 😃
@@ -122,6 +122,7 @@ defmodule ExMachina.Ecto do | |||
|> Map.delete(:__meta__) | |||
|> Map.drop(struct.__schema__(:associations)) | |||
|> drop_autogenerated_ids(struct) | |||
|> drop_nil_values |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of adding this to drop_ecto_fields
what do you think of adding it as the last pipe in params_for
and params_with_assocs
? I think that's more clear than adding it to drop_ecto_fields
@fladson This looks really awesome. Just a couple small comments and then I'll merge this in :D |
oops, I think you meant @Fs02 😁 |
@fladson Oops! I need to be more careful about using GitHub autocomplete :) Thanks! |
…s. rename v to value
I've updated the PR, this is what you meant right? and I'm wondering whether we should rename |
@@ -50,6 +50,12 @@ defmodule ExMachina.EctoTest do | |||
end | |||
end | |||
|
|||
test "params_for/2 removes doesn't return nil fields" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about "params_for/2 removes fields with nil values"?
@Fs02 Yeah that's a good idea to clarify that function name. I left a comment with a suggestion. Once that's addressed I'll go ahead and merge this in! |
Updated, thanks!! |
Looks awesome! Thanks @Fs02 |
New to elixir, but this PR breaks how I was using I'm setting some valid attrs for testing on a module attribute
And then later on in a test I mutate just what I'm testing and add the required things that the factory didn't provide.
Now that user_id is stripped i can't sub it in for my test. Is there a better practice I should be following? Thanks |
@barberj I just took a quick looks, but instead of using the update syntax, I think you could use Map.merge changeset = Person.changeset(%Person{}, Map.merge(@valid_attrs, %{deleted: true, user_id: user.id})) Alternatively you could use a function instead of an attribute changeset = Person.changeset(%Person{}, valid_attrs(deleted: true, user_id: user.id))
def valid_attrs(overrides \\ []) do
FieldGuide.Factory.params_for(:person, Map.new(overrides))
end Final option would be to use Does that help? |
It does, thanks for the help and the awesome elixir libraries |
Awesome, I'm glad that helped. Thanks for the kind words! |
This will drop all key that contains nil values which remain untouched previously.
This also fixes error when using ex_machina and arc_ecto on testing. which is caused by
cast_attachments/3
on arc_ecto doesn't expect the value of the fields to be nil.