Permalink
Browse files

Introduce #with_formats_and_variants to prevent problems with mutatin…

…g finder object
  • Loading branch information...
1 parent 9f677bf commit 2c2326e6eab4d192e361871787335ffa711af597 @lukaszx0 lukaszx0 committed Mar 14, 2014
@@ -122,11 +122,8 @@ def partial?
def template
@template ||= begin
- finder.formats = [format]
- finder.variants = [variant]
-
- finder.disable_cache do
- finder.find(logical_name, [], partial?)
+ finder.with_formats_and_variants([format], [variant]) do
+ finder.disable_cache { finder.find(logical_name, [], partial?) }
end
end
end
@@ -246,5 +246,13 @@ def with_layout_format
end
end
end
+
+ def with_formats_and_variants(new_formats, new_variants)
+ old_formats, old_variants = formats, variants
+ self.formats, self.variants = new_formats, new_variants
+ yield
+ ensure
+ self.formats, self.variants = old_formats, old_variants
+ end
end
end
@@ -40,6 +40,14 @@ def find(name, prefixes = [], partial = false, keys = [], options = {})
def disable_cache(&block)
yield
end
+
+ def with_formats_and_variants(new_formats, new_variants)
+ old_formats, old_variants = formats, variants
+ self.formats, self.variants = new_formats, new_variants
+ yield
+ ensure
+ self.formats, self.variants = old_formats, old_variants
+ end
end
class TemplateDigestorTest < ActionView::TestCase
@@ -205,6 +205,19 @@ def teardown
@lookup_context.prefixes = ["foo"]
assert_equal ["foo"], @lookup_context.prefixes
end
+
+ test "with_formats_and_variants preserves original values after execution" do
+ @lookup_context.formats = [:html]
+ @lookup_context.variants = [:phone]
+
+ @lookup_context.with_formats_and_variants([:xml], [:tablet]) do
+ assert_equal [:xml], @lookup_context.formats
+ assert_equal [:tablet], @lookup_context.variants
+ end
+
+ assert_equal [:html], @lookup_context.formats
+ assert_equal [:phone], @lookup_context.variants
+ end
end
class LookupContextWithFalseCaching < ActiveSupport::TestCase

0 comments on commit 2c2326e

Please sign in to comment.