Skip to content

Commit

Permalink
Merge pull request #199 from rcdilorenzo/master
Browse files Browse the repository at this point in the history
Add rendering of different action and/or controller
  • Loading branch information
0xAX committed May 15, 2014
2 parents 4c7c2b6 + 1537f18 commit a910311
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 15 deletions.
38 changes: 25 additions & 13 deletions lib/weber/handler/weber_req_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,32 @@ defmodule Handler.WeberReqHandler do
_ -> controller.__before__(action, conn)
end

# get response from controller
result = try do
Module.function(controller, action, 2).(getAllBinding(path, matched_path), conn)
rescue
e ->
if e.message in controller.raise_keys do
controller.render_value_for_key(e.message)
else
IO.ANSI.escape("%{red} #{e.message}\n" <> Exception.format_stacktrace(System.stacktrace)) |> IO.puts
raise e, [], System.stacktrace
end
get_response(controller, action, getAllBinding(path, matched_path), conn, req3, state)
end
end

def get_response(controller, action, data, conn, req, state) do
result = try do
Module.function(controller, action, 2).(data, conn)
rescue
e ->
if e.message in controller.raise_keys do
controller.render_value_for_key(e.message)
else
IO.ANSI.escape("%{red} #{e.message}\n" <> Exception.format_stacktrace(System.stacktrace)) |> IO.puts
raise e, [], System.stacktrace
end
# handle controller's response, see in Handler.WeberReqHandler.Result
state.handler.handle_result(result, conn, controller, Weber.Utils.capitalize(atom_to_binary(action))) |> handle_request(req3, state, {controller, action, conn})
end

result = state.handler.handle_result(result, conn, controller, Weber.Utils.capitalize(atom_to_binary(action)))
# check for action/controller redirect
case result do
{:render_other_controller, _, _} ->
[controller, action] = result |> elem(1) |> String.split("#")
{controller, _} = Code.eval_string(controller)
get_response(controller, binary_to_atom(action), result |> elem(2), conn, req, state)
_ ->
handle_request(result, req, state, {controller, action, conn})
end
end

Expand Down
13 changes: 13 additions & 0 deletions lib/weber/handler/weber_req_handler_default.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,17 @@ defmodule Handler.WeberReqHandler.Default do
{:not_found, 404, data, [{"Content-Type", "text/html"}]}
end

def request({:render_other_action, action, data}, app) do
complete_action = (atom_to_binary(app.controller) <> "#" <> atom_to_binary(action)) |> String.slice(7..-1)
request({:render_other_controller, complete_action, data}, app)
end

def request({:render_other_controller, complete_action, data}, app) do
{:render_other_controller, complete_action, data}
end

def request({:render_other_controller, controller, action, data}, app) when is_atom(controller) and is_atom(action) do
{:render_other_controller, atom_to_binary(controller) <> "#" <> atom_to_binary(action), data}
end

end
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Weber.Mixfile do
[ app: :weber,
version: "0.1.1",
name: "Weber",
elixir: "0.13.2-dev",
elixir: ">= 0.13.2",
deps: deps(Mix.env),
source_url: "https://github.com/0xAX/weber",
homepage_url: "http://0xax.github.io/weber/index.html",
Expand Down Expand Up @@ -60,4 +60,4 @@ defmodule Weber.Mixfile do
]
end

end
end
24 changes: 24 additions & 0 deletions test/controllers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ defmodule TestTestTest.Main do
{:render, [], [{"content-type", "text/html"}]}
end

def action1([binding: value], _conn) do
{:text, "Main Controller: " <> value, []}
end

end

defmodule TestTestTest.Include do
Expand Down Expand Up @@ -70,3 +74,23 @@ defmodule TestTestTest.Exceptions do
end

end


defmodule TestTestTest.Redirect do
use Weber.Controller

layout false

def render_other_action([], _conn) do
{:render_other_action, :action1, [binding: "original value"]}
end

def render_other_controller([], _conn) do
{:render_other_controller, :TestTestTest.Main, :action1, [binding: "original value"]}
end

def action1([binding: value], _conn) do
{:text, value, []}
end

end
2 changes: 2 additions & 0 deletions test/route.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ defmodule Route do
|> on("GET", "/partials", :TestTestTest.Partials, :partials)
|> on("GET", "/unauthorized", :TestTestTest.Exceptions, :unauthorized_action)
|> on("GET", "/unknown", :TestTestTest.Exceptions, :error_500_action)
|> on("GET", "/render_other_action", :TestTestTest.Redirect, :render_other_action)
|> on("GET", "/render_other_controller", :TestTestTest.Redirect, :render_other_controller)
|> redirect("GET", "/redirect", "/weber")

end
14 changes: 14 additions & 0 deletions test/weberTest/response_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,18 @@ defmodule WeberHttpResponseTest do
assert(status == 500)
assert(body == {:ok, "An unknown error occurred"})
end

test "rendering other action" do
{:ok, status, _, client} = :hackney.request(:get, 'http://localhost:8080/render_other_action', [], <<>>, [])
body = :hackney.body(client)
assert(status == 200)
assert(body == {:ok, "original value"})
end

test "rendering other controller and action" do
{:ok, status, _, client} = :hackney.request(:get, 'http://localhost:8080/render_other_controller', [], <<>>, [])
body = :hackney.body(client)
assert(status == 200)
assert(body == {:ok, "Main Controller: original value"})
end
end

0 comments on commit a910311

Please sign in to comment.