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

Rename fields_for to params_for #98

Merged
merged 1 commit into from
Mar 4, 2016
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ create(:comment, attrs)
create_pair(:comment, attrs)
create_list(3, :comment, attrs)

# `fields_for` returns a plain map without any Ecto specific attributes.
# `params_for` returns a plain map without any Ecto specific attributes.
# This is only available when using `ExMachina.Ecto`.
fields_for(:comment, attrs)
params_for(:comment, attrs)
```

## Usage in a test
Expand Down
10 changes: 7 additions & 3 deletions lib/ex_machina/ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ defmodule ExMachina.Ecto do

@repo unquote(repo)

def params_for(factory_name, attrs \\ %{}) do
ExMachina.Ecto.params_for(__MODULE__, factory_name, attrs)
end

def fields_for(factory_name, attrs \\ %{}) do
ExMachina.Ecto.fields_for(__MODULE__, factory_name, attrs)
raise "fields_for/2 has been renamed to params_for/2."
end

def save_record(record) do
Expand Down Expand Up @@ -61,9 +65,9 @@ defmodule ExMachina.Ecto do
end

# Returns %{name: "John Doe", admin: true}
fields_for(:user, admin: true)
params_for(:user, admin: true)
"""
def fields_for(module, factory_name, attrs \\ %{}) do
def params_for(module, factory_name, attrs \\ %{}) do
module.build(factory_name, attrs)
|> drop_ecto_fields
end
Expand Down
8 changes: 4 additions & 4 deletions test/ex_machina/ecto_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,17 @@ defmodule ExMachina.EctoTest do
end
end

test "fields_for/2 removes Ecto specific fields" do
assert Factory.fields_for(:user) == %{
test "params_for/2 removes Ecto specific fields" do
assert Factory.params_for(:user) == %{
id: nil,
name: "John Doe",
admin: false
}
end

test "fields_for/2 raises when passed a map" do
test "params_for/2 raises when passed a map" do
assert_raise ArgumentError, fn ->
Factory.fields_for(:user_map)
Factory.params_for(:user_map)
end
end

Expand Down