with_scope did not resolve (ie the Liquid way) correctly the values
This commit is contained in:
parent
effce313bf
commit
1f49eca09f
@ -55,7 +55,7 @@ PATH
|
|||||||
PATH
|
PATH
|
||||||
remote: ../gems/custom_fields
|
remote: ../gems/custom_fields
|
||||||
specs:
|
specs:
|
||||||
custom_fields (2.0.0.rc5)
|
custom_fields (2.0.0.rc6)
|
||||||
activesupport (~> 3.2.1)
|
activesupport (~> 3.2.1)
|
||||||
carrierwave-mongoid (~> 0.1.3)
|
carrierwave-mongoid (~> 0.1.3)
|
||||||
mongoid (~> 2.4.5)
|
mongoid (~> 2.4.5)
|
||||||
|
@ -1,11 +1,25 @@
|
|||||||
module Locomotive
|
module Locomotive
|
||||||
module Liquid
|
module Liquid
|
||||||
module Tags
|
module Tags
|
||||||
|
|
||||||
|
# Filter a collection
|
||||||
|
#
|
||||||
|
# Usage:
|
||||||
|
#
|
||||||
|
# {% with_scope main_developer: 'John Doe', active: true %}
|
||||||
|
# {% for project in contents.projects %}
|
||||||
|
# {{ project.name }}
|
||||||
|
# {% endfor %}
|
||||||
|
# {% endwith_scope %}
|
||||||
|
#
|
||||||
|
|
||||||
class WithScope < ::Liquid::Block
|
class WithScope < ::Liquid::Block
|
||||||
|
|
||||||
|
TagAttributes = /(\w+|\w+\.\w+)\s*\:\s*(#{::Liquid::QuotedFragment})/
|
||||||
|
|
||||||
def initialize(tag_name, markup, tokens, context)
|
def initialize(tag_name, markup, tokens, context)
|
||||||
@attributes = {}
|
@attributes = HashWithIndifferentAccess.new
|
||||||
markup.scan(::Liquid::TagAttributes) do |key, value|
|
markup.scan(TagAttributes) do |key, value|
|
||||||
@attributes[key] = value
|
@attributes[key] = value
|
||||||
end
|
end
|
||||||
super
|
super
|
||||||
@ -22,13 +36,7 @@ module Locomotive
|
|||||||
|
|
||||||
def decode(attributes, context)
|
def decode(attributes, context)
|
||||||
attributes.each_pair do |key, value|
|
attributes.each_pair do |key, value|
|
||||||
attributes[key] = (case value
|
attributes[key] = context[value]
|
||||||
when /^true|false$/i then value == 'true'
|
|
||||||
when /^[0-9]+$/ then value.to_i
|
|
||||||
when /^["|'](.+)["|']$/ then $1.gsub(/^["|']/, '').gsub(/["|']$/, '')
|
|
||||||
else
|
|
||||||
context[value]
|
|
||||||
end)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -2,25 +2,33 @@ require 'spec_helper'
|
|||||||
|
|
||||||
describe Locomotive::Liquid::Tags::WithScope do
|
describe Locomotive::Liquid::Tags::WithScope do
|
||||||
|
|
||||||
it 'should decode basic options (boolean, interger, ...)' do
|
it 'decodes basic options (boolean, integer, ...)' do
|
||||||
scope = Locomotive::Liquid::Tags::WithScope.new('with_scope', 'active:true price:42 title:\'foo\' hidden:false', ["{% endwith_scope %}"], {})
|
scope = Locomotive::Liquid::Tags::WithScope.new('with_scope', 'active:true price:42 title:\'foo\' hidden:false', ["{% endwith_scope %}"], {})
|
||||||
attributes = scope.send(:decode, scope.instance_variable_get(:@attributes), {})
|
attributes = scope.send(:decode, scope.instance_variable_get(:@attributes), ::Liquid::Context.new)
|
||||||
attributes['active'].should == true
|
attributes['active'].should == true
|
||||||
attributes['price'].should == 42
|
attributes['price'].should == 42
|
||||||
attributes['title'].should == 'foo'
|
attributes['title'].should == 'foo'
|
||||||
attributes['hidden'].should == false
|
attributes['hidden'].should == false
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should decode context variable' do
|
it 'decodes more complex options' do
|
||||||
|
scope = Locomotive::Liquid::Tags::WithScope.new('with_scope', 'price.gt:42.0 price.lt:50', ["{% endwith_scope %}"], {})
|
||||||
|
attributes = scope.send(:decode, scope.instance_variable_get(:@attributes), ::Liquid::Context.new)
|
||||||
|
attributes['price.gt'].should == 42.0
|
||||||
|
attributes['price.lt'].should == 50
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'decodes context variable' do
|
||||||
scope = Locomotive::Liquid::Tags::WithScope.new('with_scope', 'category: params.type', ["{% endwith_scope %}"], {})
|
scope = Locomotive::Liquid::Tags::WithScope.new('with_scope', 'category: params.type', ["{% endwith_scope %}"], {})
|
||||||
attributes = scope.send(:decode, scope.instance_variable_get(:@attributes), { 'params.type' => 'posts' })
|
attributes = scope.send(:decode, scope.instance_variable_get(:@attributes), ::Liquid::Context.new({ 'params' => { 'type' => 'posts' } }))
|
||||||
attributes['category'].should == 'posts'
|
attributes['category'].should == 'posts'
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should store attributes in the context' do
|
it 'stores attributes in the context' do
|
||||||
template = ::Liquid::Template.parse("{% with_scope active:true title:'foo' %}{{ with_scope.active }}-{{ with_scope.title }}{% endwith_scope %}")
|
template = ::Liquid::Template.parse("{% with_scope active:true title:'foo' %}{{ with_scope.active }}-{{ with_scope.title }}{% endwith_scope %}")
|
||||||
text = template.render
|
text = template.render
|
||||||
text.should == "true-foo"
|
text.should == "true-foo"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user