Introduce #with_formats_and_variants to prevent problems with mutating finder object

This commit is contained in:
Łukasz Strzałkowski 2014-03-14 13:40:46 +01:00
parent 9f677bf043
commit 2c2326e6ea
4 changed files with 31 additions and 5 deletions

View File

@ -122,11 +122,8 @@ module ActionView
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

View File

@ -246,5 +246,13 @@ module ActionView
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

View File

@ -40,6 +40,14 @@ class FixtureFinder
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

View File

@ -205,6 +205,19 @@ class LookupContextTest < ActiveSupport::TestCase
@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