Skip to content

Commit

Permalink
Fix: locomotivecms#171 looping was modifing the original attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienbeau committed Jan 27, 2020
1 parent 9be88e1 commit fece1c8
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
8 changes: 4 additions & 4 deletions lib/locomotive/steam/liquid/tags/concerns/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ module Attributes
private

def parse_attributes(markup, default = {})
@attributes = default || {}
@raw_attributes = default || {}
attribute_markup = ""
if markup =~ /^ *([a-zA-Z0-9_.]*:.*)$/
attribute_markup = $1
elsif markup =~ /^[a-zA-Z0-9 _"']*, *(.*)$/
attribute_markup = $1
end
unless attribute_markup.blank?
@attributes.merge!(AttributeParser.parse(attribute_markup))
@raw_attributes.merge!(AttributeParser.parse(attribute_markup))
end
@attributes
@raw_attributes
end

def context_evaluate_array(vals)
Expand All @@ -41,7 +41,7 @@ def context_evaluate(vals)

def evaluate_attributes(context, lax: false)
@attributes = HashWithIndifferentAccess.new.tap do |hash|
attributes.each do |key, value|
raw_attributes.each do |key, value|
hash[evaluate_value(context, key, lax: lax)] = evaluate_value(context, value, lax: lax)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/locomotive/steam/liquid/tags/consume.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def initialize(tag_name, markup, options)
super

if markup =~ Syntax
@variable_name, @url, attributes = $1.to_s, ::Liquid::Expression.parse($2), $3
@variable_name, @url, _attributes = $1.to_s, ::Liquid::Expression.parse($2), $3

parse_attributes(attributes)
parse_attributes(_attributes)
else
raise ::Liquid::SyntaxError.new("Syntax Error in 'consume' - Valid syntax: consume <var> from \"<url>\" [username: value, password: value]")
end
Expand Down
8 changes: 4 additions & 4 deletions lib/locomotive/steam/liquid/tags/with_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,21 @@ def initialize(tag_name, markup, options)
# simple hash?
parse_attributes(markup) { |value| parse_attribute(value) }

if attributes.empty? && markup =~ SingleVariable
if raw_attributes.empty? && markup =~ SingleVariable
# alright, maybe we'vot got a single variable built
# with the Action liquid tag instead?
@attributes_var_name = Regexp.last_match(1)
end

if attributes.empty? && attributes_var_name.blank?
if raw_attributes.empty? && attributes_var_name.blank?
raise ::Liquid::SyntaxError.new("Syntax Error in 'with_scope' - Valid syntax: with_scope <name_1>: <value_1>, ..., <name_n>: <value_n>")
end
end

def render(context)
context.stack do
@attributes = context[attributes_var_name] || {} if attributes_var_name.present?
@attributes.transform_keys! { |key| key.to_s == '_permalink' ? '_slug' : key.to_s }
@raw_attributes = context[attributes_var_name] || {} if attributes_var_name.present?
@raw_attributes.transform_keys! { |key| key.to_s == '_permalink' ? '_slug' : key.to_s }
context['with_scope'] = evaluate_attributes(context)

# for now, no content type is assigned to this with_scope
Expand Down
7 changes: 7 additions & 0 deletions spec/unit/liquid/tags/path_to_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@

it { is_expected.to eq '/fr/a-notre-sujet' }

context 'loop on several locale from variable' do
let(:assigns) { { 'about_us' => drop, 'langs' => ['en', 'fr'] } }
let(:source) { "{% for lang in langs %}{% path_to about_us, locale: lang %}|{% endfor %}" }

it { is_expected.to eq '/about-us|/fr/a-notre-sujet|' }
end

end

end
Expand Down
9 changes: 9 additions & 0 deletions spec/unit/liquid/tags/with_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,15 @@

end

describe 'In a loop context, each scope should be evaluated correctly' do
let(:assigns) { {'list' => ['A', 'B', 'C']} }

let(:source) { "{% for key in list %}{% with_scope foo: key %}{% assign conditions = with_scope %}{% endwith_scope %}{{ conditions }}{% endfor %}" }

it { expect(output).to eq '{"foo"=>"A"}{"foo"=>"B"}{"foo"=>"C"}' }

end

end

end

0 comments on commit fece1c8

Please sign in to comment.