add with_scope liquid tag (+ specs)

did committed Feb 03, 2015
commit 3adba9dce448dba64e47e7713175f77f6c8c1a5c
Showing 4 changed files with 90 additions and 10 deletions
.travis.yml +2 -0
@@ @@ -1,6 +1,8 @@
language: ruby
rvm:
- 2.1.3
+ env:
+ - CODECLIMATE_REPO_TOKEN=8d9c25de4eff1cd06d3accdc09775397e1a81be67e2a159453ba4e4408acae16
addons:
code_climate:
repo_token: 8d9c25de4eff1cd06d3accdc09775397e1a81be67e2a159453ba4e4408acae16
locomotive/steam/liquid/tags/with_scope.rb b/lib/locomotive/steam/liquid/tags/with_scope.rb +4 -9
@@ @@ -23,11 +23,11 @@ module Locomotive
# register the tag
tag_name :with_scope
- def initialize(tag_name, arguments_string, tokens, context = {})
+ def initialize(name, markup, options)
# convert symbol operators into valid ruby code
- arguments_string.gsub!(SYMBOL_OPERATORS_REGEXP, ':"\1" =>')
+ markup.gsub!(SYMBOL_OPERATORS_REGEXP, ':"\1" =>')
- super(tag_name, arguments_string, tokens, context)
+ super(name, markup, options)
end
def display(options = {}, &block)
@@ @@ -42,13 +42,8 @@ module Locomotive
def decode(options)
HashWithIndifferentAccess.new.tap do |hash|
options.each do |key, value|
- _key, _operator = key.to_s.split('.')
-
# _slug instead of _permalink
- _key = '_slug' if _key == '_permalink'
-
- # key to h4s symbol
- _key = _key.to_s.to_sym.send(_operator.to_sym) if _operator
+ _key = key.to_s == '_permalink' ? '_slug' : key.to_s
hash[_key] = (case value
# regexp inside a string
spec/unit/liquid/tags/session_assign_spec.rb +1 -1
@@ @@ -8,7 +8,7 @@ describe Locomotive::Steam::Liquid::Tags::SessionAssign do
let(:assigns) { {} }
let(:context) { ::Liquid::Context.new(assigns, {}, { request: request }) }
- let!(:output) { render_template(source, context) }
+ let!(:output) { render_template(source, context) }
subject { session[:title] }
spec/unit/liquid/tags/with_scope_spec.rb +83 -0
@@ @@ -0,0 +1,83 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::Liquid::Tags::WithScope do
+
+ let(:assigns) { {} }
+ let(:context) { ::Liquid::Context.new(assigns, {}, {}) }
+ let!(:output) { render_template(source, context) }
+ let(:conditions) { context['conditions'] }
+
+ describe 'decode basic options (boolean, integer, ...)' do
+
+ let(:source) { "{% with_scope active: true, price: 42, title: 'foo', hidden: false %}{% assign conditions = with_scope %}{% endwith_scope %}" }
+
+ it { expect(conditions['active']).to eq true }
+ it { expect(conditions['price']).to eq 42 }
+ it { expect(conditions['title']).to eq 'foo' }
+ it { expect(conditions['hidden']).to eq false }
+
+ end
+
+ describe 'decode regexps' do
+
+ let(:source) { "{% with_scope title: /Like this one|or this one/ %}{% assign conditions = with_scope %}{% endwith_scope %}" }
+ it { expect(conditions['title']).to eq(/Like this one|or this one/) }
+
+ end
+
+ describe 'decode context variable' do
+
+ let(:assigns) { { 'params' => { 'type' => 'posts' } } }
+ let(:source) { "{% with_scope category: params.type %}{% assign conditions = with_scope %}{% endwith_scope %}" }
+ it { expect(conditions['category']).to eq 'posts' }
+
+ end
+
+ describe 'decode a regexp stored in a context variable' do
+
+ let(:assigns) { { 'my_regexp' => '/^Hello World/' } }
+ let(:source) { "{% with_scope title: my_regexp %}{% assign conditions = with_scope %}{% endwith_scope %}" }
+ it { expect(conditions['title']).to eq(/^Hello World/) }
+
+ end
+
+ describe 'allow order_by option' do
+
+ let(:source) { "{% with_scope order_by:\'name DESC\' %}{% assign conditions = with_scope %}{% endwith_scope %}" }
+ it { expect(conditions['order_by']).to eq 'name DESC' }
+
+ end
+
+ describe 'replace _permalink by _slug' do
+
+ let(:source) { "{% with_scope _permalink: 'foo' %}{% assign conditions = with_scope %}{% endwith_scope %}" }
+ it { expect(conditions['_slug']).to eq 'foo' }
+
+ end
+
+ describe 'decode criteria with gt and lt' do
+
+ let(:source) { "{% with_scope price.gt:42.0, price.lt:50 %}{% assign conditions = with_scope %}{% endwith_scope %}" }
+ it { expect(conditions['price.gt']).to eq 42.0 }
+ it { expect(conditions['price.lt']).to eq 50 }
+
+ end
+
+ # describe "advanced queries thanks to h4s" do
+
+ # it 'decodes criteria with gt and lt' do
+ # scope = Locomotive::Liquid::Tags::WithScope.new('with_scope', 'price.gt:42.0, price.lt:50', ["{% endwith_scope %}"], {})
+ # options = decode_options(scope)
+ # expect(options[:price.gt]).to eq(42.0)
+ # expect(options[:price.lt]).to eq(50)
+ # end
+
+ # end
+
+ # def decode_options(tag, assigns = {})
+ # context = ::Liquid::Context.new(assigns)
+ # arguments = tag.instance_variable_get(:@arguments)
+ # tag.send(:decode, *arguments.interpolate(context))
+ # end
+
+ end