Skip to content

Commit

Permalink
Merge pull request #74 from prismicio/composite-slice
Browse files Browse the repository at this point in the history
Add support for composite slice
  • Loading branch information
srenault authored Jul 28, 2017
2 parents d6490d1 + 4b2c032 commit 97dc67f
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
prismic.io (1.4.4)
prismic.io (1.4.5)
hashery (~> 2.1.1)

GEM
Expand Down
53 changes: 51 additions & 2 deletions lib/prismic/fragments/slices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,57 @@
module Prismic
module Fragments

# A fragment of type Slice, an item in a SliceZone
class Slice < Fragment
# A fragment of type CompositeSlice, an item in a SliceZone
class CompositeSlice < Fragment
attr_accessor :slice_type
attr_accessor :slice_label
attr_accessor :non_repeat
attr_accessor :repeat

def initialize(slice_type, slice_label, non_repeat, repeat)
@slice_type = slice_type
@slice_label = slice_label
@non_repeat = non_repeat
@repeat = repeat
end

# Generate an text representation of the group
#
# @return [String] the text representation
def as_text
non_repeat_text = ''
@non_repeat.each do |fragment_key, fragment_value|
non_repeat_text += fragment_value.as_text
end

"#{non_repeat_text}\n#{@repeat.as_text}"
end

# Generate an HTML representation of the group
#
# @param link_resolver [LinkResolver] The LinkResolver used to build
# application's specific URL
#
# @return [String] the HTML representation
def as_html(link_resolver=nil)
classes = ['slice']
unless (@slice_label.nil?)
classes.push(@slice_label)
end

non_repeat_html = ''
@non_repeat.each do |fragment_key, fragment_value|
non_repeat_html += fragment_value.as_html(link_resolver)
end

repeat_html = repeat.as_html(link_resolver)

