Skip to content

Commit

Permalink
Merge pull request #49 from vjustov/render
Browse files Browse the repository at this point in the history
[WIP] Avoids having to specify the view folder in render/2
  • Loading branch information
slogsdon committed Dec 20, 2014
2 parents ee62105 + 15806d9 commit 30ef97b
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 9 deletions.
31 changes: 30 additions & 1 deletion lib/sugar/controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,26 @@ defmodule Sugar.Controller do
`Plug.Conn`
"""
def render(conn, template_key, assigns \\ [], opts \\ []) do
def render(conn) do
template = build_template_key(conn)
render_view(conn, template, [], [])
end

def render(conn, template, assigns \\ [], opts \\ [])

def render(conn, template, assigns, opts) when is_atom(template) do
template = build_template_key(conn,template)
render_view(conn, template, assigns, opts)
end

def render(conn, template, assigns, opts) when is_binary(template) do
template = build_template_key(conn,template)
render_view(conn, template, assigns, opts)
end

defp render_view(conn, template_key, assigns, opts) do
opts = [status: 200] |> Keyword.merge opts

html = Sugar.Views.Finder.one("lib/#{Mix.Project.config[:app]}/views", template_key)
|> Sugar.Templates.render(assigns)

Expand Down Expand Up @@ -250,6 +268,17 @@ defmodule Sugar.Controller do
|> send_resp_if_not_sent(opts[:status], "")
end

defp build_template_key(conn, template \\ nil) do
template = template || conn.private.action

controller = "#{Map.get(conn.private, :controller, "")}"
|> String.split(".")
|> List.last
|> String.downcase

"#{controller}/#{template}"
end

defp put_resp_header_if_not_sent(%Plug.Conn{state: :sent} = conn, _, _) do
conn
end
Expand Down
4 changes: 4 additions & 0 deletions lib/sugar/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ defmodule Sugar.Router do
binding = binding()
conn = var!(conn)

conn = update_in conn.private,
&(&1 |> Map.put(:controller, unquote(controller))
|> Map.put(:action, unquote(action)))

# pass off to controller action
call_controller_action conn, unquote(controller), unquote(action), binding
end
Expand Down
17 changes: 9 additions & 8 deletions lib/sugar/views/finder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ defmodule Sugar.Views.Finder do
"""
def all(root) do
Path.wildcard("#{root}/**/*.*")
|> Enum.map(fn (path) ->
Path.relative_to(path, root)
|> build(path)
end)
|> Enum.map(fn (path) -> build(path) end)
end

@doc """
Expand All @@ -37,17 +34,21 @@ defmodule Sugar.Views.Finder do
"""
def one(root, key) do
path = Path.join(root, key)
if Path.extname(path) == "", do: path = path <> ".*"
path = path |> Path.wildcard
|> List.first
|| ""

if path do
build(key, path)
if File.exists?(path) do
build(path)
else
{ :error, :notfound }
end
end

defp build(key, path) do
defp build(path) do
%Sugar.Templates.Template{
key: key,
key: Path.basename(path),
engine: path |> get_ext |> get_engine,
source: File.read!(path),
updated_at: File.stat!(path).mtime
Expand Down
12 changes: 12 additions & 0 deletions test/sugar/controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ defmodule Sugar.ControllerTest do
destroy_template
end

test "render/4 with a symbol" do
File.mkdir("lib/sugar/views/tests/")
File.touch("lib/sugar/views/tests/index.html.eex")
conn = conn(:get, "/")
|> Map.put(:state, :set)
|> render("tests/index.html.eex", [], [content_type: "text/html"])

assert conn.state === :sent
assert get_resp_header(conn, "content-type") === ["text/html; charset=utf-8"]
File.rm!("lib/sugar/views/tests/index.html.eex")
end

test "halt!/2 without opts" do
conn = conn(:get, "/")
|> Map.put(:state, :set)
Expand Down
20 changes: 20 additions & 0 deletions test/sugar/views/finder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,24 @@ defmodule Sugar.Views.FinderTest do
assert template.engine === expected.engine
assert template.source === expected.source
end

test "one/2 without extension" do
template = Sugar.Views.Finder.one("test/fixtures/view_finder", "index")
expected = %Sugar.Templates.Template{
engine: Sugar.Templates.Engines.EEx,
key: "index.html.eex",
source: ""
}

assert template.key === expected.key
assert template.engine === expected.engine
assert template.source === expected.source
end

test "one/2 when view does not exist" do
template = Sugar.Views.Finder.one("test/fixtures/view_finder", "not_found.html.eex")
expected = { :error, :notfound }

assert template === expected
end
end

0 comments on commit 30ef97b

Please sign in to comment.