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

Refactor Some Code #205

Merged
merged 5 commits into from
Mar 13, 2017
Merged

Conversation

BenMorganIO
Copy link
Contributor

Hey guys,

I was going through the open source code and reasoning about the implementation. I noticed there was some code that could have been refactored. I hope this helps :)

Copy link
Contributor

@paulcsmith paulcsmith left a comment

Choose a reason for hiding this comment

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

Thanks a ton. Nice simple refactors that help clean things up 👍 Just one thing that would be great if you changed. Reverting the start_link and reset functions https://github.com/thoughtbot/ex_machina/pull/205/files#r105686541

defp build_function_name(factory_name) do
factory_name
|> Atom.to_string
<> "_factory"
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 do Kernel.<>("_factory") here instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that's what we're doing. It's taking Atom.to_string(factory_name) <> "_factory". Can you expand more on what you mean?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry for the confusion. I meant to type |> Kernel.<>("_factory") that way you can keep the pipe chain going

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, good catch. Running this code in my iex and seeing that it doesn't actually work...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And fixed :p

Copy link
Contributor

Choose a reason for hiding this comment

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

👍 :D

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry for jumping to the party and possibly silly question, but why not simply:

"#{factory_name}_factory"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pdawczak that's a very very good question :p

end

defp do_merge(%{__struct__: _} = record, attrs), do: struct!(record, attrs)
defp do_merge(record, attrs), do: Map.merge(record, attrs)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!

@@ -177,20 +180,16 @@ defmodule ExMachina do
build_list(3, :user)
"""
def build_list(module, number_of_factories, factory_name, attrs \\ %{}) do
Enum.map(1..number_of_factories, fn(_) ->
Enum.map 1..number_of_factories, fn(_) ->
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah that looks nicer 👍

@doc """
Raises a helpful error if no factory is defined.
"""
@doc "Raises a helpful error if no factory is defined."
Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch

@@ -230,7 +226,7 @@ defmodule ExMachina.Ecto do
defp insert_built_belongs_to_assoc(module, association, record) do
case Map.get(record, association.field) do
built_relation = %{__meta__: %{state: :built}} ->
relation = built_relation |> module.insert
relation = module.insert(built_relation)
Copy link
Contributor

Choose a reason for hiding this comment

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

Good idea. That reads better

)
Enum.reduce struct, Map.new, fn({key, value}, acc) ->
Map.put(acc, to_string(key), value)
end
Copy link
Contributor

Choose a reason for hiding this comment

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

Much better

def reset do
Agent.update(__MODULE__, fn(_) -> Map.new end)
end
def reset, do: Agent.update(__MODULE__, fn(_) -> Map.new end)
Copy link
Contributor

Choose a reason for hiding this comment

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

For this and start_link I think I prefer the previous version. This is a bit too busy IMO

@BenMorganIO
Copy link
Contributor Author

@paulcsmith I've reverted and rebased the changes with start_link and reset. I've also added 81ffde1 for some areas that I missed.

@paulcsmith paulcsmith merged commit 4d9fdea into beam-community:master Mar 13, 2017
@paulcsmith
Copy link
Contributor

Thanks a ton @BenMorganIO!

@BenMorganIO BenMorganIO deleted the refactor-ecto branch March 13, 2017 16:44
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.

3 participants