Skip to content

Commit

Permalink
Merge branch 'master' into fixing-create-schema
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinst committed Nov 17, 2017
2 parents 0cbaf54 + 1757d1a commit 5bef74a
Show file tree
Hide file tree
Showing 19 changed files with 135 additions and 128 deletions.
2 changes: 1 addition & 1 deletion lib/triplex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ defmodule Triplex do
end

@doc """
Returns the path for your tenant migrations.
Returns the path for the tenant migrations on your `repo`.
"""
def migrations_path(repo \\ config().repo) do
if repo do
Expand Down
25 changes: 0 additions & 25 deletions lib/triplex/param_plug.ex

This file was deleted.

48 changes: 0 additions & 48 deletions lib/triplex/plug_config.ex

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ defmodule Triplex.EnsurePlug do
callback: &TenantHelper.callback/2
failure_callback: &TenantHelper.failure_callback/2
See `Triplex.PlugConfig` to check all the allowed `config` flags.
See `Triplex.EnsurePlugConfig` to check all the allowed `config` flags.
"""

import Triplex.Plug
alias Triplex.PlugConfig
alias Triplex.EnsurePlugConfig

@doc false
def init(opts), do: PlugConfig.new(opts)
def init(opts), do: struct(EnsurePlugConfig, opts)

@doc false
def call(conn, config), do: ensure_tenant(conn, config)
Expand Down
17 changes: 17 additions & 0 deletions lib/triplex/plugs/ensure_plug_config.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Triplex.EnsurePlugConfig do
@moduledoc """
This is a struct that holds the configuration for `Triplex.EnsurePlug`.
Here are the config keys allowed:
- `assign`: the name of the assign where we must save the tenant.
- `callback`: function that might be called when the plug succeeded. It
must return a connection.
- `failure_callback`: function that might be called when the plug failed.
It must return a connection.
"""

defstruct [:callback, :failure_callback, assign: :current_tenant]
end


32 changes: 32 additions & 0 deletions lib/triplex/plugs/param_plug.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule Triplex.ParamPlug do
@moduledoc """
This is a basic plug that loads the current tenant assign from a given
param.
To plug it on your router, you can use:
plug Triplex.ParamPlug,
param: :subdomain,
tenant_handler: &TenantHelper.tenant_handler/1
See `Triplex.ParamPlugConfig` to check all the allowed `config` flags.
"""

import Triplex.Plug
alias Triplex.ParamPlugConfig

@doc false
def init(opts), do: struct(ParamPlugConfig, opts)

@doc false
def call(conn, config),
do: put_tenant(conn, get_param(conn, config.param), config)

defp get_param(conn, %ParamPlugConfig{param: key}),
do: get_param(conn, key)
defp get_param(conn, key) when is_atom(key),
do: get_param(conn, Atom.to_string(key))
defp get_param(conn, key),
do: conn.params[key]
end

16 changes: 16 additions & 0 deletions lib/triplex/plugs/param_plug_config.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule Triplex.ParamPlugConfig do
@moduledoc """
This is a struct that holds all configuration for `Triplex.ParamPlug`.
Here are the config keys allowed:
- `tenant_handler`: function to handle the tenant param. Its return will
be used as the tenant.
- `assign`: the name of the assign where we must save the tenant.
- `param`: the param name to load the tenant from.
"""

defstruct [:tenant_handler, assign: :current_tenant, param: "tenant"]
end


13 changes: 8 additions & 5 deletions lib/triplex/plug.ex → lib/triplex/plugs/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ defmodule Triplex.Plug do
- `Triplex.SessionPlug` - loads the tenant from a session param
- `Triplex.SubdomainPlug` - loads the tenant from the url subdomain
- `Triplex.EnsurePlug` - ensures the current tenant is loaded and halts if not
You can also check `Triplex.PlugConfig` for the config options you have for
each plug.
"""

