Skip to content

Commit 3cb5053

Browse files
committed
Maintain backwards compatibility on sandbox API, closes #197
1 parent 95fde93 commit 3cb5053

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

lib/phoenix_ecto/sql/sandbox.ex

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ defmodule Phoenix.Ecto.SQL.Sandbox do
108108
{:cont, socket}
109109
end
110110
end
111-
111+
112112
Now, in your `my_app_web.ex` file, you can invoke this callback for all of your
113113
LiveViews if the sandbox configuration, defined at the beginning of the
114114
documentation, is enabled:
@@ -117,11 +117,11 @@ defmodule Phoenix.Ecto.SQL.Sandbox do
117117
quote do
118118
use Phoenix.LiveView
119119
# ...
120-
120+
121121
if Application.compile_env(:your_app, :sql_sandbox) do
122122
on_mount MyApp.LiveAcceptance
123123
end
124-
124+
125125
# ...
126126
end
127127
end
@@ -215,7 +215,8 @@ defmodule Phoenix.Ecto.SQL.Sandbox do
215215
header: Keyword.get(opts, :header, "user-agent"),
216216
path: get_path_info(opts[:at]),
217217
repos: List.wrap(opts[:repo]),
218-
sandbox: session_opts[:sandbox] || Ecto.Adapters.SQL.Sandbox,
218+
sandbox:
219+
session_opts[:sandbox] || {Ecto.Adapters.SQL.Sandbox, :allow, [[unallow_existing: true]]},
219220
session_opts: session_opts
220221
}
221222
end
@@ -339,8 +340,14 @@ defmodule Phoenix.Ecto.SQL.Sandbox do
339340
end
340341

341342
def allow(%{repo: repo, owner: owner}, sandbox),
342-
do: Enum.each(List.wrap(repo), &sandbox.allow(&1, owner, self(), unallow_existing: true))
343+
do: Enum.each(List.wrap(repo), &allow_sandbox(sandbox, &1, owner, self()))
343344

344345
def allow(%{}, _sandbox), do: :ok
345346
def allow(nil, _sandbox), do: :ok
347+
348+
defp allow_sandbox({m, f, args}, repo, owner, pid),
349+
do: apply(m, f, [repo, owner, pid | args])
350+
351+
defp allow_sandbox(sandbox, repo, owner, pid) when is_atom(sandbox),
352+
do: sandbox.allow(repo, owner, pid)
346353
end

test/phoenix_ecto/sql/sandbox_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ defmodule PhoenixEcto.SQL.SandboxTest do
1515
:ok
1616
end
1717

18-
def allow(repo, owner, _allowed, _opts \\ []) do
18+
def allow(repo, owner, _allowed) do
1919
send(owner, {:allowed, repo})
2020
end
2121
end

0 commit comments

Comments
 (0)