Permalink
Browse files

Add tests for template caching

  • Loading branch information...
1 parent 896f73c commit 1d1c52f54e8a6bd64ed62d667766607831f11c81 @josevalim josevalim committed Sep 7, 2012
Showing with 30 additions and 11 deletions.
  1. +10 −10 lib/dynamo/views.ex
  2. +19 −1 test/dynamo/views_test.exs
  3. +1 −0 test/fixtures/views/module.html.eex
View
@@ -7,17 +7,17 @@ defmodule Dynamo.Views do
end
end
- def render(query, view_paths, assigns) do
- template = Enum.find_value(view_paths, fn(x) -> x.find(query) end)
- check_template(template, query, view_paths)
- Dynamo.Views.Renderer.render(template, Keyword.put(assigns, :template, template))
- end
-
- defp check_template(nil, query, view_paths) do
- raise Dynamo.Views.TemplateNotFound, query: query, view_paths: view_paths
+ @doc """
+ Finds the given template in any of the views paths.
+ """
+ def find(query, view_paths) do
+ Enum.find_value(view_paths, fn(x) -> x.find(query) end)
end
- defp check_template(_, _, _) do
- :ok
+ @doc """
+ Renders the given template with the given assigns.
+ """
+ def render(template, assigns) do
+ Dynamo.Views.Renderer.render(template, Keyword.put(assigns, :template, template))
end
end
View
@@ -14,7 +14,25 @@ defmodule Dynamo.ViewsTest do
end
test "renders a template" do
- body = Dynamo.Views.render "hello.html", @view_paths, []
+ body = render "hello.html"
assert body == "HELLO!"
end
+
+ test "uses cached template unless it changes" do
+ module = render "module.html"
+ assert "Elixir-" <> _ = module
+
+ cached = render "module.html"
+ assert module == cached
+
+ template = File.expand_path("../../fixtures/views/module.html.eex", __FILE__)
+ File.touch!(template, { { 2030, 1, 1 }, { 0, 0, 0 } })
+
+ not_cached = render "module.html"
+ assert module != not_cached
+ end
+
+ defp render(query) do
+ Dynamo.Views.render Dynamo.Views.find(query, @view_paths), []
+ end
end
@@ -0,0 +1 @@
+<%= __MODULE__ %>

0 comments on commit 1d1c52f

Please sign in to comment.