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

Drop fields that has nil values #148

Merged
merged 6 commits into from
Aug 5, 2016
Merged

Drop fields that has nil values #148

merged 6 commits into from
Aug 5, 2016

Conversation

Fs02
Copy link
Contributor

@Fs02 Fs02 commented Jul 13, 2016

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.


defp drop_nil_values(map) do
map
|> Enum.filter(fn({_, v}) -> v != nil end)

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?

Copy link
Contributor Author

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
Copy link
Contributor

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

@paulcsmith
Copy link
Contributor

@fladson This looks really awesome. Just a couple small comments and then I'll merge this in :D

@fladson
Copy link
Contributor

fladson commented Jul 29, 2016

oops, I think you meant @Fs02 😁

@paulcsmith
Copy link
Contributor

@fladson Oops! I need to be more careful about using GitHub autocomplete :) Thanks!

@Fs02
Copy link
Contributor Author

Fs02 commented Jul 30, 2016

I've updated the PR, this is what you meant right?

and I'm wondering whether we should rename drop_nil_values/1 to drop_nil_fields or maybe drop_empty_fields?

@@ -50,6 +50,12 @@ defmodule ExMachina.EctoTest do
end
end

test "params_for/2 removes doesn't return nil fields" do
Copy link
Contributor

@paulcsmith paulcsmith Aug 2, 2016

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"?

@paulcsmith
Copy link
Contributor

@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!

@Fs02
Copy link
Contributor Author

Fs02 commented Aug 3, 2016

Updated, thanks!!

@paulcsmith paulcsmith merged commit 7f43711 into beam-community:master Aug 5, 2016
@paulcsmith
Copy link
Contributor

Looks awesome! Thanks @Fs02

@pdawczak pdawczak mentioned this pull request Aug 13, 2016
@barberj
Copy link

barberj commented Sep 15, 2016

New to elixir, but this PR breaks how I was using ex_machina. Curious what the expected approach should be.

I'm setting some valid attrs for testing on a module attribute

@valid_attrs FieldGuide.Factory.params_for(:person)

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.

changeset = Person.changeset(%Person{}, %{@valid_attrs | deleted: true, user_id: user.id})

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

@paulcsmith
Copy link
Contributor

@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 params_for(:person) directly. Then you can do params_for(:person, deleted: true, user_id: user.id)

Does that help?

@barberj
Copy link

barberj commented Sep 15, 2016

It does, thanks for the help and the awesome elixir libraries

@paulcsmith
Copy link
Contributor

Awesome, I'm glad that helped. Thanks for the kind words!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants