-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pass opts to Repo.insert! (add function-level opts to strategies) (#411)
What changed? ============= We add the ability for a strategy to receive more options as a third argument. These options could be a function-level override of the strategy's options, or some other function-level options that we want to pass to the underlying strategy implementation. For example, the EctoStrategy can use those options and pass them to the `Repo.insert/2` function: ```elixir insert(:user, [name: "gandalf"], returning: true) build(:user, name: "gandalf") |> insert(returning: true) ``` That allows people to have more control over inserting the factory into the database. For example, we can now automatically get db-generated values by passing `returning: true`. Or we can use `prefix` or `on_conflict`. For a full list of insert options, see [Repo.insert docs]. [Repo.insert docs]: https://hexdocs.pm/ecto/Ecto.Repo.html#c:insert/2-options Known less-than-good ergonomics ------------------------------- Because both the second and third arguments are lists or maps, we need to add the surrounding keyword list `[]` to the second argument when we're using `insert/3`. In the worst case, when we don't pass args to the factory but we pass options to the repo, we're required to define an empty list: ```elixir insert(:user, [], returning: true) ``` We also have to wrap previously unwrapped lists _when_ we use the third opts argument: ```elixir insert(:user, [name: "Aragorn"], returning: true) ``` That's just the nature of having two arguments that are both lists or maps (factory args and repo opts). It reads more nicely when it's an already built struct: ```elixir build(:user) |> insert(returning :true) build(:user, name: "james") |> insert(returning :true) insert(%User{}, returning: true) ``` Also available for [strategy_func]_pair and _list functions ------------------------------------------------------------- We also update the strategy's _pair and _list function generators to allow for `opts` to be passed, since presumably users might want to do something like this: ```elixir insert_pair(:user, [name: "jane"], returning: true) ``` Current strategies in the wild that don't support this will not break. If they implement a `handle_[function_name]/3`, then they can also accept the third arguments, and their users can pass the third argument. Otherwise, they can continue using the strategies as they already have.
- Loading branch information
Showing
8 changed files
with
186 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters