Skip to content

Commit

Permalink
Add syntax highlighting in README
Browse files Browse the repository at this point in the history
Adds syntax highlighting in README for elixir code snippets in delayed
evaluation section. It also adds a little note that the delayed factory
is not an inserted record.
  • Loading branch information
germsvel committed Dec 24, 2020
1 parent 489cdd3 commit 6dd7079
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,18 +170,24 @@ string_params_with_assocs(:comment, attrs)
`build/2` is a function call. As such, it gets evaluated immediately. So this
code:

insert_pair(:account, user: build(:user))
```elixir
insert_pair(:account, user: build(:user))
```

Is equivalent to this:

user = build(:user)
insert_pair(:account, user: user) # same user for both accounts
```elixir
user = build(:user)
insert_pair(:account, user: user) # same user for both accounts
```

Sometimes that presents a problem. Consider the following factory:

def user_factory do
%{name: "Gandalf", email: sequence(:email, "gandalf#{&1}@istari.com")}
end
```elixir
def user_factory do
%{name: "Gandalf", email: sequence(:email, "gandalf#{&1}@istari.com")}
end
```

If you want to build a separate `user` per `account`, then calling
`insert_pair(:account, user: build(:user))` will not give you the desired
Expand All @@ -190,20 +196,29 @@ result.
In those cases, you can delay the execution of the factory by passing it as an
anonymous function:

insert_pair(:account, user: fn -> build(:user) end)
```elixir
insert_pair(:account, user: fn -> build(:user) end)
```

You can also do that in a factory definition:

def account_factory do
%{user: fn -> build(:user) end}
end
```elixir
def account_factory do
%{user: fn -> build(:user) end}
end
```

You can even accept the parent record as an argument to the function:

def account_factory do
%{user: fn account -> build(:user, vip: account.premium) end}
end
```elixir
def account_factory do
%{user: fn account -> build(:user, vip: account.premium) end}
end
```

Note that the `account` passed to the anonymous function is only the struct
after it's built. It's not an inserted record. Thus, it does not have data that
is only accessible after being inserted into the database (e.g. `id`).

## Full control of factory

Expand Down

0 comments on commit 6dd7079

Please sign in to comment.