with_scope allows passing variable (strings) that will be converted in regexp if the format is okay

did committed Oct 10, 2014
commit 04eaac4b9c85327f22fd9851423b5a889c3a477b
Showing 4 changed files with 35 additions and 31 deletions
locomotive/wagon/liquid/tags/with_scope.rb b/lib/locomotive/wagon/liquid/tags/with_scope.rb +29 -25
@@ @@ -2,42 +2,46 @@ module Locomotive
module Wagon
module Liquid
module Tags
- class WithScope < ::Liquid::Block
- SlashedString = /\/[^\/]*\//
- TagAttributes = /(\w+|\w+\.\w+)\s*\:\s*(#{SlashedString}|#{::Liquid::QuotedFragment})/
+ class WithScope < Solid::Block
- def initialize(tag_name, markup, tokens, options)
- @tag_options = HashWithIndifferentAccess.new
- markup.scan(TagAttributes) do |key, value|
- @tag_options[key] = value
- end
- super
+ OPERATORS = %w(all exists gt gte in lt lte ne nin size near within)
+
+ SYMBOL_OPERATORS_REGEXP = /(\w+\.(#{OPERATORS.join('|')})){1}\s*\:/
+
+ # register the tag
+ tag_name :with_scope
+
+ def initialize(tag_name, arguments_string, tokens, context = {})
+ # convert symbol operators into valid ruby code
+ arguments_string.gsub!(SYMBOL_OPERATORS_REGEXP, ':"\1" =>')
+
+ super(tag_name, arguments_string, tokens, context)
end
- def render(context)
- context.stack do
- context['with_scope'] = decode(@tag_options, context)
- render_all(@nodelist, context)
+ def display(options = {}, &block)
+ current_context.stack do
+ current_context['with_scope'] = self.decode(options)
+ yield
end
end
- private
-
- def decode(attributes, context)
- attributes.each_pair do |key, value|
- attributes[key] = (case value
- when /^true|false$/i then value == 'true'
- when /^\/[^\/]*\/$/ then Regexp.new(value[1..-2])
- when /^["|'](.+)["|']$/ then $1.gsub(/^["|']/, '').gsub(/["|']$/, '')
- else
- context[value].nil? ? value : context[value]
- end)
+ protected
+
+ def decode(options)
+ HashWithIndifferentAccess.new.tap do |hash|
+ options.each do |key, value|
+ hash[key] = (case value
+ # regexp inside a string
+ when /^\/[^\/]*\/$/ then Regexp.new(value[1..-2])
+ else
+ value
+ end)
+ end
end
end
end
- ::Liquid::Template.register_tag('with_scope', WithScope)
end
end
end
locomotive/wagon/version.rb b/lib/locomotive/wagon/version.rb +1 -1
@@ @@ -1,5 +1,5 @@
module Locomotive
module Wagon
- VERSION = '1.5.0'
+ VERSION = '1.5.0.rc1'
end
end
locomotivecms_wagon.gemspec +1 -1
@@ @@ -35,7 +35,7 @@ Gem::Specification.new do |gem|
gem.add_dependency 'httmultiparty', '0.3.10'
gem.add_dependency 'will_paginate', '~> 3.0.3'
- gem.add_dependency 'locomotivecms_mounter', '~> 1.4.3'
+ gem.add_dependency 'locomotivecms_mounter', '~> 1.4.4'
gem.add_dependency 'faker', '~> 0.9.5'
spec/fixtures/default/app/views/pages/filtered.liquid.haml +4 -4
@@ @@ -1,11 +1,11 @@
---
title: Various uses of the with_scope tag
---
- {% assign begin = '2012-06-01 00:00:00' %}
- {% assign end = '2012-06-10 23:59:59' %}
- {% assign prices = '5.0,5.5' | split: ',' | map: 'to_f' %}
+ {% assign begin_date = '2012-06-01 00:00:00' %}
+ {% assign end_date = '2012-06-10 23:59:59' %}
+ {% assign prices = '5.0,5.5' | split: ',' | map: 'to_f' %}
- {% with_scope date.gte: begin, date.lte: end, city: /Kansas/, state.ne: 'Illinois', tags: 'awesome', tags.nin: 'bad', price.in: prices, order_by: 'price.desc' %}
+ {% with_scope date.gte: begin_date, date.lte: end_date, city: /Kansas/, state.ne: 'Illinois', tags: 'awesome', tags.nin: 'bad', price.in: prices, order_by: 'price.desc' %}
events={{ contents.events.count }}.
{% endwith_scope %}