Skip to content

Commit ea41395

Browse files
authored
Making the plug dependency optional (#50)
* Making the plug dependency optional * Fixing tests for migrations path * Getting a better coverage
1 parent 26c1589 commit ea41395

File tree

9 files changed

+172
-168
lines changed

9 files changed

+172
-168
lines changed

lib/mix/tasks/triplex.gen.migration.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ defmodule Mix.Tasks.Triplex.Gen.Migration do
6969
"#{y}#{pad(m)}#{pad(d)}#{pad(hh)}#{pad(mm)}#{pad(ss)}"
7070
end
7171

72-
defp pad(i) when i < 10, do: << ?0, ?0 + i >>
73-
defp pad(i), do: to_string(i)
72+
defp pad(i), do: i |> to_string() |> String.pad_leading(2, "0")
7473

7574
embed_template :migration, """
7675
defmodule <%= inspect @mod %> do

lib/triplex/plugs/ensure_plug.ex

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
defmodule Triplex.EnsurePlug do
2-
@moduledoc """
3-
This is a basic plug that ensure the tenant is loaded.
1+
if Code.ensure_loaded?(Plug) do
2+
defmodule Triplex.EnsurePlug do
3+
@moduledoc """
4+
This is a basic plug that ensure the tenant is loaded.
45
5-
To plug it on your router, you can use:
6+
To plug it on your router, you can use:
67
7-
plug Triplex.EnsurePlug,
8-
callback: &TenantHelper.callback/2
9-
failure_callback: &TenantHelper.failure_callback/2
8+
plug Triplex.EnsurePlug,
9+
callback: &TenantHelper.callback/2
10+
failure_callback: &TenantHelper.failure_callback/2
1011
11-
See `Triplex.EnsurePlugConfig` to check all the allowed `config` flags.
12-
"""
12+
See `Triplex.EnsurePlugConfig` to check all the allowed `config` flags.
13+
"""
1314

14-
import Triplex.Plug
15-
alias Triplex.EnsurePlugConfig
15+
import Triplex.Plug
16+
alias Triplex.EnsurePlugConfig
1617

17-
@doc false
18-
def init(opts), do: struct(EnsurePlugConfig, opts)
18+
@doc false
19+
def init(opts), do: struct(EnsurePlugConfig, opts)
1920

20-
@doc false
21-
def call(conn, config), do: ensure_tenant(conn, config)
21+
@doc false
22+
def call(conn, config), do: ensure_tenant(conn, config)
23+
end
2224
end
23-

lib/triplex/plugs/param_plug.ex

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
1-
defmodule Triplex.ParamPlug do
2-
@moduledoc """
3-
This is a basic plug that loads the current tenant assign from a given
4-
param.
5-
6-
To plug it on your router, you can use:
7-
8-
plug Triplex.ParamPlug,
9-
param: :subdomain,
10-
tenant_handler: &TenantHelper.tenant_handler/1
11-
12-
See `Triplex.ParamPlugConfig` to check all the allowed `config` flags.
13-
"""
14-
15-
import Triplex.Plug
16-
alias Triplex.ParamPlugConfig
17-
18-
@doc false
19-
def init(opts), do: struct(ParamPlugConfig, opts)
20-
21-
@doc false
22-
def call(conn, config),
23-
do: put_tenant(conn, get_param(conn, config), config)
24-
25-
defp get_param(conn, %ParamPlugConfig{param: key}),
26-
do: get_param(conn, key)
27-
defp get_param(conn, key) when is_atom(key),
28-
do: get_param(conn, Atom.to_string(key))
29-
defp get_param(conn, key),
30-
do: conn.params[key]
1+
if Code.ensure_loaded?(Plug) do
2+
defmodule Triplex.ParamPlug do
3+
@moduledoc """
4+
This is a basic plug that loads the current tenant assign from a given
5+
param.
6+
7+
To plug it on your router, you can use:
8+
9+
plug Triplex.ParamPlug,
10+
param: :subdomain,
11+
tenant_handler: &TenantHelper.tenant_handler/1
12+
13+
See `Triplex.ParamPlugConfig` to check all the allowed `config` flags.
14+
"""
15+
16+
import Triplex.Plug
17+
alias Triplex.ParamPlugConfig
18+
19+
@doc false
20+
def init(opts), do: struct(ParamPlugConfig, opts)
21+
22+
@doc false
23+
def call(conn, config),
24+
do: put_tenant(conn, get_param(conn, config), config)
25+
26+
defp get_param(conn, %ParamPlugConfig{param: key}),
27+
do: get_param(conn, key)
28+
defp get_param(conn, key) when is_atom(key),
29+
do: get_param(conn, Atom.to_string(key))
30+
defp get_param(conn, key),
31+
do: conn.params[key]
32+
end
3133
end
32-

lib/triplex/plugs/plug.ex

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,69 @@
1-
defmodule Triplex.Plug do
2-
@moduledoc """
3-
This module have some basic functions for our triplex plugs.
1+
if Code.ensure_loaded?(Plug) do
2+
defmodule Triplex.Plug do
3+
@moduledoc """
4+
This module have some basic functions for our triplex plugs.
45
5-
The plugs we have for now are:
6+
The plugs we have for now are:
67
7-
- `Triplex.ParamPlug` - loads the tenant from a body or query param
8-
- `Triplex.SessionPlug` - loads the tenant from a session param
9-
- `Triplex.SubdomainPlug` - loads the tenant from the url subdomain
10-
- `Triplex.EnsurePlug` - ensures the current tenant is loaded and halts if not
11-
"""
8+
- `Triplex.ParamPlug` - loads the tenant from a body or query param
9+
- `Triplex.SessionPlug` - loads the tenant from a session param
10+
- `Triplex.SubdomainPlug` - loads the tenant from the url subdomain
11+
- `Triplex.EnsurePlug` - ensures the current tenant is loaded and halts if not
12+
"""
1213

13-
import Plug.Conn
14+
import Plug.Conn
1415

15-
@raw_tenant_assign :raw_current_tenant
16+
@raw_tenant_assign :raw_current_tenant
1617

17-
@doc """
18-
Puts the given `tenant` as an assign on the given `conn`, but only if the
19-
tenant is not reserved.
18+
@doc """
19+
Puts the given `tenant` as an assign on the given `conn`, but only if the
20+
tenant is not reserved.
2021
21-
The `config` map/struct must have:
22+
The `config` map/struct must have:
2223
23-
- `tenant_handler`: function to handle the tenant param. Its return will
24-
be used as the tenant.
25-
- `assign`: the name of the assign where we must save the tenant.
26-
"""
27-
def put_tenant(conn, tenant, config) do
28-
if conn.assigns[config.assign] do
29-
conn
30-
else
31-
conn = assign(conn, @raw_tenant_assign, tenant)
32-
tenant = tenant_handler(tenant, config.tenant_handler)
33-
if Triplex.reserved_tenant?(tenant) do
24+
- `tenant_handler`: function to handle the tenant param. Its return will
25+
be used as the tenant.
26+
- `assign`: the name of the assign where we must save the tenant.
27+
"""
28+
def put_tenant(conn, tenant, config) do
29+
if conn.assigns[config.assign] do
3430
conn
3531
else
36-
assign(conn, config.assign, tenant)
32+
conn = assign(conn, @raw_tenant_assign, tenant)
33+
tenant = tenant_handler(tenant, config.tenant_handler)
34+
if Triplex.reserved_tenant?(tenant) do
35+
conn
36+
else
37+
assign(conn, config.assign, tenant)
38+
end
3739
end
3840
end
39-
end
4041

41-
@doc """
42-
Ensure the tenant is loaded, and if not, halts the `conn`.
42+
@doc """
43+
Ensure the tenant is loaded, and if not, halts the `conn`.
4344
44-
The `config` map/struct must have:
45+
The `config` map/struct must have:
4546
46-
- `assign`: the name of the assign where we must save the tenant.
47-
"""
48-
def ensure_tenant(conn, config) do
49-
if loaded_tenant = conn.assigns[config.assign] do
50-
callback(conn, loaded_tenant, config.callback)
51-
else
52-
conn
53-
|> callback(conn.assigns[@raw_tenant_assign], config.failure_callback)
54-
|> halt()
47+
- `assign`: the name of the assign where we must save the tenant.
48+
"""
49+
def ensure_tenant(conn, config) do
50+
if loaded_tenant = conn.assigns[config.assign] do
51+
callback(conn, loaded_tenant, config.callback)
52+
else
53+
conn
54+
|> callback(conn.assigns[@raw_tenant_assign], config.failure_callback)
55+
|> halt()
56+
end
5557
end
56-
end
5758

58-
defp tenant_handler(tenant, nil),
59-
do: tenant
60-
defp tenant_handler(tenant, handler) when is_function(handler),
61-
do: handler.(tenant)
59+
defp tenant_handler(tenant, nil),
60+
do: tenant
61+
defp tenant_handler(tenant, handler) when is_function(handler),
62+
do: handler.(tenant)
6263

63-
defp callback(conn, _, nil),
64-
do: conn
65-
defp callback(conn, tenant, callback) when is_function(callback),
66-
do: callback.(conn, tenant)
64+
defp callback(conn, _, nil),
65+
do: conn
66+
defp callback(conn, tenant, callback) when is_function(callback),
67+
do: callback.(conn, tenant)
68+
end
6769
end
68-

lib/triplex/plugs/session_plug.ex

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
defmodule Triplex.SessionPlug do
2-
@moduledoc """
3-
This is a basic plug that loads the current tenant assign from a given
4-
value set on session.
1+
if Code.ensure_loaded?(Plug) do
2+
defmodule Triplex.SessionPlug do
3+
@moduledoc """
4+
This is a basic plug that loads the current tenant assign from a given
5+
value set on session.
56
6-
To plug it on your router, you can use:
7+
To plug it on your router, you can use:
78
8-
plug Triplex.SessionPlug,
9-
session: :subdomain,
10-
tenant_handler: &TenantHelper.tenant_handler/1
9+
plug Triplex.SessionPlug,
10+
session: :subdomain,
11+
tenant_handler: &TenantHelper.tenant_handler/1
1112
12-
See `Triplex.SessionPlugConfig` to check all the allowed `config` flags.
13-
"""
13+
See `Triplex.SessionPlugConfig` to check all the allowed `config` flags.
14+
"""
1415

15-
import Triplex.Plug
16-
import Plug.Conn
17-
alias Triplex.SessionPlugConfig
16+
import Triplex.Plug
17+
import Plug.Conn
18+
alias Triplex.SessionPlugConfig
1819

19-
@doc false
20-
def init(opts), do: struct(SessionPlugConfig, opts)
20+
@doc false
21+
def init(opts), do: struct(SessionPlugConfig, opts)
2122

22-
@doc false
23-
def call(conn, config),
24-
do: put_tenant(conn, get_session(conn, config.session), config)
23+
@doc false
24+
def call(conn, config),
25+
do: put_tenant(conn, get_session(conn, config.session), config)
26+
end
2527
end
26-
Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1-
defmodule Triplex.SubdomainPlug do
2-
@moduledoc """
3-
This is a basic plug that loads the current tenant assign from a given
4-
value set on subdomain.
1+
if Code.ensure_loaded?(Plug) do
2+
defmodule Triplex.SubdomainPlug do
3+
@moduledoc """
4+
This is a basic plug that loads the current tenant assign from a given
5+
value set on subdomain.
56
6-
To plug it on your router, you can use:
7+
To plug it on your router, you can use:
78
8-
plug Triplex.SubdomainPlug,
9-
endpoint: MyApp.Endpoint,
10-
tenant_handler: &TenantHelper.tenant_handler/1
9+
plug Triplex.SubdomainPlug,
10+
endpoint: MyApp.Endpoint,
11+
tenant_handler: &TenantHelper.tenant_handler/1
1112
12-
See `Triplex.SubdomainPlugConfig` to check all the allowed `config` flags.
13-
"""
13+
See `Triplex.SubdomainPlugConfig` to check all the allowed `config` flags.
14+
"""
1415

15-
import Triplex.Plug
16-
alias Plug.Conn
17-
alias Triplex.SubdomainPlugConfig
16+
import Triplex.Plug
17+
alias Plug.Conn
18+
alias Triplex.SubdomainPlugConfig
1819

19-
@doc false
20-
def init(opts), do: struct(SubdomainPlugConfig, opts)
20+
@doc false
21+
def init(opts), do: struct(SubdomainPlugConfig, opts)
2122

22-
@doc false
23-
def call(conn, config),
24-
do: put_tenant(conn, get_subdomain(conn, config), config)
23+
@doc false
24+
def call(conn, config),
25+
do: put_tenant(conn, get_subdomain(conn, config), config)
2526

26-
defp get_subdomain(_conn, %SubdomainPlugConfig{endpoint: nil}) do
27-
nil
28-
end
29-
defp get_subdomain(%Conn{host: host},
30-
%SubdomainPlugConfig{endpoint: endpoint}) do
31-
root_host = endpoint.config(:url)[:host]
32-
if host in [root_host, "localhost", "127.0.0.1", "0.0.0.0"] do
27+
defp get_subdomain(_conn, %SubdomainPlugConfig{endpoint: nil}) do
3328
nil
34-
else
35-
String.replace(host, ~r/.?#{root_host}/, "")
29+
end
30+
defp get_subdomain(%Conn{host: host},
31+
%SubdomainPlugConfig{endpoint: endpoint}) do
32+
root_host = endpoint.config(:url)[:host]
33+
if host in [root_host, "localhost", "127.0.0.1", "0.0.0.0"] do
34+
nil
35+
else
36+
String.replace(host, ~r/.?#{root_host}/, "")
37+
end
3638
end
3739
end
3840
end
39-
40-

mix.exs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ defmodule Triplex.Mixfile do
4949
defp deps do
5050
[
5151
{:ecto, "~> 2.1"},
52-
{:plug, "~> 1.3"},
5352
{:postgrex, ">= 0.11.0"},
5453

54+
{:plug, "~> 1.3", optional: true},
55+
5556
{:ex_doc, ">= 0.0.0", only: :dev},
5657

5758
{:inch_ex, only: :docs},
@@ -68,9 +69,9 @@ defmodule Triplex.Mixfile do
6869
# See the documentation for `Mix` for more info on aliases.
6970
defp aliases do
7071
["db.migrate": ["ecto.migrate", "triplex.migrate"],
71-
"test.reset": ["ecto.drop", "ecto.create", "db.migrate"],
72-
"test.cover": &run_default_coverage/1,
73-
"test.cover.html": &run_html_coverage/1]
72+
"test.reset": ["ecto.drop", "ecto.create", "db.migrate"],
73+
"test.cover": &run_default_coverage/1,
74+
"test.cover.html": &run_html_coverage/1]
7475
end
7576

7677
defp package do
@@ -86,11 +87,11 @@ defmodule Triplex.Mixfile do
8687

8788
defp preferred_cli_env do
8889
["coveralls": :test,
89-
"coveralls.travis": :test,
90-
"coveralls.detail": :test,
91-
"coveralls.post": :test,
92-
"coveralls.html": :test,
93-
"test.reset": :test]
90+
"coveralls.travis": :test,
91+
"coveralls.detail": :test,
92+
"coveralls.post": :test,
93+
"coveralls.html": :test,
94+
"test.reset": :test]
9495
end
9596

9697
defp run_default_coverage(args), do: run_coverage("coveralls", args)

0 commit comments

Comments
 (0)