Skip to content

Commit

Permalink
Render storylines as array in seed data
Browse files Browse the repository at this point in the history
Unify interface to allow generic handling of storylines just like any
other collection.
  • Loading branch information
tf committed Dec 13, 2016
1 parent 0024e05 commit 3cea263
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 77 deletions.
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"maxlen": 100, "maxlen": 100,
"unused": "vars", "unused": "vars",
"expr": true, "expr": true,
"camelcase": false,
"predef": [ "predef": [
"jQuery", "jQuery",
"$", "$",
Expand Down
5 changes: 4 additions & 1 deletion app/assets/javascripts/pageflow/seed_entry_data.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ pageflow.SeedEntryData = pageflow.EntryData.extend({
initialize: function(options) { initialize: function(options) {
this.theming = options.theming; this.theming = options.theming;


this.storylineConfigurations = options.storyline_configurations; this.storylineConfigurations = _(options.storylines).reduce(function(memo, storyline) {
memo[storyline.id] = storyline.configuration;
return memo;
}, {});


this.storylineIdsByChapterIds = _(options.chapters).reduce(function(memo, chapter) { this.storylineIdsByChapterIds = _(options.chapters).reduce(function(memo, chapter) {
memo[chapter.id] = chapter.storyline_id; memo[chapter.id] = chapter.storyline_id;
Expand Down
9 changes: 4 additions & 5 deletions app/helpers/pageflow/entry_json_seed_helper.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def entry_json_seed(entry)
def entry_seed(entry) def entry_seed(entry)
common_entry_seed(entry).merge( common_entry_seed(entry).merge(
theming: entry_theming_seed(entry), theming: entry_theming_seed(entry),
storyline_configurations: entry_storyline_configurations_seed(entry), storylines: entry_storylines_seed(entry),
chapters: entry_chapters_seed(entry), chapters: entry_chapters_seed(entry),
pages: entry_pages_seed(entry), pages: entry_pages_seed(entry),
file_ids: entry_file_ids_seed(entry) file_ids: entry_file_ids_seed(entry)
Expand All @@ -28,10 +28,9 @@ def entry_theming_seed(entry)
} }
end end


def entry_storyline_configurations_seed(entry) def entry_storylines_seed(entry)
entry.storylines.each_with_object({}) do |storyline, result| attributes = [:id, :configuration]
result[storyline.id] = storyline.configuration entry.storylines.as_json(only: attributes)
end
end end


def entry_chapters_seed(entry) def entry_chapters_seed(entry)
Expand Down
11 changes: 6 additions & 5 deletions spec/helpers/pageflow/entry_json_seed_helper_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -28,20 +28,21 @@ module Pageflow
end end
end end


describe '#entry_storyline_configurations_seed' do describe '#entry_storylines_seed' do
it 'indexed configurations by id' do it 'includes id and configuration' do
revision = create(:revision, :published) revision = create(:revision, :published)
storyline = create(:storyline, revision: revision, configuration: {text: 'some text'}) storyline = create(:storyline, revision: revision, configuration: {text: 'some text'})
entry = PublishedEntry.new(create(:entry, published_revision: revision)) entry = PublishedEntry.new(create(:entry, published_revision: revision))


result = helper.entry_storyline_configurations_seed(entry) result = helper.entry_storylines_seed(entry)


expect(result[storyline.id]['text']).to eq('some text') expect(result[0]['id']).to eq(storyline.id)
expect(result[0]['configuration']['text']).to eq('some text')
end end
end end


describe '#entry_chapters_seed' do describe '#entry_chapters_seed' do
it 'indexed configurations by id' do it 'includes id and configuration' do
revision = create(:revision, :published) revision = create(:revision, :published)
storyline = create(:storyline, revision: revision) storyline = create(:storyline, revision: revision)
chapter = create(:chapter, chapter = create(:chapter,
Expand Down
33 changes: 21 additions & 12 deletions spec/javascripts/pageflow/chapter_filter_spec.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ describe('pageflow.ChapterFilter', function() {
describe('with default strategy', function() { describe('with default strategy', function() {
it('return true for chapters of same storyline', function() { it('return true for chapters of same storyline', function() {
var chapterFilter = new p.ChapterFilter(new p.SeedEntryData({ var chapterFilter = new p.ChapterFilter(new p.SeedEntryData({
storyline_configurations: { storylines: [
10: {} {
}, id: 10,
configuration: {}
}
],
chapters: [ chapters: [
{ {
id: 1, id: 1,
Expand All @@ -33,12 +36,15 @@ describe('pageflow.ChapterFilter', function() {
describe('with inherit_from_parent strategy', function() { describe('with inherit_from_parent strategy', function() {
it('returns true only for chapters visible from parent chapter', function() { it('returns true only for chapters visible from parent chapter', function() {
var chapterFilter = new p.ChapterFilter(new p.SeedEntryData({ var chapterFilter = new p.ChapterFilter(new p.SeedEntryData({
storyline_configurations: { storylines: [
10: { {
navigation_bar_mode: 'inherit_from_parent', id: 10,
parent_page_perma_id: 100 configuration: {
navigation_bar_mode: 'inherit_from_parent',
parent_page_perma_id: 100
}
} }
}, ],
chapters: [ chapters: [
{ {
id: 1, id: 1,
Expand Down Expand Up @@ -67,11 +73,14 @@ describe('pageflow.ChapterFilter', function() {
describe('with non strategy', function() { describe('with non strategy', function() {
it('returns false', function() { it('returns false', function() {
var chapterFilter = new p.ChapterFilter(new p.SeedEntryData({ var chapterFilter = new p.ChapterFilter(new p.SeedEntryData({
storyline_configurations: { storylines: [
10: { {
navigation_bar_mode: 'non' id: 10,
configuration: {
navigation_bar_mode: 'non'
}
} }
}, ],
chapters: [ chapters: [
{ {
id: 1, id: 1,
Expand Down
64 changes: 40 additions & 24 deletions spec/javascripts/pageflow/entry_data_spec.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ describe('pageflow.EntryData', function() {
id: 100, id: 100,
storyline_id: 1000 storyline_id: 1000
}], }],
storyline_configurations: { storylines: [{
1000: { id: 1000,
configuration: {
parent_page_perma_id: 6 parent_page_perma_id: 6
} }
} }]
}); });


var result = entryData.getParentPagePermaIdByPagePermaId(5); var result = entryData.getParentPagePermaIdByPagePermaId(5);
Expand Down Expand Up @@ -67,12 +68,17 @@ describe('pageflow.EntryData', function() {
storyline_id: 1001 storyline_id: 1001
} }
], ],
storyline_configurations: { storylines: [
1000: {}, {
1001: { id: 1000
parent_page_perma_id: 5 },
{
id: 1001,
configuration: {
parent_page_perma_id: 5
}
} }
} ]
}); });


var result = entryData.getParentStorylineId(1001); var result = entryData.getParentStorylineId(1001);
Expand All @@ -82,9 +88,9 @@ describe('pageflow.EntryData', function() {


it('returns undefined if there is no parent page', function() { it('returns undefined if there is no parent page', function() {
var entryData = new createEntryData({ var entryData = new createEntryData({
storyline_configurations: { storylines: [{
1001: {} id: 1001
} }]
}); });


var result = entryData.getParentStorylineId(1001); var result = entryData.getParentStorylineId(1001);
Expand All @@ -100,11 +106,12 @@ describe('pageflow.EntryData', function() {
id: 100, id: 100,
storyline_id: 1000 storyline_id: 1000
}], }],
storyline_configurations: { storylines: [{
1000: { id: 1000,
configuration: {
parent_page_perma_id: 6 parent_page_perma_id: 6
} }
}, }],
pages: [{ pages: [{
perma_id: 6, perma_id: 6,
chapter_id: 101 chapter_id: 101
Expand All @@ -120,11 +127,12 @@ describe('pageflow.EntryData', function() {
describe('#getParentPagePermaId', function() { describe('#getParentPagePermaId', function() {
it('returns perma id of storyline`s parent page', function() { it('returns perma id of storyline`s parent page', function() {
var entryData = new createEntryData({ var entryData = new createEntryData({
storyline_configurations: { storylines: [{
1000: { id: 1000,
configuration: {
parent_page_perma_id: 6 parent_page_perma_id: 6
} }
} }]
}); });


var result = entryData.getParentPagePermaId(1000); var result = entryData.getParentPagePermaId(1000);
Expand Down Expand Up @@ -156,15 +164,23 @@ describe('pageflow.EntryData', function() {
storyline_id: 1001 storyline_id: 1001
} }
], ],
storyline_configurations: { storylines: [
1000: {}, {
1001: { id: 1000,
parent_page_perma_id: 5
}, },
1002: { {
parent_page_perma_id: 6 id: 1001,
configuration: {
parent_page_perma_id: 5
},
},
{
id: 1002,
configuration: {
parent_page_perma_id: 6
}
} }
} ]
}); });


expect(entryData.getStorylineLevel(1000)).to.eq(0); expect(entryData.getStorylineLevel(1000)).to.eq(0);
Expand Down
78 changes: 51 additions & 27 deletions spec/javascripts/pageflow/highlighted_page_spec.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ describe('pageflow.HighlightedPage', function() {
describe('for storylines with own navigation bar', function() { describe('for storylines with own navigation bar', function() {
it('returns perma id of same page if page is displayed in navigation', function() { it('returns perma id of same page if page is displayed in navigation', function() {
var outline = new p.HighlightedPage(new p.SeedEntryData({ var outline = new p.HighlightedPage(new p.SeedEntryData({
storyline_configurations: { storylines: [
10: {} {
}, id: 10,
configuration: {}
}
],
chapters: [{ chapters: [{
id: 1, id: 1,
storyline_id: 10 storyline_id: 10
Expand All @@ -27,9 +30,12 @@ describe('pageflow.HighlightedPage', function() {


it('returns perma id of previous page if page is not displayed in navigation', function() { it('returns perma id of previous page if page is not displayed in navigation', function() {
var outline = new p.HighlightedPage(new p.SeedEntryData({ var outline = new p.HighlightedPage(new p.SeedEntryData({
storyline_configurations: { storylines: [
10: {} {
}, id: 10,
configuration: {}
}
],
chapters: [{ chapters: [{
id: 1, id: 1,
storyline_id: 10 storyline_id: 10
Expand Down Expand Up @@ -58,12 +64,15 @@ describe('pageflow.HighlightedPage', function() {
describe('for chapter with inherited navigation bar', function() { describe('for chapter with inherited navigation bar', function() {
it('returns perma id of parent page', function() { it('returns perma id of parent page', function() {
var outline = new p.HighlightedPage(new p.SeedEntryData({ var outline = new p.HighlightedPage(new p.SeedEntryData({
storyline_configurations: { storylines: [
10: { {
navigation_bar_mode: 'inherit_from_parent', id: 10,
parent_page_perma_id: 101 configuration: {
navigation_bar_mode: 'inherit_from_parent',
parent_page_perma_id: 101
}
}, },
}, ],
chapters: [ chapters: [
{ {
id: 1, id: 1,
Expand Down Expand Up @@ -91,16 +100,22 @@ describe('pageflow.HighlightedPage', function() {
describe('for chapter with inherited navigation bar over multiple levels', function() { describe('for chapter with inherited navigation bar over multiple levels', function() {
it('returns perma id of parent page', function() { it('returns perma id of parent page', function() {
var outline = new p.HighlightedPage(new p.SeedEntryData({ var outline = new p.HighlightedPage(new p.SeedEntryData({
storyline_configurations: { storylines: [
10: { {
navigation_bar_mode: 'inherit_from_parent', id: 10,
parent_page_perma_id: 101 configuration: {
navigation_bar_mode: 'inherit_from_parent',
parent_page_perma_id: 101
}
}, },
20: { {
navigation_bar_mode: 'inherit_from_parent', id: 20,
parent_page_perma_id: 102 configuration: {
navigation_bar_mode: 'inherit_from_parent',
parent_page_perma_id: 102
}
} }
}, ],
chapters: [ chapters: [
{id: 1, storyline_id: 10}, {id: 1, storyline_id: 10},
{id: 2, storyline_id: 20}, {id: 2, storyline_id: 20},
Expand All @@ -122,17 +137,26 @@ describe('pageflow.HighlightedPage', function() {
describe('with customNavigationBarMode option', function() { describe('with customNavigationBarMode option', function() {
it('uses navigation bar modes returned by option', function() { it('uses navigation bar modes returned by option', function() {
var entryData = new p.SeedEntryData({ var entryData = new p.SeedEntryData({
storyline_configurations: { storylines: [
10: { {
parent_page_perma_id: 101 id: 10,
configuration: {
parent_page_perma_id: 101
}
}, },
20: { {
parent_page_perma_id: 102, id: 20,
configuration: {
parent_page_perma_id: 102,
}
}, },
30: { {
main: true id: 30,
configuration: {
main: true
}
} }
}, ],
chapters: [ chapters: [
{id: 1, storyline_id: 10}, {id: 1, storyline_id: 10},
{id: 2, storyline_id: 20}, {id: 2, storyline_id: 20},
Expand Down
9 changes: 6 additions & 3 deletions spec/javascripts/pageflow/seed_entry_data_spec.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ describe('pageflow.SeedEntryData', function() {
it('returns configruation by chapter id', function() { it('returns configruation by chapter id', function() {
var configuration = {}; var configuration = {};
var entryData = new p.SeedEntryData({ var entryData = new p.SeedEntryData({
storyline_configurations: { storylines: [
1: configuration {
} id: 1,
configuration: configuration
}
]
}); });


var result = entryData.getStorylineConfiguration(1); var result = entryData.getStorylineConfiguration(1);
Expand Down

0 comments on commit 3cea263

Please sign in to comment.