Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' of github.com:evanmiller/ChicagoBoss

  • Loading branch information...
commit 1bf207623f69af3e59a352bf2d65f03ead3ee1c2 2 parents eea58f1 + 871f213
@evanmiller evanmiller authored
Showing with 47 additions and 3 deletions.
  1. +7 −2 README_ELIXIR
  2. +39 −0 priv/web_controller.ex
  3. +1 −1  src/boss/boss_mail.erl
View
9 README_ELIXIR
@@ -51,11 +51,16 @@ should NOT be prefixed with the application name. Example:
Open up the controller and write a basic module, like:
defmodule MyApplication.PuppyController do
- import Boss.WebController
+ use Boss.WebController
get :index, [] do
{:output, "Hello, world!"}
end
+
+ get about, [] do
+ {:output, "Hello, world!"}
+ end
+
end
To test it out, start the server and visit:
@@ -127,5 +132,5 @@ This is a comment:
Of course, you're free to continue using Django templates with extension ".dtl"
if you prefer a dedicated template language.
-
+
Enjoy!
View
39 priv/web_controller.ex
@@ -1,4 +1,13 @@
defmodule Boss.WebController do
+
+ defmacro __using__(_) do
+ quote do
+ Module.register_attribute __MODULE__, :__routes__, persist: false, accumulate: true
+ @before_compile unquote(__MODULE__)
+ import unquote(__MODULE__)
+ end
+ end
+
defmacro get(action, tokens, block) do
handle(:GET, action, tokens, block)
end
@@ -68,14 +77,44 @@ defmodule Boss.WebController do
end
defp handle(method, action, tokens, block) do
+ action = to_action(action)
+ route = {action, to_route_tokens(tokens)}
quote do
+ @__routes__ unquote(route)
def unquote(action)(var!(req), var!(session_id), unquote(method), unquote(tokens)), unquote(block)
end
end
defp handle(method, action, tokens, block, info) do
+ action = to_action(action)
+ route = {action, to_route_tokens(tokens)}
quote do
+ @__routes__ unquote(route)
def unquote(action)(var!(req), var!(session_id), unquote(method), unquote(tokens), unquote(info)), unquote(block)
end
end
+
+ defp to_action({atom, _, _}), do: atom
+ defp to_action(action), do: action
+
+ defp to_route_tokens({_, _, nil}), do: []
+ defp to_route_tokens(tokens), do: lc token inlist tokens, do: to_route_token(token)
+
+ defp to_route_token(token) do
+ case token do
+ {:|, _, [token, _]} -> to_route_token(token)
+ {name, _, _} -> name
+ value -> value
+ end
+ end
+
+ defmacro __before_compile__(_) do
+ quote do
+ def _routes(_), do: @__routes__
+ # simulate a new && instance functions
+ def new(req), do: {__MODULE__, req}
+ def instance(req), do: {__MODULE__, req}
+ end
+ end
+
end
View
2  src/boss/boss_mail.erl
@@ -118,7 +118,7 @@ render_view(App, {Action, Extension}, Variables, ContentLanguage) ->
TranslatorPid = boss_web:translator_pid(App),
RouterPid = boss_web:router_pid(App),
case boss_load:load_view_if_dev(App, ViewPath, TranslatorPid) of
- {ok, ViewModule} ->
+ {ok, ViewModule, _ViewAdapter} ->
TranslationFun = boss_translator:fun_for(TranslatorPid, ContentLanguage),
ViewModule:render([{"_lang", ContentLanguage}|Variables],
[{translation_fun, TranslationFun}, {locale, ContentLanguage},
Please sign in to comment.
Something went wrong with that request. Please try again.