1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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.
This commit is contained in:
Piotr Chmolowski 2014-03-08 23:09:54 +01:00
parent 45efd0ebf7
commit 025c691536
8 changed files with 63 additions and 8 deletions

View file

@ -164,6 +164,13 @@ class FunctionalCachingController < CachingController
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 @@ CACHED
@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)

View file

@ -0,0 +1,3 @@
<body>
<%= cache do %><p>PHONE</p><% end %>
</body>

View file

@ -126,7 +126,10 @@ module ActionView
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

View file

@ -165,10 +165,20 @@ module ActionView
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

View file

@ -0,0 +1 @@
Hello phone!

View file

@ -0,0 +1 @@
Hello texty phone!

View file

@ -15,10 +15,12 @@ end
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 @@ class FixtureFinder
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

View file

@ -93,6 +93,20 @@ class LookupContextTest < ActiveSupport::TestCase
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]