the path_to liquid tag accepts variables in the options

did committed Apr 20, 2016
commit aed1cddd2b6eb78d5759007a62b5db56f9bb069e
Showing 2 changed files with 34 additions and 4 deletions
locomotive/steam/liquid/tags/concerns/path.rb b/lib/locomotive/steam/liquid/tags/concerns/path.rb +9 -3
@@ @@ -74,7 +74,7 @@ module Locomotive
def _retrieve_templatized_page_drop_from(drop)
entry = drop.send(:_source)
- if page = repository.template_for(entry, @path_options[:with])
+ if page = repository.template_for(entry, template_slug)
page.to_liquid.tap { |d| d.context = @context }
end
end
@@ @@ -84,7 +84,11 @@ module Locomotive
end
def locale
- @path_options[:locale] || @locale
+ @path_options[:locale] || @raw_locale || @locale
+ end
+
+ def template_slug
+ @path_options[:with] || @raw_with
end
def set_vars_from_context(context)
@@ @@ -101,7 +105,9 @@ module Locomotive
def make_options_compatible_with_previous_version(options)
if options
%w(with locale).each do |name|
- options.gsub!(/#{name}: ([\w-]+)/, name + ': "\1"')
+ if options =~ /#{name}: ([\w-]+)/
+ instance_variable_set(:"@raw_#{name}", $1);
+ end
end
end
end
spec/unit/liquid/tags/path_to_spec.rb +25 -1
@@ @@ -68,6 +68,15 @@ describe Locomotive::Steam::Liquid::Tags::PathTo do
let(:source) { "{% path_to about_us, locale: 'fr' %}" }
it { is_expected.to eq '/fr/a-notre-sujet' }
+ context 'locale is a variable' do
+
+ let(:assigns) { { 'about_us' => drop, 'language' => 'fr' } }
+ let(:source) { "{% path_to about_us, locale: language %}" }
+
+ it { is_expected.to eq '/fr/a-notre-sujet' }
+
+ end
+
end
end
@@ @@ -82,7 +91,7 @@ describe Locomotive::Steam::Liquid::Tags::PathTo do
let(:source) { '{% path_to article %}' }
before do
- expect(services.repositories.page).to receive(:template_for).with(entry, nil).and_return(page)
+ allow(services.repositories.page).to receive(:template_for).with(entry, nil).and_return(page)
allow(page).to receive(:to_liquid).and_return(drop)
end
@@ @@ -95,6 +104,21 @@ describe Locomotive::Steam::Liquid::Tags::PathTo do
end
+ context 'and a different template' do
+
+ let(:archive) { liquid_instance_double('ArticleTemplate', title: 'Template of an article', handle: 'article', localized_attributes: { fullpath: true }, fullpath: { en: 'my-archives/content_type_template', fr: 'mes-archives/content_type_template' }, content_entry: entry_drop.send(:_source), templatized?: true) }
+ let(:drop) { Locomotive::Steam::Liquid::Drops::Page.new(archive) }
+
+ before do
+ allow(services.repositories.page).to receive(:template_for).with(entry, 'archives').and_return(archive)
+ allow(archive).to receive(:to_liquid).and_return(drop)
+ end
+
+ let(:source) { "{% path_to article, with: archives, locale: fr %}" }
+ it { is_expected.to eq '/fr/mes-archives/bonjour-monde' }
+
+ end
+
end
end