From 22e9ab0a1a6814256670b5165158d19dca403fa3 Mon Sep 17 00:00:00 2001 From: Henry Snopek Date: Tue, 27 Oct 2015 14:08:46 -0500 Subject: [PATCH 01/23] :construction: scaffolding --- lib/index.coffee | 8 ++++++-- test/fixtures/basic/app.coffee | 1 + test/fixtures/locale/about.jade | 1 + test/fixtures/locale/app.coffee | 20 ++++++++++++++++++++ test/fixtures/locale/index.jade | 5 +++++ test/fixtures/locale/package.json | 6 ++++++ 6 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/locale/about.jade create mode 100644 test/fixtures/locale/app.coffee create mode 100644 test/fixtures/locale/index.jade create mode 100644 test/fixtures/locale/package.json diff --git a/lib/index.coffee b/lib/index.coffee index 9bc58a2..a0f6d9d 100644 --- a/lib/index.coffee +++ b/lib/index.coffee @@ -104,8 +104,12 @@ module.exports = (opts) -> fetch_content = (type) -> W( - client.entries( - _.merge(type.filters, content_type: type.id, include: 10) + client.entries(_.merge( + type.filters, + content_type: type.id, + include: 10, + locale: type.locale + ) ) ) diff --git a/test/fixtures/basic/app.coffee b/test/fixtures/basic/app.coffee index b303f3c..bd307a0 100644 --- a/test/fixtures/basic/app.coffee +++ b/test/fixtures/basic/app.coffee @@ -9,6 +9,7 @@ module.exports = content_types: [ { id: '6BYT1gNiIEyIw8Og8aQAO6' + locale: 'en-ES' } { id: '7CDlVsacqQc88cmIEGYWMa' diff --git a/test/fixtures/locale/about.jade b/test/fixtures/locale/about.jade new file mode 100644 index 0000000..8240f0e --- /dev/null +++ b/test/fixtures/locale/about.jade @@ -0,0 +1 @@ +h1 wow diff --git a/test/fixtures/locale/app.coffee b/test/fixtures/locale/app.coffee new file mode 100644 index 0000000..08900ec --- /dev/null +++ b/test/fixtures/locale/app.coffee @@ -0,0 +1,20 @@ +contentful = require '../../..' + +module.exports = + ignores: ["**/_*", "**/.DS_Store"] + extensions: [ + contentful( + access_token: 'YOUR_ACCESS_TOKEN' + space_id: 'aqzq2qya2jm4' + content_types: [ + { + id: '6BYT1gNiIEyIw8Og8aQAO6' + locale: 'en-ES' + } + { + id: '7CDlVsacqQc88cmIEGYWMa' + locale: 'en-ES' + } + ] + ) + ] diff --git a/test/fixtures/locale/index.jade b/test/fixtures/locale/index.jade new file mode 100644 index 0000000..4769500 --- /dev/null +++ b/test/fixtures/locale/index.jade @@ -0,0 +1,5 @@ +ul + - for p in contentful.blog_posts + li + h1= p.title + p= p.body diff --git a/test/fixtures/locale/package.json b/test/fixtures/locale/package.json new file mode 100644 index 0000000..2d0ae2c --- /dev/null +++ b/test/fixtures/locale/package.json @@ -0,0 +1,6 @@ +{ + "name": "test", + "dependencies": { + "jade": "*" + } +} From a793b5b8d8c1d7e825361b8cde4c92d759ad9cc5 Mon Sep 17 00:00:00 2001 From: Henry Snopek Date: Wed, 28 Oct 2015 14:27:06 -0500 Subject: [PATCH 02/23] add untested logic --- lib/index.coffee | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/index.coffee b/lib/index.coffee index a0f6d9d..e742a9a 100644 --- a/lib/index.coffee +++ b/lib/index.coffee @@ -40,7 +40,7 @@ module.exports = (opts) -> @roots.config.locals.asset = asset_view_helper setup: -> - configure_content(opts.content_types).with(@) + configure_content(opts).with(@) .then(get_all_content) .tap(set_urls) .then(transform_entries) @@ -56,19 +56,41 @@ module.exports = (opts) -> * @return {Promise} - returns an array of configured content types ### - configure_content = (types) -> + configure_content = (opts) -> + types = opts.content_types + locales = opts.locales + + if locales is "*" # if locales is wildcard `*`, fetch & set locales + locales = fetch_all_locales() + if _.isPlainObject(types) then types = reconfigure_alt_type_config(types) + # duplicate & update type to contain locale's data + if _.isArray(locales) + for locale in locales + for t in types + unless t.locale? # type's locale overrides global locale + tmp = _.clone(t, true) # create clone + tmp.prefix = opts.locales_prefix[locale] or "#{locale}-" + types.push tmp # add to types + else + t.prefix ?= "#{t.locale}-" # set prefix, only if it isn't set + W.map types, (t) -> if not t.id then return W.reject(errors.no_type_id) t.filters ?= {} + if (not t.name || (t.template && not t.path)) return W client.contentType(t.id).then (res) -> t.name ?= pluralize(S(res.name).toLowerCase().underscore().s) + unless _.isUndefined t.prefix + t.name = t.prefix + t.name # add prefix + if t.template t.path ?= (e) -> "#{t.name}/#{S(e[res.displayField]).slugify().s}" return t return W.resolve(t) + ###* * Reconfigures content types set in app.coffee using an object instead of * an array. The keys of the object set as the `name` option in the config @@ -113,6 +135,19 @@ module.exports = (opts) -> ) ) + ###* + * Fetch all locales in space + * Used when `*` is used in opts.locales + * @return {Array} - returns array of locales + ### + + fetch_all_locales = -> + W(client.space()) + .then (res) -> + for locale in res.locales + locales.push locale.code + W.resolve locales + ###* * Formats raw response from Contentful * @param {Object} content - entries API response for a content type @@ -190,6 +225,7 @@ module.exports = (opts) -> * @return {Promise} - promise for when compilation is finished ### + # TODO remove prefix during compilation, from type.name compile_entries = (types) -> W.map types, (t) => if not t.template then return W.resolve() From 5d018077bda02d44e0d4f445fae4c3644e572941 Mon Sep 17 00:00:00 2001 From: Henry Snopek Date: Wed, 28 Oct 2015 15:18:18 -0500 Subject: [PATCH 03/23] update variables and move logic to proper spot --- lib/index.coffee | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/index.coffee b/lib/index.coffee index e742a9a..d2bc9b9 100644 --- a/lib/index.coffee +++ b/lib/index.coffee @@ -58,10 +58,10 @@ module.exports = (opts) -> configure_content = (opts) -> types = opts.content_types - locales = opts.locales + locales = opts.locale if locales is "*" # if locales is wildcard `*`, fetch & set locales - locales = fetch_all_locales() + locales = fetch_all_locales() # TODO Add Test for me if _.isPlainObject(types) then types = reconfigure_alt_type_config(types) # duplicate & update type to contain locale's data @@ -74,6 +74,8 @@ module.exports = (opts) -> types.push tmp # add to types else t.prefix ?= "#{t.locale}-" # set prefix, only if it isn't set + else + if _.isString opts.locale then global_locale = true W.map types, (t) -> if not t.id then return W.reject(errors.no_type_id) @@ -82,12 +84,17 @@ module.exports = (opts) -> if (not t.name || (t.template && not t.path)) return W client.contentType(t.id).then (res) -> t.name ?= pluralize(S(res.name).toLowerCase().underscore().s) - unless _.isUndefined t.prefix - t.name = t.prefix + t.name # add prefix if t.template t.path ?= (e) -> "#{t.name}/#{S(e[res.displayField]).slugify().s}" return t + + unless _.isUndefined t.prefix + t.name = t.prefix + t.name + t.path = t.prefix + t.path + + if global_locale then t.locale or= opts.locale + return W.resolve(t) @@ -144,6 +151,7 @@ module.exports = (opts) -> fetch_all_locales = -> W(client.space()) .then (res) -> + locales = [] for locale in res.locales locales.push locale.code W.resolve locales From 6a87b7011c363a2c613e1ec5f300445460c58707 Mon Sep 17 00:00:00 2001 From: Henry Snopek Date: Wed, 28 Oct 2015 15:18:50 -0500 Subject: [PATCH 04/23] update options --- test/fixtures/locale/app.coffee | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/fixtures/locale/app.coffee b/test/fixtures/locale/app.coffee index 08900ec..733be4f 100644 --- a/test/fixtures/locale/app.coffee +++ b/test/fixtures/locale/app.coffee @@ -6,6 +6,11 @@ module.exports = contentful( access_token: 'YOUR_ACCESS_TOKEN' space_id: 'aqzq2qya2jm4' + locale: ['en-us', 'en-ca'] + locale_prefix: + 'en-us': 'english-' + 'en-ca': 'french-' + 'en-br': 'spanish-' content_types: [ { id: '6BYT1gNiIEyIw8Og8aQAO6' @@ -13,7 +18,6 @@ module.exports = } { id: '7CDlVsacqQc88cmIEGYWMa' - locale: 'en-ES' } ] ) From 9e9ed02696ed13c903295b062cb15169ed6ce428 Mon Sep 17 00:00:00 2001 From: Henry Snopek Date: Fri, 30 Oct 2015 12:03:07 -0500 Subject: [PATCH 05/23] update code format --- lib/index.coffee | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/index.coffee b/lib/index.coffee index d2bc9b9..1313ca4 100644 --- a/lib/index.coffee +++ b/lib/index.coffee @@ -64,8 +64,8 @@ module.exports = (opts) -> locales = fetch_all_locales() # TODO Add Test for me if _.isPlainObject(types) then types = reconfigure_alt_type_config(types) - # duplicate & update type to contain locale's data - if _.isArray(locales) + + if _.isArray(locales) # duplicate & update type to contain locale's data for locale in locales for t in types unless t.locale? # type's locale overrides global locale From 18609a9aa7dddaafb8a876f6706012a488a68bc6 Mon Sep 17 00:00:00 2001 From: Henry Snopek Date: Fri, 30 Oct 2015 12:04:52 -0500 Subject: [PATCH 06/23] scaffold tests --- test/test.coffee | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/test.coffee b/test/test.coffee index cc46f31..9308af3 100644 --- a/test/test.coffee +++ b/test/test.coffee @@ -312,3 +312,17 @@ describe 'single entry views', -> h.file.contains(p, "#{@img_path}?w=100&h=100").should.be.true after -> unmock_contentful() + +describe 'locale', -> + it 'should render a global locale' + + it 'should render an array of global locales' + + it 'should render the content type locale, not the global' + + it 'should rendering using the correct prefix' + + it 'should fetch all locales from * wildcard' + + it 'should render using the correct template' + From fcab7cf0c78fa0bcd24f1471f4b3348e4a8e822d Mon Sep 17 00:00:00 2001 From: Henry Snopek Date: Fri, 30 Oct 2015 12:05:03 -0500 Subject: [PATCH 07/23] add locales --- test/fixtures/locale/app.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/fixtures/locale/app.coffee b/test/fixtures/locale/app.coffee index 733be4f..6d0ec16 100644 --- a/test/fixtures/locale/app.coffee +++ b/test/fixtures/locale/app.coffee @@ -14,7 +14,7 @@ module.exports = content_types: [ { id: '6BYT1gNiIEyIw8Og8aQAO6' - locale: 'en-ES' + locale: 'en-es' } { id: '7CDlVsacqQc88cmIEGYWMa' From 27e012cc02d71165a6bd80463f49b3cbf07565d0 Mon Sep 17 00:00:00 2001 From: Henry Snopek Date: Fri, 30 Oct 2015 18:04:23 -0500 Subject: [PATCH 08/23] add removal of duplicate types w/o a defined locale, add prefix --- lib/index.coffee | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/index.coffee b/lib/index.coffee index 1313ca4..b027eeb 100644 --- a/lib/index.coffee +++ b/lib/index.coffee @@ -59,9 +59,10 @@ module.exports = (opts) -> configure_content = (opts) -> types = opts.content_types locales = opts.locale + lPrefixes = opts.locales_prefix if locales is "*" # if locales is wildcard `*`, fetch & set locales - locales = fetch_all_locales() # TODO Add Test for me + locales = fetch_all_locales() if _.isPlainObject(types) then types = reconfigure_alt_type_config(types) @@ -70,10 +71,14 @@ module.exports = (opts) -> for t in types unless t.locale? # type's locale overrides global locale tmp = _.clone(t, true) # create clone - tmp.prefix = opts.locales_prefix[locale] or "#{locale}-" + tmp.locale = locale + tmp.prefix = lPrefixes?[locale] ? "#{locale}-" types.push tmp # add to types else t.prefix ?= "#{t.locale}-" # set prefix, only if it isn't set + + types = _.remove types, (t) -> t.locale? # remove duplicates w/o locale + else if _.isString opts.locale then global_locale = true @@ -90,6 +95,7 @@ module.exports = (opts) -> return t unless _.isUndefined t.prefix + console.log t, t.prefix t.name = t.prefix + t.name t.path = t.prefix + t.path From 524fc1be482e02d8de79cfde8b243425b20ef0e7 Mon Sep 17 00:00:00 2001 From: Henry Snopek Date: Wed, 4 Nov 2015 11:51:55 -0600 Subject: [PATCH 09/23] add async walkthru for grabbing wildcard (*) locale --- lib/index.coffee | 83 +++++++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 33 deletions(-) diff --git a/lib/index.coffee b/lib/index.coffee index b027eeb..9a7b7c4 100644 --- a/lib/index.coffee +++ b/lib/index.coffee @@ -61,47 +61,64 @@ module.exports = (opts) -> locales = opts.locale lPrefixes = opts.locales_prefix - if locales is "*" # if locales is wildcard `*`, fetch & set locales - locales = fetch_all_locales() + isWildcard = -> # if locales is wildcard `*`, fetch & set locales + return W( + if locales is "*" + fetch_all_locales().then (res) -> + locales = res + W.resolve locales + else + W.resolve + ) - if _.isPlainObject(types) then types = reconfigure_alt_type_config(types) + reconfigObj = -> + types = reconfigure_alt_type_config(types) if _.isPlainObject(types) - if _.isArray(locales) # duplicate & update type to contain locale's data - for locale in locales - for t in types - unless t.locale? # type's locale overrides global locale - tmp = _.clone(t, true) # create clone - tmp.locale = locale - tmp.prefix = lPrefixes?[locale] ? "#{locale}-" - types.push tmp # add to types - else - t.prefix ?= "#{t.locale}-" # set prefix, only if it isn't set + localesArray = -> + if _.isArray(locales) # duplicate & update type to contain locale's data + for locale in locales + for t in types + unless t.locale? # type's locale overrides global locale + tmp = _.clone(t, true) # create clone + tmp.locale = locale + tmp.prefix = lPrefixes?[locale] ? "#{locale}-" + types.push tmp # add to types + else + # set prefix, only if it isn't set + t.prefix ?= lPrefixes?[locale] ? "#{locale}-" - types = _.remove types, (t) -> t.locale? # remove duplicates w/o locale + types = _.remove types, (t) -> t.locale? # remove duplicates w/o locale + else + if _.isString opts.locale + global_locale = true - else - if _.isString opts.locale then global_locale = true + isWildcard() + .then reconfigObj + .then localesArray + .then -> + W.map types, (t) -> + if not t.id then return W.reject(errors.no_type_id) + t.filters ?= {} - W.map types, (t) -> - if not t.id then return W.reject(errors.no_type_id) - t.filters ?= {} + if (not t.name || (t.template && not t.path)) + return W client.contentType(t.id).then (res) -> + t.name ?= pluralize(S(res.name).toLowerCase().underscore().s) - if (not t.name || (t.template && not t.path)) - return W client.contentType(t.id).then (res) -> - t.name ?= pluralize(S(res.name).toLowerCase().underscore().s) + unless _.isUndefined t.prefix + t.name = t.prefix + t.name - if t.template - t.path ?= (e) -> "#{t.name}/#{S(e[res.displayField]).slugify().s}" - return t + if t.template or lPrefixes? + t.path ?= (e) -> "#{t.name}/#{S(e[res.displayField]).slugify().s}" - unless _.isUndefined t.prefix - console.log t, t.prefix - t.name = t.prefix + t.name - t.path = t.prefix + t.path + t.name = _.trimLeft(t.name, t.prefix) + return t - if global_locale then t.locale or= opts.locale + unless _.isUndefined t.prefix + t.name = t.prefix + t.name - return W.resolve(t) + if global_locale? then t.locale or= opts.locale + + return W.resolve(t) ###* @@ -155,12 +172,13 @@ module.exports = (opts) -> ### fetch_all_locales = -> - W(client.space()) + W(client.space() .then (res) -> locales = [] for locale in res.locales locales.push locale.code W.resolve locales + ) ###* * Formats raw response from Contentful @@ -239,7 +257,6 @@ module.exports = (opts) -> * @return {Promise} - promise for when compilation is finished ### - # TODO remove prefix during compilation, from type.name compile_entries = (types) -> W.map types, (t) => if not t.template then return W.resolve() From 2c67d6ab1675aff208a6e0e7c5fe197a2fac8699 Mon Sep 17 00:00:00 2001 From: Henry Snopek Date: Wed, 4 Nov 2015 11:52:20 -0600 Subject: [PATCH 10/23] remove locale --- test/fixtures/basic/app.coffee | 1 - 1 file changed, 1 deletion(-) diff --git a/test/fixtures/basic/app.coffee b/test/fixtures/basic/app.coffee index bd307a0..b303f3c 100644 --- a/test/fixtures/basic/app.coffee +++ b/test/fixtures/basic/app.coffee @@ -9,7 +9,6 @@ module.exports = content_types: [ { id: '6BYT1gNiIEyIw8Og8aQAO6' - locale: 'en-ES' } { id: '7CDlVsacqQc88cmIEGYWMa' From 7e1743004d8771f7d6cdb3995a647f4319924262 Mon Sep 17 00:00:00 2001 From: Henry Snopek Date: Wed, 4 Nov 2015 11:53:32 -0600 Subject: [PATCH 11/23] split tests further --- test/fixtures/{locale => locale_setup}/about.jade | 0 test/fixtures/{locale => locale_setup}/app.coffee | 10 +--------- test/fixtures/{locale => locale_setup}/index.jade | 0 test/fixtures/{locale => locale_setup}/package.json | 0 4 files changed, 1 insertion(+), 9 deletions(-) rename test/fixtures/{locale => locale_setup}/about.jade (100%) rename test/fixtures/{locale => locale_setup}/app.coffee (56%) rename test/fixtures/{locale => locale_setup}/index.jade (100%) rename test/fixtures/{locale => locale_setup}/package.json (100%) diff --git a/test/fixtures/locale/about.jade b/test/fixtures/locale_setup/about.jade similarity index 100% rename from test/fixtures/locale/about.jade rename to test/fixtures/locale_setup/about.jade diff --git a/test/fixtures/locale/app.coffee b/test/fixtures/locale_setup/app.coffee similarity index 56% rename from test/fixtures/locale/app.coffee rename to test/fixtures/locale_setup/app.coffee index 6d0ec16..572318d 100644 --- a/test/fixtures/locale/app.coffee +++ b/test/fixtures/locale_setup/app.coffee @@ -6,18 +6,10 @@ module.exports = contentful( access_token: 'YOUR_ACCESS_TOKEN' space_id: 'aqzq2qya2jm4' - locale: ['en-us', 'en-ca'] - locale_prefix: - 'en-us': 'english-' - 'en-ca': 'french-' - 'en-br': 'spanish-' + locale: '*' content_types: [ { id: '6BYT1gNiIEyIw8Og8aQAO6' - locale: 'en-es' - } - { - id: '7CDlVsacqQc88cmIEGYWMa' } ] ) diff --git a/test/fixtures/locale/index.jade b/test/fixtures/locale_setup/index.jade similarity index 100% rename from test/fixtures/locale/index.jade rename to test/fixtures/locale_setup/index.jade diff --git a/test/fixtures/locale/package.json b/test/fixtures/locale_setup/package.json similarity index 100% rename from test/fixtures/locale/package.json rename to test/fixtures/locale_setup/package.json From 672c4447b352b380e65d852d068ca6ac35d3c619 Mon Sep 17 00:00:00 2001 From: Henry Snopek Date: Wed, 4 Nov 2015 11:54:03 -0600 Subject: [PATCH 12/23] add locales support to mockery, add initial setup support, scaffold other tests --- test/test.coffee | 77 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 70 insertions(+), 7 deletions(-) diff --git a/test/test.coffee b/test/test.coffee index 9308af3..c9fdb7e 100644 --- a/test/test.coffee +++ b/test/test.coffee @@ -19,10 +19,20 @@ mock_contentful = (opts = {}) -> entries: [ sys: sys: 'data' + locale: 'Default Locale' fields: title: 'Default Title' body: 'Default Body' ] + space: + sys: + type: "Space", + id: "cfexampleapi" + name: "Contentful Example API", + locales: [ + {code: "en-US", name: "English"}, + {code: "tlh", name: "Klingon"} + ] content_type: name: 'Blog Post' displayField: 'title' @@ -30,7 +40,14 @@ mock_contentful = (opts = {}) -> mockery.registerMock 'contentful', createClient: -> contentType: -> W.resolve(opts.content_type) - entries: -> W.resolve(opts.entries) + space: -> W.resolve(opts.space) + entries: (req) -> + if _.isUndefined req.locale + W.resolve(opts.entries) + else + W.resolve opts.entries.filter (entry) -> + return entry.sys.locale is req.locale + unmock_contentful = -> mockery.deregisterAll() @@ -314,15 +331,61 @@ describe 'single entry views', -> after -> unmock_contentful() describe 'locale', -> - it 'should render a global locale' + describe.only 'setup', -> + before (done) -> + @title = ['Throw Some Ds', '\'op Ds chuH', 'arrojar algo de Ds\''] + @body = [ + 'Rich Boy selling crack', + 'mIp loDHom ngev pe\'vIl vaj pumDI\' qoghlIj' + 'Niño rico venta de crack' + ] + mock_contentful( + entries: [ + { + fields: [{title: @title[0], body: @body[0]}] + sys: + locale: 'en-US' + } + { + fields: [{title: @title[1], body: @body[1]}], + sys: + locale: 'tlh' + } + { + fields: [{title: @title[2], body: @body[2]}], + sys: + locale: 'en-es' + } + ], + space: + locales: [ + { code: 'en-US', name: 'English' }, + { code: 'tlh', name: 'Klingon' }, + { code: 'en-es', name: 'Spanish' } + ] + ) + compile_fixture.call(@, 'locale_setup').then(-> done()).catch(done) + + it 'should fetch all locales from * wildcard', -> + p = path.join @public, 'index.html' + h.file.contains p, 'Throw Some Ds' + .should.be.true + + after -> unmock_contentful() + + describe 'global', -> + it 'should render a single global locale' - it 'should render an array of global locales' + it 'should render an array of global locales' - it 'should render the content type locale, not the global' + it 'should render the content type locale, not the global' - it 'should rendering using the correct prefix' + describe 'scoped locale', -> + it 'should render scope locale' - it 'should fetch all locales from * wildcard' + describe 'locales_prefix', -> + it 'should render using the correct template' - it 'should render using the correct template' + describe 'complex locale', -> + it 'should rendering using the correct prefix' From 3c0d388e16a9d64383a991e496a64e56e2d80ad0 Mon Sep 17 00:00:00 2001 From: Henry Snopek Date: Wed, 4 Nov 2015 12:44:20 -0600 Subject: [PATCH 13/23] update set_locales so we don't override previous locale --- lib/index.coffee | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/index.coffee b/lib/index.coffee index 9a7b7c4..14808ca 100644 --- a/lib/index.coffee +++ b/lib/index.coffee @@ -113,8 +113,8 @@ module.exports = (opts) -> t.name = _.trimLeft(t.name, t.prefix) return t - unless _.isUndefined t.prefix - t.name = t.prefix + t.name + #unless _.isUndefined t.prefix + # t.name = t.prefix + t.name if global_locale? then t.locale or= opts.locale @@ -196,7 +196,7 @@ module.exports = (opts) -> format_entry = (e) -> if _.has(e.fields, 'sys') then return W.reject(errors.sys_conflict) - _.assign(_.omit(e, 'fields'), e.fields) + _.assign(_.omit(_.omit(e, 'sys'), 'fields'), e.fields) ###* * Sets `_url` and `_urls` properties on content with single entry views @@ -222,7 +222,10 @@ module.exports = (opts) -> set_locals = (types) -> W.map types, (t) => - @roots.config.locals.contentful[t.name] = t.content + if @roots.config.locals.contentful[t.name] + @roots.config.locals.contentful[t.name].push t.content[0] + else + @roots.config.locals.contentful[t.name] = t.content ###* * Transforms every type with content with the user provided callback From e678629ece2a0e14d0de41707d5c2c0d97126fd1 Mon Sep 17 00:00:00 2001 From: Henry Snopek Date: Wed, 4 Nov 2015 12:45:05 -0600 Subject: [PATCH 14/23] update entry format, remove array wrapper --- test/test.coffee | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/test.coffee b/test/test.coffee index c9fdb7e..085040f 100644 --- a/test/test.coffee +++ b/test/test.coffee @@ -331,7 +331,7 @@ describe 'single entry views', -> after -> unmock_contentful() describe 'locale', -> - describe.only 'setup', -> + describe 'setup', -> before (done) -> @title = ['Throw Some Ds', '\'op Ds chuH', 'arrojar algo de Ds\''] @body = [ @@ -342,17 +342,17 @@ describe 'locale', -> mock_contentful( entries: [ { - fields: [{title: @title[0], body: @body[0]}] + fields: {title: @title[0], body: @body[0]} sys: locale: 'en-US' } { - fields: [{title: @title[1], body: @body[1]}], + fields: {title: @title[1], body: @body[1]}, sys: locale: 'tlh' } { - fields: [{title: @title[2], body: @body[2]}], + fields: {title: @title[2], body: @body[2]}, sys: locale: 'en-es' } From 40ad19c652fb8f0d6bf7f1d3a23d7d0288903d88 Mon Sep 17 00:00:00 2001 From: Henry Snopek Date: Wed, 4 Nov 2015 12:47:55 -0600 Subject: [PATCH 15/23] update setup to iterate thru all entries --- test/test.coffee | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/test.coffee b/test/test.coffee index 085040f..dc2142e 100644 --- a/test/test.coffee +++ b/test/test.coffee @@ -368,8 +368,11 @@ describe 'locale', -> it 'should fetch all locales from * wildcard', -> p = path.join @public, 'index.html' - h.file.contains p, 'Throw Some Ds' - .should.be.true + for title, i in @title + h.file.contains p, title + .should.be.true + h.file.contains p, @body[i] + .should.be.true after -> unmock_contentful() From c06c46d1e5bb757b3722d438b8f71c1d4cdc3141 Mon Sep 17 00:00:00 2001 From: Henry Snopek Date: Wed, 4 Nov 2015 13:22:02 -0600 Subject: [PATCH 16/23] set_locals: logic refactor --- lib/index.coffee | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/index.coffee b/lib/index.coffee index 14808ca..07967b2 100644 --- a/lib/index.coffee +++ b/lib/index.coffee @@ -221,11 +221,10 @@ module.exports = (opts) -> ### set_locals = (types) -> + contentful = @roots.config.locals.contentful W.map types, (t) => - if @roots.config.locals.contentful[t.name] - @roots.config.locals.contentful[t.name].push t.content[0] - else - @roots.config.locals.contentful[t.name] = t.content + if contentful[t.name] then contentful[t.name].push t.content[0] + else contentful[t.name] = t.content ###* * Transforms every type with content with the user provided callback From d2dbeffb429940bf187aa79f6cc4ccf1b42b934c Mon Sep 17 00:00:00 2001 From: Henry Snopek Date: Wed, 4 Nov 2015 18:26:18 -0600 Subject: [PATCH 17/23] update prefix usage --- lib/index.coffee | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/index.coffee b/lib/index.coffee index 07967b2..70def92 100644 --- a/lib/index.coffee +++ b/lib/index.coffee @@ -81,11 +81,11 @@ module.exports = (opts) -> unless t.locale? # type's locale overrides global locale tmp = _.clone(t, true) # create clone tmp.locale = locale - tmp.prefix = lPrefixes?[locale] ? "#{locale}-" + tmp.prefix = lPrefixes?[locale] ? "#{locale.replace(/-/,'_')}_" types.push tmp # add to types else # set prefix, only if it isn't set - t.prefix ?= lPrefixes?[locale] ? "#{locale}-" + t.prefix ?= lPrefixes?[locale] ? "#{locale.replace(/-/,'_')}_" types = _.remove types, (t) -> t.locale? # remove duplicates w/o locale else @@ -104,17 +104,16 @@ module.exports = (opts) -> return W client.contentType(t.id).then (res) -> t.name ?= pluralize(S(res.name).toLowerCase().underscore().s) - unless _.isUndefined t.prefix + unless _.isUndefined lPrefixes t.name = t.prefix + t.name if t.template or lPrefixes? t.path ?= (e) -> "#{t.name}/#{S(e[res.displayField]).slugify().s}" - t.name = _.trimLeft(t.name, t.prefix) return t - #unless _.isUndefined t.prefix - # t.name = t.prefix + t.name + unless _.isUndefined lPrefixes + t.name = t.prefix + t.name if global_locale? then t.locale or= opts.locale @@ -223,6 +222,7 @@ module.exports = (opts) -> set_locals = (types) -> contentful = @roots.config.locals.contentful W.map types, (t) => + console.log t.name, JSON.stringify t.content if contentful[t.name] then contentful[t.name].push t.content[0] else contentful[t.name] = t.content From c77d646ff22a081ad4b7f20ae372d18a2665a89a Mon Sep 17 00:00:00 2001 From: Henry Snopek Date: Wed, 4 Nov 2015 18:27:49 -0600 Subject: [PATCH 18/23] add global locale/locales_prefix --- test/fixtures/locale_global/about.jade | 1 + test/fixtures/locale_global/app.coffee | 17 ++ test/fixtures/locale_global/index.jade | 5 + test/fixtures/locale_global/package.json | 6 + test/fixtures/locale_multi/app.coffee | 16 ++ test/fixtures/locale_multi/index.jade | 5 + test/fixtures/locale_multi/package.json | 6 + test/fixtures/locale_prefix/app.coffee | 19 +++ test/fixtures/locale_prefix/klingon.jade | 5 + test/fixtures/locale_prefix/package.json | 6 + test/fixtures/locale_prefix/spanish.jade | 5 + test/fixtures/locale_scope/about.jade | 1 + test/fixtures/locale_scope/app.coffee | 17 ++ test/fixtures/locale_scope/index.jade | 5 + test/fixtures/locale_scope/package.json | 6 + test/fixtures/locale_single/about.jade | 1 + test/fixtures/locale_single/app.coffee | 16 ++ test/fixtures/locale_single/index.jade | 5 + test/fixtures/locale_single/package.json | 6 + test/test.coffee | 194 ++++++++++++++++++++++- 20 files changed, 335 insertions(+), 7 deletions(-) create mode 100644 test/fixtures/locale_global/about.jade create mode 100644 test/fixtures/locale_global/app.coffee create mode 100644 test/fixtures/locale_global/index.jade create mode 100644 test/fixtures/locale_global/package.json create mode 100644 test/fixtures/locale_multi/app.coffee create mode 100644 test/fixtures/locale_multi/index.jade create mode 100644 test/fixtures/locale_multi/package.json create mode 100644 test/fixtures/locale_prefix/app.coffee create mode 100644 test/fixtures/locale_prefix/klingon.jade create mode 100644 test/fixtures/locale_prefix/package.json create mode 100644 test/fixtures/locale_prefix/spanish.jade create mode 100644 test/fixtures/locale_scope/about.jade create mode 100644 test/fixtures/locale_scope/app.coffee create mode 100644 test/fixtures/locale_scope/index.jade create mode 100644 test/fixtures/locale_scope/package.json create mode 100644 test/fixtures/locale_single/about.jade create mode 100644 test/fixtures/locale_single/app.coffee create mode 100644 test/fixtures/locale_single/index.jade create mode 100644 test/fixtures/locale_single/package.json diff --git a/test/fixtures/locale_global/about.jade b/test/fixtures/locale_global/about.jade new file mode 100644 index 0000000..8240f0e --- /dev/null +++ b/test/fixtures/locale_global/about.jade @@ -0,0 +1 @@ +h1 wow diff --git a/test/fixtures/locale_global/app.coffee b/test/fixtures/locale_global/app.coffee new file mode 100644 index 0000000..b26ecce --- /dev/null +++ b/test/fixtures/locale_global/app.coffee @@ -0,0 +1,17 @@ +contentful = require '../../..' + +module.exports = + ignores: ["**/_*", "**/.DS_Store"] + extensions: [ + contentful( + access_token: 'YOUR_ACCESS_TOKEN' + space_id: 'aqzq2qya2jm4' + locale: ['en-US'] + content_types: [ + { + id: '6BYT1gNiIEyIw8Og8aQAO6' + locale: 'en-es' + } + ] + ) + ] diff --git a/test/fixtures/locale_global/index.jade b/test/fixtures/locale_global/index.jade new file mode 100644 index 0000000..4769500 --- /dev/null +++ b/test/fixtures/locale_global/index.jade @@ -0,0 +1,5 @@ +ul + - for p in contentful.blog_posts + li + h1= p.title + p= p.body diff --git a/test/fixtures/locale_global/package.json b/test/fixtures/locale_global/package.json new file mode 100644 index 0000000..2d0ae2c --- /dev/null +++ b/test/fixtures/locale_global/package.json @@ -0,0 +1,6 @@ +{ + "name": "test", + "dependencies": { + "jade": "*" + } +} diff --git a/test/fixtures/locale_multi/app.coffee b/test/fixtures/locale_multi/app.coffee new file mode 100644 index 0000000..647c89c --- /dev/null +++ b/test/fixtures/locale_multi/app.coffee @@ -0,0 +1,16 @@ +contentful = require '../../..' + +module.exports = + ignores: ["**/_*", "**/.DS_Store"] + extensions: [ + contentful( + access_token: 'YOUR_ACCESS_TOKEN' + space_id: 'aqzq2qya2jm4' + locale: ['en-es', 'tlh'] + content_types: [ + { + id: '6BYT1gNiIEyIw8Og8aQAO6' + } + ] + ) + ] diff --git a/test/fixtures/locale_multi/index.jade b/test/fixtures/locale_multi/index.jade new file mode 100644 index 0000000..4769500 --- /dev/null +++ b/test/fixtures/locale_multi/index.jade @@ -0,0 +1,5 @@ +ul + - for p in contentful.blog_posts + li + h1= p.title + p= p.body diff --git a/test/fixtures/locale_multi/package.json b/test/fixtures/locale_multi/package.json new file mode 100644 index 0000000..2d0ae2c --- /dev/null +++ b/test/fixtures/locale_multi/package.json @@ -0,0 +1,6 @@ +{ + "name": "test", + "dependencies": { + "jade": "*" + } +} diff --git a/test/fixtures/locale_prefix/app.coffee b/test/fixtures/locale_prefix/app.coffee new file mode 100644 index 0000000..45224d2 --- /dev/null +++ b/test/fixtures/locale_prefix/app.coffee @@ -0,0 +1,19 @@ +contentful = require '../../..' + +module.exports = + ignores: ["**/_*", "**/.DS_Store"] + extensions: [ + contentful( + access_token: 'YOUR_ACCESS_TOKEN' + space_id: 'aqzq2qya2jm4' + locale: ['en-es', 'tlh'] + locales_prefix: { + 'tlh': 'klingon_' + } + content_types: [ + { + id: '6BYT1gNiIEyIw8Og8aQAO6' + } + ] + ) + ] diff --git a/test/fixtures/locale_prefix/klingon.jade b/test/fixtures/locale_prefix/klingon.jade new file mode 100644 index 0000000..9c7fe36 --- /dev/null +++ b/test/fixtures/locale_prefix/klingon.jade @@ -0,0 +1,5 @@ +ul + - for p in contentful.klingon_blog_posts + li + h1= p.title + p= p.body diff --git a/test/fixtures/locale_prefix/package.json b/test/fixtures/locale_prefix/package.json new file mode 100644 index 0000000..2d0ae2c --- /dev/null +++ b/test/fixtures/locale_prefix/package.json @@ -0,0 +1,6 @@ +{ + "name": "test", + "dependencies": { + "jade": "*" + } +} diff --git a/test/fixtures/locale_prefix/spanish.jade b/test/fixtures/locale_prefix/spanish.jade new file mode 100644 index 0000000..620ac70 --- /dev/null +++ b/test/fixtures/locale_prefix/spanish.jade @@ -0,0 +1,5 @@ +ul + - for p in contentful.en_es_blog_posts + li + h1= p.title + p= p.body diff --git a/test/fixtures/locale_scope/about.jade b/test/fixtures/locale_scope/about.jade new file mode 100644 index 0000000..8240f0e --- /dev/null +++ b/test/fixtures/locale_scope/about.jade @@ -0,0 +1 @@ +h1 wow diff --git a/test/fixtures/locale_scope/app.coffee b/test/fixtures/locale_scope/app.coffee new file mode 100644 index 0000000..3710671 --- /dev/null +++ b/test/fixtures/locale_scope/app.coffee @@ -0,0 +1,17 @@ +contentful = require '../../..' + +module.exports = + ignores: ["**/_*", "**/.DS_Store"] + extensions: [ + contentful( + access_token: 'YOUR_ACCESS_TOKEN' + space_id: 'aqzq2qya2jm4' + locale: ['tlh'] + content_types: [ + { + id: '6BYT1gNiIEyIw8Og8aQAO6' + locale: 'en-es' + } + ] + ) + ] diff --git a/test/fixtures/locale_scope/index.jade b/test/fixtures/locale_scope/index.jade new file mode 100644 index 0000000..4769500 --- /dev/null +++ b/test/fixtures/locale_scope/index.jade @@ -0,0 +1,5 @@ +ul + - for p in contentful.blog_posts + li + h1= p.title + p= p.body diff --git a/test/fixtures/locale_scope/package.json b/test/fixtures/locale_scope/package.json new file mode 100644 index 0000000..2d0ae2c --- /dev/null +++ b/test/fixtures/locale_scope/package.json @@ -0,0 +1,6 @@ +{ + "name": "test", + "dependencies": { + "jade": "*" + } +} diff --git a/test/fixtures/locale_single/about.jade b/test/fixtures/locale_single/about.jade new file mode 100644 index 0000000..8240f0e --- /dev/null +++ b/test/fixtures/locale_single/about.jade @@ -0,0 +1 @@ +h1 wow diff --git a/test/fixtures/locale_single/app.coffee b/test/fixtures/locale_single/app.coffee new file mode 100644 index 0000000..56e976f --- /dev/null +++ b/test/fixtures/locale_single/app.coffee @@ -0,0 +1,16 @@ +contentful = require '../../..' + +module.exports = + ignores: ["**/_*", "**/.DS_Store"] + extensions: [ + contentful( + access_token: 'YOUR_ACCESS_TOKEN' + space_id: 'aqzq2qya2jm4' + locale: ['tlh'] + content_types: [ + { + id: '6BYT1gNiIEyIw8Og8aQAO6' + } + ] + ) + ] diff --git a/test/fixtures/locale_single/index.jade b/test/fixtures/locale_single/index.jade new file mode 100644 index 0000000..4769500 --- /dev/null +++ b/test/fixtures/locale_single/index.jade @@ -0,0 +1,5 @@ +ul + - for p in contentful.blog_posts + li + h1= p.title + p= p.body diff --git a/test/fixtures/locale_single/package.json b/test/fixtures/locale_single/package.json new file mode 100644 index 0000000..2d0ae2c --- /dev/null +++ b/test/fixtures/locale_single/package.json @@ -0,0 +1,6 @@ +{ + "name": "test", + "dependencies": { + "jade": "*" + } +} diff --git a/test/test.coffee b/test/test.coffee index dc2142e..9f3a42a 100644 --- a/test/test.coffee +++ b/test/test.coffee @@ -330,7 +330,7 @@ describe 'single entry views', -> after -> unmock_contentful() -describe 'locale', -> +describe.only 'locale', -> describe 'setup', -> before (done) -> @title = ['Throw Some Ds', '\'op Ds chuH', 'arrojar algo de Ds\''] @@ -377,17 +377,197 @@ describe 'locale', -> after -> unmock_contentful() describe 'global', -> - it 'should render a single global locale' + describe 'single locale', -> + before (done) -> + @title = ['Throw Some Ds', '\'op Ds chuH', 'arrojar algo de Ds\''] + @body = [ + 'Rich Boy selling crack', + 'mIp loDHom ngev pe\'vIl vaj pumDI\' qoghlIj' + 'Niño rico venta de crack' + ] + mock_contentful( + entries: [ + { + fields: {title: @title[0], body: @body[0]} + sys: + locale: 'en-US' + } + { + fields: {title: @title[1], body: @body[1]}, + sys: + locale: 'tlh' + } + { + fields: {title: @title[2], body: @body[2]}, + sys: + locale: 'en-es' + } + ], + space: + locales: [ + { code: 'en-US', name: 'English' }, + { code: 'tlh', name: 'Klingon' }, + { code: 'en-es', name: 'Spanish' } + ] + ) + compile_fixture.call(@, 'locale_single').then(-> done()).catch(done) + + it 'should render a single global', -> + p = path.join @public, 'index.html' + h.file.contains p, @title[0] + .should.be.false + h.file.contains p, @title[1] + .should.be.true - it 'should render an array of global locales' + after -> unmock_contentful() + + describe 'array of locales', -> + before (done) -> + @title = ['Throw Some Ds', '\'op Ds chuH', 'ajrrojar algo de Ds\''] + @body = [ + 'Rich Boy selling crack', + 'mIp loDHom ngev pe\'vIl vaj pumDI\' qoghlIj' + 'Niño rico venta de crack' + ] + mock_contentful( + entries: [ + { + fields: {title: @title[0], body: @body[0]} + sys: + locale: 'en-US' + } + { + fields: {title: @title[1], body: @body[1]}, + sys: + locale: 'tlh' + } + { + fields: {title: @title[2], body: @body[2]}, + sys: + locale: 'en-es' + } + ], + space: + locales: [ + { code: 'en-US', name: 'English' }, + { code: 'tlh', name: 'Klingon' }, + { code: 'en-es', name: 'Spanish' } + ] + ) + compile_fixture.call(@, 'locale_multi').then(-> done()).catch(done) + + + it 'should render an array of global locales', -> + p = path.join @public, 'index.html' + h.file.contains p, @title[1] + .should.be.true + h.file.contains p, @title[2] + .should.be.true + h.file.contains p, @title[0] + .should.be.false - it 'should render the content type locale, not the global' + after -> unmock_contentful() - describe 'scoped locale', -> - it 'should render scope locale' + describe 'scoped', -> + before (done) -> + @title = ['Throw Some Ds', '\'op Ds chuH', 'arrojar algo de Ds\''] + @body = [ + 'Rich Boy selling crack', + 'mIp loDHom ngev pe\'vIl vaj pumDI\' qoghlIj' + 'Niño rico venta de crack' + ] + mock_contentful( + entries: [ + { + fields: {title: @title[0], body: @body[0]} + sys: + locale: 'en-US' + } + { + fields: {title: @title[1], body: @body[1]}, + sys: + locale: 'tlh' + } + { + fields: {title: @title[2], body: @body[2]}, + sys: + locale: 'en-es' + } + ], + space: + locales: [ + { code: 'en-US', name: 'English' }, + { code: 'tlh', name: 'Klingon' }, + { code: 'en-es', name: 'Spanish' } + ] + ) + compile_fixture.call(@, 'locale_scope').then(-> done()).catch(done) + + it 'should render the content type locale, not the global', -> + p = path.join @public, 'index.html' + h.file.contains p, @title[2] + .should.be.true + h.file.contains p, @body[2] + .should.be.true + h.file.contains p, @title[1] + .should.be.false + h.file.contains p, @body[1] + .should.be.false + + after -> unmock_contentful() describe 'locales_prefix', -> - it 'should render using the correct template' + before (done) -> + @title = ['Throw Some Ds', '\'op Ds chuH', 'arrojar algo de Ds\''] + @body = [ + 'Rich Boy selling crack', + 'mIp loDHom ngev pe\'vIl vaj pumDI\' qoghlIj' + 'Niño rico venta de crack' + ] + mock_contentful( + entries: [ + { + fields: {title: @title[0], body: @body[0]} + sys: + locale: 'en-US' + } + { + fields: {title: @title[1], body: @body[1]}, + sys: + locale: 'tlh' + } + { + fields: {title: @title[2], body: @body[2]}, + sys: + locale: 'en-es' + } + ], + space: + locales: [ + { code: 'en-US', name: 'English' }, + { code: 'tlh', name: 'Klingon' }, + { code: 'en-es', name: 'Spanish' } + ] + ) + compile_fixture.call(@, 'locale_prefix').then(-> done()).catch(done) + + it 'should render using the correct template', -> + klingon = path.join @public, 'klingon.html' + spanish = path.join @public, 'spanish.html' + + h.file.contains klingon, @title[1] + .should.be.true + h.file.contains klingon, @body[1] + .should.be.true + h.file.contains klingon, @body[2] + .should.be.false + + h.file.contains spanish, @title[2] + .should.be.true + h.file.contains spanish, @body[2] + .should.be.true + h.file.contains spanish, @body[1] + .should.be.false describe 'complex locale', -> it 'should rendering using the correct prefix' From 424436a4b89aa611ab47f4ded378ae31916af4e9 Mon Sep 17 00:00:00 2001 From: Henry Snopek Date: Thu, 5 Nov 2015 16:37:58 -0600 Subject: [PATCH 19/23] remove complex test --- test/test.coffee | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/test.coffee b/test/test.coffee index 9f3a42a..83c3e38 100644 --- a/test/test.coffee +++ b/test/test.coffee @@ -569,6 +569,3 @@ describe.only 'locale', -> h.file.contains spanish, @body[1] .should.be.false - describe 'complex locale', -> - it 'should rendering using the correct prefix' - From c823cab59e88c4f4a9085cf90c84ea198deaf657 Mon Sep 17 00:00:00 2001 From: Henry Snopek Date: Thu, 5 Nov 2015 16:38:09 -0600 Subject: [PATCH 20/23] remove console.log --- lib/index.coffee | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/index.coffee b/lib/index.coffee index 70def92..c555e95 100644 --- a/lib/index.coffee +++ b/lib/index.coffee @@ -222,7 +222,6 @@ module.exports = (opts) -> set_locals = (types) -> contentful = @roots.config.locals.contentful W.map types, (t) => - console.log t.name, JSON.stringify t.content if contentful[t.name] then contentful[t.name].push t.content[0] else contentful[t.name] = t.content From e618e674e9e0ad348c127b91b48da6cc21822d7e Mon Sep 17 00:00:00 2001 From: Henry Snopek Date: Thu, 5 Nov 2015 16:38:43 -0600 Subject: [PATCH 21/23] remove .only --- test/test.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.coffee b/test/test.coffee index 83c3e38..3d16814 100644 --- a/test/test.coffee +++ b/test/test.coffee @@ -330,7 +330,7 @@ describe 'single entry views', -> after -> unmock_contentful() -describe.only 'locale', -> +describe 'locale', -> describe 'setup', -> before (done) -> @title = ['Throw Some Ds', '\'op Ds chuH', 'arrojar algo de Ds\''] From 90f5066881176a0ba64f22f46c3bea4b11f7a948 Mon Sep 17 00:00:00 2001 From: kylemac Date: Mon, 9 Nov 2015 15:41:55 -0500 Subject: [PATCH 22/23] minor syntax cleanup --- lib/index.coffee | 17 +++++++++-------- test/test.coffee | 3 +-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/index.coffee b/lib/index.coffee index c555e95..7faa3a9 100644 --- a/lib/index.coffee +++ b/lib/index.coffee @@ -87,7 +87,7 @@ module.exports = (opts) -> # set prefix, only if it isn't set t.prefix ?= lPrefixes?[locale] ? "#{locale.replace(/-/,'_')}_" - types = _.remove types, (t) -> t.locale? # remove duplicates w/o locale + types = _.remove types, (t) -> t.locale? # remove dupes w/o locale else if _.isString opts.locale global_locale = true @@ -108,7 +108,8 @@ module.exports = (opts) -> t.name = t.prefix + t.name if t.template or lPrefixes? - t.path ?= (e) -> "#{t.name}/#{S(e[res.displayField]).slugify().s}" + t.path ?= (e) -> + "#{t.name}/#{S(e[res.displayField]).slugify().s}" return t @@ -221,7 +222,7 @@ module.exports = (opts) -> set_locals = (types) -> contentful = @roots.config.locals.contentful - W.map types, (t) => + W.map types, (t) -> if contentful[t.name] then contentful[t.name].push t.content[0] else contentful[t.name] = t.content @@ -232,9 +233,9 @@ module.exports = (opts) -> ### transform_entries = (types) -> - W.map types, (t) => + W.map types, (t) -> if t.transform - W.map t.content, (entry) => + W.map t.content, (entry) -> W(entry, t.transform) W.resolve(t) @@ -245,10 +246,10 @@ module.exports = (opts) -> ### sort_entries = (types) -> - W.map types, (t) => + W.map types, (t) -> if t.sort - # Unfortunately, in order to sort promises we have to resolve them first. - W.all(t.content).then (data) => + # In order to sort promises we have to resolve them first. + W.all(t.content).then (data) -> t.content = data.sort(t.sort) W.resolve(t) diff --git a/test/test.coffee b/test/test.coffee index 3d16814..038f61d 100644 --- a/test/test.coffee +++ b/test/test.coffee @@ -260,7 +260,7 @@ describe 'single entry views', -> after -> unmock_contentful() - it 'should not have first entry\'s content in second entries single view', -> + it 'should not have first entry\'s content in 2nd entries single view', -> p = path.join(@public, "blog_posts/#{S(@title_2).slugify().s}.html") h.file.contains(p, @body).should.not.be.true @@ -568,4 +568,3 @@ describe 'locale', -> .should.be.true h.file.contains spanish, @body[1] .should.be.false - From a8fc894134de1544d913e34b243b442567129c27 Mon Sep 17 00:00:00 2001 From: Henry Snopek Date: Wed, 11 Nov 2015 10:41:47 -0600 Subject: [PATCH 23/23] add locale documentation --- readme.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/readme.md b/readme.md index 471eed4..119a110 100644 --- a/readme.md +++ b/readme.md @@ -30,6 +30,9 @@ module.exports = contentful access_token: 'YOUR_ACCESS_TOKEN' space_id: 'xxxxxx' + locale: 'tlh' + locales_prefix: + tlh: 'klingon_' content_types: blog_posts: id: 'xxxxxx' @@ -104,6 +107,29 @@ Required. The space ID containing the content you wish to retrieve. Optional. (Boolean) Allows you use the Contentful Preview API. Also able to be accessed by setting the environment variable `CONTENTFUL_ENV` to `"develop"` (preview api) or `"production"` (default cdn). +#### locales +Locales allow you to request your content in a different language, `tlh` is Klingon. + +##### Global locale +Optional. (String or Array) Defines locale for all content_types. +String: `'tlh'` +Array: `['en-es', 'tlh']` +Wildcard: `'*'` - grabs all locales from contentful + +##### content_type specific locale +Optional. (String) Define content_types locale, will override global locale. Add `locale: 'tlh'` to any of your content types and you'll retrieve that post in the specific locale. + +#### locales_prefix +Optional. (Object) Defines the prefix given to a group of locales. + +``` +locales_prefix: + 'tlh': 'klingon_' + 'en-es': 'spanish_' +``` + +Lets say you have 3 global locales defined, `['tlh', 'en-us', 'en-es']`, and above is our defined locales_prefix. Since we did not declare `'en-us'` you can access it with `contentful.en_us_blog_posts`. Similarly you can access each preix according to what you've set in the object above. `'tlh'` would be accessible by `contentful.klingon_blog_posts` and `en-es` by `contentful.spanish_blog_posts`. + #### content_types An object whose key-value pairs correspond to a Contentful Content Types. Each