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

Handle nested structures in string_params_for/2 #224

Merged
merged 1 commit into from
Jun 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 7 additions & 3 deletions lib/ex_machina/ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,13 @@ defmodule ExMachina.Ecto do
|> Enum.into(%{})
end

defp convert_atom_keys_to_strings(struct) do
Enum.reduce struct, Map.new, fn({key, value}, acc) ->
Map.put(acc, to_string(key), value)
defp convert_atom_keys_to_strings(values) when is_list(values) do
Enum.map(values, &convert_atom_keys_to_strings/1)
end
defp convert_atom_keys_to_strings(record) when is_map(record) do
Enum.reduce record, Map.new, fn({key, value}, acc) ->
Map.put(acc, to_string(key), convert_atom_keys_to_strings(value))
end
end
defp convert_atom_keys_to_strings(value), do: value
end
35 changes: 35 additions & 0 deletions test/ex_machina/ecto_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,41 @@ defmodule ExMachina.EctoTest do
}
end

test "string_params_for/2 converts has_one association into map with strings as keys" do
article = TestFactory.build(:article)

user_params = TestFactory.string_params_for(:user, best_article: article)

assert user_params["best_article"] == %{"title" => article.title}
end

test "string_params_for/2 converts has_many associations into a list of maps with strings as keys" do
article = TestFactory.build(:article, title: "Foo")

user_params = TestFactory.string_params_for(:user, articles: [article])

assert user_params["articles"] == [%{"title" => article.title}]
end

test "string_params_for/2 converts embeds_one into a map with strings as keys" do
author = %ExMachina.Author{name: "Author", salary: 1.0}

comment_params = TestFactory.string_params_for(:comment_with_embedded_assocs, author: author)

assert comment_params["author"] == %{"name" => "Author", "salary" => 1.0}
end

test "string_params_for/2 converts embeds_many into a list of maps with strings as keys" do
links = [%ExMachina.Link{url: "https://thoughtbot.com", rating: 5}, %ExMachina.Link{url: "https://github.com", rating: 4}]

comment_params = TestFactory.string_params_for(:comment_with_embedded_assocs, links: links)

assert comment_params["links"] == [
%{"url" => "https://thoughtbot.com", "rating" => 5},
%{"url" => "https://github.com", "rating" => 4}
]
end

test "params_with_assocs/2 inserts belongs_tos that are set by the factory" do
assert has_association_in_schema?(ExMachina.Article, :editor)

Expand Down