%[<div data-slicetype="#{@slice_type}" class="#{classes.join(' ')}">#{non_repeat_html + repeat_html}</div>]
end
end

# A fragment of type SimpleSlice, an item in a SliceZone
class SimpleSlice < Fragment
attr_accessor :slice_type
attr_accessor :slice_label
attr_accessor :value
Expand Down
17 changes: 16 additions & 1 deletion lib/prismic/json_parsers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,23 @@ def group_parser(json)

def slices_parser(json)
slices = []

json['value'].each do |data|
slices << Prismic::Fragments::Slice.new(data['slice_type'], data['slice_label'], fragment_parser(data['value']))
slice_type = data['slice_type']
slice_label = data['slice_label']

if data.key?('value')
slices << Prismic::Fragments::SimpleSlice.new(slice_type, slice_label, fragment_parser(data['value']))
else
non_repeat = {}
data['non-repeat'].each do |fragment_key, fragment_value|
non_repeat[fragment_key] = fragment_parser(fragment_value)
end

repeat = group_parser({ 'type' => 'Group', 'value' => data['repeat']})

slices << Prismic::Fragments::CompositeSlice.new(slice_type, slice_label, non_repeat, repeat)
end
end
Prismic::Fragments::SliceZone.new(slices)
end
Expand Down
34 changes: 32 additions & 2 deletions spec/fragments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -720,9 +720,9 @@ def strong(start, stop)

end

describe 'Slices' do
describe 'Simple slice' do
before do
@raw_json_slices = File.read("#{File.dirname(__FILE__)}/responses_mocks/slices.json")
@raw_json_slices = File.read("#{File.dirname(__FILE__)}/responses_mocks/simple_slice.json")
@json_slices = JSON.load(@raw_json_slices)
@doc = Prismic::JsonParser.document_parser(@json_slices)
@slices = @doc.get_slice_zone("article.blocks")
Expand All @@ -734,4 +734,34 @@ def strong(start, stop)
@slices.as_html(@link_resolver).gsub('&#39;', "'").should == %[<div data-slicetype="features" class="slice features-label"><section data-field="illustration"><img src="https://wroomdev.s3.amazonaws.com/toto/db3775edb44f9818c54baa72bbfc8d3d6394b6ef_hsf_evilsquall.jpg" alt="" width="4285" height="709" /></section>\n<section data-field="title"><span class="text">c'est un bloc features</span></section></div>\n<div data-slicetype="text" class="slice"><p>C'est un bloc content</p></div>\n<div data-slicetype="separator" class="slice"><section data-field="sep"><hr class="separator" /></section></div>]
end

it 'get item correctly' do
expected_url = 'https://wroomdev.s3.amazonaws.com/toto/db3775edb44f9818c54baa72bbfc8d3d6394b6ef_hsf_evilsquall.jpg'
@slices.slices[0].value[0]["illustration"].url.should == expected_url
end
end

describe 'Composite slice' do
before do
@raw_json_slices = File.read("#{File.dirname(__FILE__)}/responses_mocks/composite_slice.json")
@json_slices = JSON.load(@raw_json_slices)
@doc = Prismic::JsonParser.document_parser(@json_slices)
@slices = @doc.get_slice_zone("nav-nodejs.items")
@link_resolver = Prismic.link_resolver('master'){|doc_link| "http://localhost/#{doc_link.type}/#{doc_link.id}" }
end

it 'get html' do
@slices.as_html(@link_resolver).gsub('&#39;', "'").should == %[<div data-slicetype="with-submenu" class="slice"><p>Getting Started</p><section data-field="label"><p>Start from Scratch</p></section>\n<section data-field="link"><a href="http://localhost/page-nodejs/WPeD0SoAACsABzNC">page---nodejs---getting-started-with-the-starter-kit</a></section></div>]
end

it 'get text' do
@slices.as_text.should == %[Getting Started\nStart from Scratch\n]
end

it 'get item in non-repeat correctly' do
@slices.slices[0].non_repeat['label'].as_text.should == 'Getting Started'
end

it 'get item in repeat correctly' do
@slices.slices[0].repeat[0]['label'].as_text.should == 'Start from Scratch'
end
end
63 changes: 63 additions & 0 deletions spec/responses_mocks/composite_slice.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"id": "WPeLbyoAACsAB1Su",
"uid": null,
"type": "nav-nodejs",
"href": "http://prismicio-docs.prismic.io/api/v1/documents/search?ref=WXnqmigAALs3V7Kx&q=%5B%5B%3Ad+%3D+at%28document.id%2C+%22WPeLbyoAACsAB1Su%22%29+%5D%5D",
"tags": [],
"first_publication_date": "2017-04-19T16:08:43+0000",
"last_publication_date": "2017-07-07T11:26:27+0000",
"slugs": ["navigation---nodejs", "navigation-nodejs"],
"data": {
"nav-nodejs": {
"display-name": {
"type": "StructuredText",
"value": [{
"type": "paragraph",
"text": "Navigation - NodeJS",
"spans": []
}]
},
"items": {
"type": "SliceZone",
"value": [{
"type": "Slice",
"slice_type": "with-submenu",
"slice_label": null,
"repeat": [{
"label": {
"type": "StructuredText",
"value": [{
"type": "paragraph",
"text": "Start from Scratch",
"spans": []
}]
},
"link": {
"type": "Link.document",
"value": {
"document": {
"id": "WPeD0SoAACsABzNC",
"type": "page-nodejs",
"tags": [],
"slug": "page---nodejs---getting-started-with-the-starter-kit",
"uid": "prismic-from-scratch-with-nodejs"
},
"isBroken": false
}
}
}],
"non-repeat": {
"label": {
"type": "StructuredText",
"value": [{
"type": "paragraph",
"text": "Getting Started",
"spans": []
}]
}
}
}]
}
}
}
}
File renamed without changes.

0 comments on commit 97dc67f

Please sign in to comment.