Permalink
Browse files

Ensure LookupContext in Digestor selects correct variant

Related to: #14242 #14243 14293

Variants passed to LookupContext#find() seem to be ignored, so
I've used the setter instead: `finder.variants = [ variant ]`.

I've also added some more test cases for variants. Hopefully this
time passing tests will mean it actually works.
  • Loading branch information...
1 parent 45efd0e commit 025c691536b22cc3f0ba802f6c303dd6f955c1c3 @pch pch committed Mar 8, 2014
@@ -164,6 +164,13 @@ def formatted_fragment_cached
end
end
+ def formatted_fragment_cached_with_variant
+ respond_to do |format|
+ format.html.phone
+ format.html
+ end
+ end
+
def fragment_cached_without_digest
end
end
@@ -242,6 +249,20 @@ def test_xml_formatted_fragment_caching
@store.read("views/test.host/functional_caching/formatted_fragment_cached/#{template_digest("functional_caching/formatted_fragment_cached", "xml")}")
end
+
+ def test_fragment_caching_with_variant
+ @request.variant = :phone
+
+ get :formatted_fragment_cached_with_variant, :format => "html", :variant => :phone
+ assert_response :success
+ expected_body = "<body>\n<p>PHONE</p>\n</body>\n"
+
+ assert_equal expected_body, @response.body
+
+ assert_equal "<p>PHONE</p>",
+ @store.read("views/test.host/functional_caching/formatted_fragment_cached_with_variant/#{template_digest("functional_caching/formatted_fragment_cached_with_variant", :html, :phone)}")
+ end
+
private
def template_digest(name, format, variant = nil)
ActionView::Digestor.digest(name: name, format: format, variant: variant, finder: @controller.lookup_context)
@@ -0,0 +1,3 @@
+<body>
+<%= cache do %><p>PHONE</p><% end %>
+</body>
@@ -126,7 +126,10 @@ def partial?
end
def template
- @template ||= finder.find(logical_name, [], partial?, formats: [ format ], variants: [ variant ])
+ @template ||= begin
+ finder.variants = [ variant ]
+ finder.find(logical_name, [], partial?, formats: [ format ])
+ end
end
def source
@@ -165,10 +165,20 @@ def cache_fragment_name(name = {}, options = nil)
def fragment_name_with_digest(name) #:nodoc:
if @virtual_path
- [
- *Array(name.is_a?(Hash) ? controller.url_for(name).split("://").last : name),
- Digestor.digest(name: @virtual_path, format: formats.last.to_sym, variant: request.variant, finder: lookup_context, dependencies: view_cache_dependencies)
- ]
+ variant = request.variant.is_a?(Array) ? request.variant.first : request.variant
+
+ options = {
+ name: @virtual_path,
+ format: formats.last.to_sym,
+ variant: variant,
+ finder: lookup_context,
+ dependencies: view_cache_dependencies
+ }
+
+ names = Array(name.is_a?(Hash) ? controller.url_for(name).split("://").last : name)
+ digest = Digestor.digest(options)
+
+ [*names, digest]
else
name
end
@@ -0,0 +1 @@
+Hello phone!
@@ -0,0 +1 @@
+Hello texty phone!
@@ -15,10 +15,12 @@ def initialize(template_path)
class FixtureFinder
FIXTURES_DIR = "#{File.dirname(__FILE__)}/../fixtures/digestor"
- attr_reader :details
+ attr_reader :details
+ attr_accessor :variants
def initialize
- @details = {}
+ @details = {}
+ @variants = []
end
def details_key
@@ -28,7 +30,7 @@ def details_key
def find(logical_name, keys, partial, options)
partial_name = partial ? logical_name.gsub(%r|/([^/]+)$|, '/_\1') : logical_name
format = options[:formats].first.to_s
- format += "+#{options[:variants].first}" if options[:variants].any?
+ format += "+#{@variants.first}" if @variants.any?
FixtureTemplate.new("digestor/#{partial_name}.#{format}.erb")
end
@@ -93,6 +93,20 @@ def teardown
assert_equal "Hey verden", template.source
end
+ test "find templates with given variants" do
+ @lookup_context.formats = [:html]
+ @lookup_context.variants = [:phone]
+
+ template = @lookup_context.find("hello_world", %w(test))
+ assert_equal "Hello phone!", template.source
+
+ @lookup_context.variants = [:phone]
+ @lookup_context.formats = [:text]
+
+ template = @lookup_context.find("hello_world", %w(test))
+ assert_equal "Hello texty phone!", template.source
+ end
+
test "found templates respects given formats if one cannot be found from template or handler" do
ActionView::Template::Handlers::Builder.expects(:default_format).returns(nil)
@lookup_context.formats = [:text]

0 comments on commit 025c691

Please sign in to comment.