Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix with_scope for Section Blocks #145

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion lib/locomotive/steam/liquid/drops/section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def anchor
end

def blocks
(@content['blocks'] || []).each_with_index.map do |block, index|
scoped_blocks.each_with_index.map do |block, index|
SectionBlock.new(@section, block, index)
end
end
Expand All @@ -52,6 +52,26 @@ def editor_setting_data
SectionEditorSettingData.new(@section)
end

private

def scoped_blocks
val = (@content['blocks'] || [])

if @context['with_scope']
@context['with_scope_content_type'] ||= 'blocks'

if @context['with_scope_content_type'] == 'blocks'
conditions = @context['with_scope'] || {}

val = val.select do |block|
conditions.all?{|k,v| block[k] == v}
end
end
end

val
end

end

end
Expand Down
43 changes: 39 additions & 4 deletions spec/unit/liquid/drops/section_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,25 @@

describe Locomotive::Steam::Liquid::Drops::Section do

let(:context) { ::Liquid::Context.new({}, {}, { locale: 'en' }) }
let(:settings) { [] }
let(:definition) { instance_double('SectionDefinition', definition: { 'settings' => settings }) }
let(:drop) { described_class.new(definition, content).tap { |d| d.context = context } }
let(:context) { ::Liquid::Context.new({}, {}, { locale: 'en' }) }
let(:settings) { [] }
let(:definition) {
instance_double('SectionDefinition', definition: {
'settings' => settings,
'blocks' => [{type: :foo}, {type: :bar}],
})
}
let(:content) {
{
'settings' => {},
'blocks' => [
{'type' => 'foo'},
{'type' => 'bar'},
{'type' => 'foo'},
]
}
}
let(:drop) { described_class.new(definition, content).tap { |d| d.context = context } }

describe 'text type setting' do

Expand All @@ -23,4 +38,24 @@

end

describe '#blocks' do
subject { drop.send(:blocks).count }

before { context['with_scope'] = {'type' => 'foo'} }

it { is_expected.to eq 2 }

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 3 }
end

context 'the with_scope has been used before by the same content type' do
before { context['with_scope_content_type'] = 'blocks' }
it { is_expected.to eq 2 }
end

end


end