import Plug.Conn
Expand All @@ -21,7 +18,11 @@ defmodule Triplex.Plug do
Puts the given `tenant` as an assign on the given `conn`, but only if the
tenant is not reserved.
See `Triplex.PlugConfig` to the allowed `config` flags.
The `config` map/struct must have:
- `tenant_handler`: function to handle the tenant param. Its return will
be used as the tenant.
- `assign`: the name of the assign where we must save the tenant.
"""
def put_tenant(conn, tenant, config) do
if conn.assigns[config.assign] do
Expand All @@ -40,7 +41,9 @@ defmodule Triplex.Plug do
@doc """
Ensure the tenant is loaded, and if not, halts the `conn`.
See `Triplex.PlugConfig` to the allowed `config` flags.
The `config` map/struct must have:
- `assign`: the name of the assign where we must save the tenant.
"""
def ensure_tenant(conn, config) do
if loaded_tenant = conn.assigns[config.assign] do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ defmodule Triplex.SessionPlug do
session: :subdomain,
tenant_handler: &TenantHelper.tenant_handler/1
See `Triplex.PlugConfig` to check all the allowed `config` flags.
See `Triplex.SessionPlugConfig` to check all the allowed `config` flags.
"""

import Triplex.Plug
import Plug.Conn
alias Triplex.PlugConfig
alias Triplex.SessionPlugConfig

@doc false
def init(opts), do: PlugConfig.new(opts)
def init(opts), do: struct(SessionPlugConfig, opts)

@doc false
def call(conn, config),
Expand Down
18 changes: 18 additions & 0 deletions lib/triplex/plugs/session_plug_config.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Triplex.SessionPlugConfig do
@moduledoc """
This is a struct that holds the configuration for `Triplex.SessionPlug`.
Here are the config keys allowed:
- `tenant_handler`: function to handle the tenant param. Its return will
be used as the tenant.
- `assign`: the name of the assign where we must save the tenant.
- `session`: the session param name to load the tenant from.
"""

defstruct [
:tenant_handler,
assign: :current_tenant,
session: :tenant
]
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,25 @@ defmodule Triplex.SubdomainPlug do
endpoint: MyApp.Endpoint,
tenant_handler: &TenantHelper.tenant_handler/1
See `Triplex.PlugConfig` to check all the allowed `config` flags.
See `Triplex.SubdomainPlugConfig` to check all the allowed `config` flags.
"""

import Triplex.Plug
alias Plug.Conn
alias Triplex.PlugConfig
alias Triplex.SubdomainPlugConfig

@doc false
def init(opts), do: PlugConfig.new(opts)
def init(opts), do: struct(SubdomainPlugConfig, opts)

@doc false
def call(conn, config),
do: put_tenant(conn, get_subdomain(conn, config), config)

defp get_subdomain(_conn, %PlugConfig{endpoint: nil}) do
defp get_subdomain(_conn, %SubdomainPlugConfig{endpoint: nil}) do
nil
end
defp get_subdomain(%Conn{host: host}, %PlugConfig{endpoint: endpoint}) do
defp get_subdomain(%Conn{host: host},
%SubdomainPlugConfig{endpoint: endpoint}) do
root_host = endpoint.config(:url)[:host]
if host in [root_host, "localhost", "127.0.0.1", "0.0.0.0"] do
nil
Expand Down
19 changes: 19 additions & 0 deletions lib/triplex/plugs/subdomain_plug_config.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Triplex.SubdomainPlugConfig do
@moduledoc """
This is a struct that holds the configuration for `Triplex.SubdomainPlug`.
Here are the config keys allowed:
- `tenant_handler`: function to handle the tenant param. Its return will
be used as the tenant.
- `assign`: the name of the assign where we must save the tenant.
- `endpoint`: the Phoenix.Endpoint to get the host name to dicover the
subdomain.
"""

defstruct [
:endpoint,
:tenant_handler,
assign: :current_tenant,
]
end
27 changes: 0 additions & 27 deletions test/triplex/plug_config_test.exs

This file was deleted.

File renamed without changes.
File renamed without changes.

0 comments on commit 5bef74a

Please sign in to comment.