From f7dae62b9e9919a10ca194f54b60b8339e5665f1 Mon Sep 17 00:00:00 2001 From: Nizar Venturini Date: Tue, 19 Jul 2016 19:20:57 +0100 Subject: [PATCH] Make configurations compatible with releases 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 #77 --- configs.md | 4 ++-- lib/addict/controller.ex | 2 +- lib/addict/helper.ex | 20 ++++++++++++++++++++ lib/addict/interactors/register.ex | 4 ++-- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/configs.md b/configs.md index a86d4ad..74798c6 100644 --- a/configs.md +++ b/configs.md @@ -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: @@ -128,7 +128,7 @@ And in your configuration file: config :addict, (...), - extra_validation: &MyApp.User.validate/2 + extra_validation: {MyApp.User, :validate} ``` # CSRF Token diff --git a/lib/addict/controller.ex b/lib/addict/controller.ex index 01070fd..cde6764 100644 --- a/lib/addict/controller.ex +++ b/lib/addict/controller.ex @@ -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 diff --git a/lib/addict/helper.ex b/lib/addict/helper.ex index 9abda11..58db036 100644 --- a/lib/addict/helper.ex +++ b/lib/addict/helper.ex @@ -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 diff --git a/lib/addict/interactors/register.ex b/lib/addict/interactors/register.ex index e6cab5f..b4a8589 100644 --- a/lib/addict/interactors/register.ex +++ b/lib/addict/interactors/register.ex @@ -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)