with_scope is now associated to the first content_type found in the template (fix issue locomotivecms/engine#806, locomotivecms/steam#48)
did
committed Dec 08, 2015
commit dfa638b38d7579ab0193341e20a4ef3345846e51
Showing 8
changed files with
68 additions
and 7 deletions
locomotive/steam/liquid/drops/content_entry.rb b/lib/locomotive/steam/liquid/drops/content_entry.rb
+7
-1
| @@ | @@ -50,7 +50,7 @@ module Locomotive |
| return '' if @_source.nil? | |
| if not @@forbidden_attributes.include?(meth.to_s) | |
| - | repository(@_source).value_for(@_source, meth, @context['with_scope']) |
| + | repository(@_source).value_for(@_source, meth, conditions_for(meth)) |
| else | |
| nil | |
| end | |
| @@ | @@ -86,6 +86,12 @@ module Locomotive |
| repository.with(entry.content_type) | |
| end | |
| + | def conditions_for(name) |
| + | # note: treat conditions only they apply to the content type (if it's a has_many/many_to_many relationships) |
| + | _name = @context['with_scope_content_type'] |
| + | !_name || _name == name ? @context['with_scope'] : nil |
| + | end |
| + | |
| end | |
| end | |
| end | |
locomotive/steam/liquid/drops/content_entry_collection.rb b/lib/locomotive/steam/liquid/drops/content_entry_collection.rb
+2
-1
| @@ | @@ -60,7 +60,8 @@ module Locomotive |
| end | |
| def conditions | |
| - | @context['with_scope'] |
| + | _slug = (@context['with_scope_content_type'] ||= @content_type.slug) |
| + | _slug == @content_type.slug ? @context['with_scope'] : {} |
| end | |
| def services | |
locomotive/steam/liquid/tags/with_scope.rb b/lib/locomotive/steam/liquid/tags/with_scope.rb
+1
-0
| @@ | @@ -33,6 +33,7 @@ module Locomotive |
| def display(options = {}, &block) | |
| current_context.stack do | |
| current_context['with_scope'] = self.decode(options) | |
| + | current_context['with_scope_content_type'] = false # for now, no content type is assigned to this with_scope |
| yield | |
| end | |
| end | |
locomotive/steam/repositories/content_entry_repository.rb b/lib/locomotive/steam/repositories/content_entry_repository.rb
+1
-0
| @@ | @@ -76,6 +76,7 @@ module Locomotive |
| if %i(has_many many_to_many).include?(field.type) | |
| # a safe copy of the proxy repository is needed here | |
| value = value.dup | |
| + | |
| # like this, we do not modify the original local conditions | |
| value.local_conditions.merge!(conditions) if conditions | |
| end | |
spec/spec_helper.rb
+2
-3
| @@ | @@ -3,11 +3,10 @@ require 'codeclimate-test-reporter' |
| require 'coveralls' | |
| SimpleCov.start do | |
| - | formatter SimpleCov::Formatter::MultiFormatter[ |
| + | formatter SimpleCov::Formatter::MultiFormatter.new([ |
| SimpleCov::Formatter::HTMLFormatter, | |
| CodeClimate::TestReporter::Formatter, | |
| - | Coveralls::SimpleCov::Formatter |
| - | ] |
| + | Coveralls::SimpleCov::Formatter]) |
| add_filter 'config/' | |
| add_filter 'example/' | |
spec/unit/liquid/drops/content_entry_collection_spec.rb
+20
-2
| @@ | @@ -38,18 +38,36 @@ describe Locomotive::Steam::Liquid::Drops::ContentEntryCollection do |
| describe '#first' do | |
| before do | |
| - | expect(services.repositories.content_entry).to receive(:all).with({ 'visible' => true }).and_return(['a', 'b']) |
| + | expect(services.repositories.content_entry).to receive(:all).with('visible' => true).and_return(['a', 'b']) |
| end | |
| it { expect(drop.first).to eq('a') } | |
| end | |
| describe '#count' do | |
| before do | |
| - | expect(services.repositories.content_entry).to receive(:count).with({ 'visible' => true }).and_return(2) |
| + | expect(services.repositories.content_entry).to receive(:count).with('visible' => true).and_return(2) |
| end | |
| it { expect(drop.count).to eq 2 } | |
| end | |
| + | describe 'only applied to the first content type' do |
| + | |
| + | it 'sets the content type in the context' do |
| + | expect(services.repositories.content_entry).to receive(:all).with('visible' => true).and_return(['a', 'b']) |
| + | expect(context['with_scope_content_type']).to eq nil |
| + | drop.first |
| + | expect(context['with_scope_content_type']).to eq 'articles' |
| + | end |
| + | |
| + | it "doesn't apply the with_scope conditions if it's not the same content type" do |
| + | context['with_scope_content_type'] = 'projects' |
| + | expect(services.repositories.content_entry).to receive(:all).with({}).and_return(['a', 'b']) |
| + | drop.first |
| + | expect(context['with_scope_content_type']).to eq 'projects' |
| + | end |
| + | |
| + | end |
| + | |
| end | |
| end | |
spec/unit/liquid/drops/content_entry_spec.rb
+26
-0
| @@ | @@ -112,4 +112,30 @@ describe Locomotive::Steam::Liquid::Drops::ContentEntry do |
| end | |
| + | describe '#conditions_for' do |
| + | |
| + | let(:name) { 'news' } |
| + | |
| + | subject { drop.send(:conditions_for, name) } |
| + | |
| + | before { context['with_scope'] = 42 } |
| + | |
| + | it { is_expected.to eq 42 } |
| + | |
| + | context 'the with_scope has been used before by another and different content type' do |
| + | |
| + | before { context['with_scope_content_type'] = 'articles' } |
| + | it { is_expected.to eq nil } |
| + | |
| + | end |
| + | |
| + | context 'the with_scope has been used before by the same content type' do |
| + | |
| + | before { context['with_scope_content_type'] = 'news' } |
| + | it { is_expected.to eq 42 } |
| + | |
| + | end |
| + | |
| + | end |
| + | |
| end | |
spec/unit/liquid/tags/with_scope_spec.rb
+9
-0
| @@ | @@ -7,6 +7,15 @@ describe Locomotive::Steam::Liquid::Tags::WithScope do |
| let!(:output) { render_template(source, context) } | |
| let(:conditions) { context['conditions'] } | |
| + | describe 'store the conditions in the context' do |
| + | |
| + | let(:source) { "{% with_scope active: true, price: 42, title: 'foo', hidden: false %}{% assign conditions = with_scope %}{% assign content_type = with_scope_content_type %}{% endwith_scope %}" } |
| + | |
| + | it { expect(context['conditions'].keys).to eq(%w(active price title hidden)) } |
| + | it { expect(context['content_type']).to eq false } |
| + | |
| + | end |
| + | |
| 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 %}" } | |