Skip to content

Commit

Permalink
Allow template handlers to store temp data.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Nov 24, 2010
1 parent e391384 commit 8624065
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
5 changes: 5 additions & 0 deletions actionpack/lib/action_view/template.rb
Expand Up @@ -184,6 +184,11 @@ def rerender(view)
end
end

# Used to store template data by template handlers.
def data
@data ||= {}
end

def inspect
@inspect ||=
if defined?(Rails.root)
Expand Down
29 changes: 27 additions & 2 deletions actionpack/test/template/lookup_context_test.rb
Expand Up @@ -180,6 +180,16 @@ def teardown

assert_not_equal template, old_template
end

test "data can be stored in cached templates" do
template = @lookup_context.find("hello_world", "test")
template.data["cached"] = "data"
assert_equal "Hello world!", template.source

template = @lookup_context.find("hello_world", "test")
assert_equal "data", template.data["cached"]
assert_equal "Hello world!", template.source
end
end

class LookupContextWithFalseCaching < ActiveSupport::TestCase
Expand All @@ -205,7 +215,7 @@ def setup
assert_equal "Bar", template.source
end

test "if no template was found in the second lookup, give it higher preference" do
test "if no template was found in the second lookup, with no cache, raise error" do
template = @lookup_context.find("foo", "test", true)
assert_equal "Foo", template.source

Expand All @@ -215,7 +225,7 @@ def setup
end
end

test "if no template was cached in the first lookup, do not use the cache in the second" do
test "if no template was cached in the first lookup, retrieval should work in the second call" do
@resolver.hash.clear
assert_raise ActionView::MissingTemplate do
@lookup_context.find("foo", "test", true)
Expand All @@ -225,4 +235,19 @@ def setup
template = @lookup_context.find("foo", "test", true)
assert_equal "Foo", template.source
end

test "data can be stored as long as template was not updated" do
template = @lookup_context.find("foo", "test", true)
template.data["cached"] = "data"
assert_equal "Foo", template.source

template = @lookup_context.find("foo", "test", true)
assert_equal "data", template.data["cached"]
assert_equal "Foo", template.source

@resolver.hash["test/_foo.erb"][1] = Time.now.utc
template = @lookup_context.find("foo", "test", true)
assert_nil template.data["cached"]
assert_equal "Foo", template.source
end
end

0 comments on commit 8624065

Please sign in to comment.