Last active
April 12, 2016 16:26
-
-
Save pikender/52c5f30c74f1a2bbff886e6ffcc6be46 to your computer and use it in GitHub Desktop.
Phoenix Router Extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Nectar.RouterExtension do | |
defmacro __using__(_opts) do | |
quote do | |
Module.register_attribute(__MODULE__, :defined_routes, accumulate: true) | |
import Nectar.RouterExtension, only: [define_route: 1] | |
@before_compile Nectar.RouterExtension | |
end | |
end | |
defmacro define_route([do: block]) do | |
route = Macro.escape(block) | |
quote bind_quoted: [route: route] do | |
Module.put_attribute(__MODULE__, :defined_routes, route) | |
end | |
end | |
defmacro __before_compile__(_env) do | |
quote do | |
defmacro mount do | |
@defined_routes | |
end | |
end | |
end | |
end | |
defmodule Nectar.ExtendRouter do | |
use Nectar.RouterExtension | |
define_route do: get "/favorites", FavoriteProducts.FavoriteController, :index | |
end | |
defmodule Nectar.Router do | |
... | |
# Add Route Here | |
# get "/favorites", FavoriteProducts.FavoriteController, :index | |
require Nectar.ExtendRouter | |
Nectar.ExtendRouter.mount | |
... | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment