Skip to content

Commit

Permalink
Make configurations compatible with releases
Browse files Browse the repository at this point in the history
When creating a release, having functions defined via `fn` or via the
`&fun/arity` syntax, breaks the build.

Addict now requires that the hooks are defined via a tuple that defines
the Module and the target function, e.g.: `{ Module, :function}`.

Thanks to @smpallen99 for the tip!

Closes trenpixster#77
  • Loading branch information
trenpixster committed Jul 19, 2016
1 parent 299a484 commit f7dae62
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
4 changes: 2 additions & 2 deletions configs.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ And add it to the configuration:

config :addict,
(...),
post_login: &MyApp.PostLoginAction.log/3
post_login: {MyApp.PostLoginAction, :log}
```

If you want to take different flows according to the success criteria of the action, you can pattern match the arguments:
Expand Down Expand Up @@ -128,7 +128,7 @@ And in your configuration file:

config :addict,
(...),
extra_validation: &MyApp.User.validate/2
extra_validation: {MyApp.User, :validate}
```

# CSRF Token
Expand Down
2 changes: 1 addition & 1 deletion lib/addict/controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ defmodule Addict.AddictController do

defp generate_csrf_token do
if Addict.Configs.generate_csrf_token != nil do
Addict.Configs.generate_csrf_token.()
Addict.Helper.exec Addict.Configs.generate_csrf_token, []
else
""
end
Expand Down
20 changes: 20 additions & 0 deletions lib/addict/helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,24 @@ Addict Helper functions
def is_logged_in(conn) do
current_user(conn) != nil
end

@doc """
Utility helper for executing Config defined functions
"""

def exec(nil, _) do
nil
end

def exec({mod, func}, args) do
:erlang.apply mod, func, args
end

def exec(func, []) do
:erlang.apply func, []
end

def exec(func, args) do
:erlang.apply func, args
end
end
4 changes: 2 additions & 2 deletions lib/addict/interactors/register.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ defmodule Addict.Interactors.Register do
extra_validation = configs.extra_validation || fn (a,_) -> a end

{valid, errors} = ValidateUserForRegistration.call(user_params)
user_params = InjectHash.call user_params
{valid, errors} = extra_validation.({valid, errors}, user_params)
user_params = InjectHash.call user_params
{valid, errors} = Addict.Helper.exec extra_validation, [{valid, errors}, user_params]

case {valid, errors} do
{:ok, _} -> do_register(user_params, configs)
Expand Down

0 comments on commit f7dae62

Please sign in to comment.