A project that integrates Phoenix with Ecto, implementing all relevant protocols.
Usage
You can use phoenix_ecto in your projects in two steps:
Add it to your
mix.exsdependencies:def deps do [{:phoenix_ecto, "~> 2.0"}] endList it as your application dependency:
def application do [applications: [:logger, :phoenix_ecto]] end
The Phoenix <-> Ecto integration
Thanks to Elixir protocols, the integration between Phoenix and Ecto is simply a matter of implementing a handful of protocols. We provide the following implementations:
Phoenix.HTML.FormDataprotocol forEcto.ChangesetPhoenix.HTML.Safeprotocol forDecimal,Ecto.Date,Ecto.TimeandEcto.DateTimePlug.Exceptionprotocol for the relevant Ecto exceptionsPoison.Encoderprotocol forDecimal,Ecto.Date,Ecto.TimeandEcto.DateTime
Concurrent acceptance tests
This library also provides a plug called Phoenix.Ecto.SQL.Sandbox that allows developers to run acceptance tests concurrently. If you are not familiar with Ecto's SQL sandbox, we recommend you to first get acquainted with it by reading Ecto.Adapters.SQL.Sandbox documentation.
In the examples below, we will list the instructions of running concurrent acceptance tests with Hound but it would apply to any tool.
First add Hound as a dependency to your project as outlined in its README
Then set a flag to enable the sandbox in
config/test.exs:
```elixir
config :your_app, sql_sandbox: true
```-
And use the flag to conditionally add the plug to
lib/your_app/endpoint.ex:
```elixir
if Application.get_env(:your_app, :sql_sandbox) do
plug Phoenix.Ecto.SQL.Sandbox
end
```- Then, within an acceptance test, checkout a sandboxed connection as usual and then use your acceptance test driver to access the sandbox endpoint. This will set a cookie that will be used for connection management in following requests:
```elixir
use Hound.Helpers
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(YourApp.Repo)
Hound.start_session
navigate_to Phoenix.Ecto.SQL.Sandbox.path_for(YourApp.Repo, self())
end
```
Keep in mind phantomjs is not currently supported for concurrent transactional tests as it shares cookies between sessions.
License
Same license as Phoenix.