uniform way to get the slug of a templatized page

did committed Feb 25, 2015
commit 2d5347ee6149314ff1e1c329a0e2602dd86834a0
Showing 12 changed files with 17 additions and 188 deletions
locomotive/steam.rb b/lib/locomotive/steam.rb +2 -0
@@ @@ -26,6 +26,8 @@ module Locomotive
FRONTMATTER_REGEXP = /^(?<yaml>(---\s*\n.*?\n?)^(---\s*$\n?))?(?<template>.*)/mo
+ WILDCARD = 'content_type_template'.freeze
+
class << self
attr_writer :configuration
end
locomotive/steam/adapters/filesystem/sanitizers/page.rb b/lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb +3 -29
@@ @@ -3,17 +3,10 @@ module Locomotive::Steam
module Filesystem
module Sanitizers
- class Page # < Struct.new(:default_locale, :locales)
+ class Page
include Adapters::Filesystem::Sanitizer
- # def initialize(default_locale, locales)
- # super
- # @content_types = {}
- # @localized = {}
- #
- # end
-
def setup(scope)
super.tap do
@ids, @parent_ids = {}, {}
@@ @@ -80,31 +73,12 @@ module Locomotive::Steam
# its parent is one of them
page[:content_type] = content_type
end
-
- # end
-
- # if page.templatized?
- # # change the slug of a templatized page
- # page[:slug][locale] = 'content-type-template'
-
- # # this also means to change the fullpath
- # if page[:fullpath][locale]
- # page[:fullpath][locale].gsub!(/[^\/]+$/, 'content-type-template')
- # end
-
- # # make sure its children will have its content type
- # set_content_type(page._fullpath, page.content_type)
- # elsif content_type = fetch_content_type(parent_fullpath(page))
- # # not a templatized page but it becomes one because
- # # its parent is one of them
- # page[:content_type] = content_type
- # end
end
def set_fullpath_for(page, locale)
page._fullpath ||= page.attributes.delete(:_fullpath)
- slug = fullpath = page.slug[locale].try(:dasherize)
+ slug = fullpath = page.slug[locale].try(page.templatized? ? :to_s : :dasherize)
return if slug.blank?
@@ @@ -165,7 +139,7 @@ module Locomotive::Steam
def modify_if_templatized(page, locale)
if page.templatized?
- page[:slug][locale] = 'content-type-template'
+ page[:slug][locale] = Locomotive::Steam::WILDCARD
set_content_type(page[:_fullpath], page.content_type)
end
end
locomotive/steam/middlewares/templatized_page.rb b/lib/locomotive/steam/middlewares/templatized_page.rb +1 -1
@@ @@ -15,7 +15,7 @@ module Locomotive::Steam
def set_content_entry!
# extract the slug of the content entry
- %r(^#{page.fullpath.gsub('content-type-template', '([^\/]+)')}$) =~ path
+ %r(^#{page.fullpath.gsub(Locomotive::Steam::WILDCARD, '([^\/]+)')}$) =~ path
if entry = fetch_content_entry($1)
# the entry will be available in the template under different keys
locomotive/steam/repositories/filesystem/sanitizers/page.rb b/lib/locomotive/steam/repositories/filesystem/sanitizers/page.rb +0 -145
@@ @@ -1,145 +0,0 @@
- module Locomotive
- module Steam
- module Repositories
- module Filesystem
- module Sanitizers
-
- class Page < Struct.new(:default_locale, :locales)
-
- def initialize(default_locale, locales)
- super
- @content_types = {}
- @localized = {}
- locales.each { |locale| @localized[locale] = {} }
- end
-
- def apply_to(collection)
- sorted_collection(collection).each do |page|
- locales.each do |locale|
- set_fullpath_for(page, locale)
- modify_if_templatized(page, locale)
- build_editable_elements(page, locale)
- use_default_locale_template_path(page, locale)
- set_default_redirect_type(page, locale)
- end
- end
- end
-
- # If the page does not have a template in a locale
- # then use the template of the default locale
- #
- def use_default_locale_template_path(page, locale)
- paths = page.template_path
-
- if paths[locale] == false
- paths[locale] = paths[default_locale]
- end
- end
-
- def set_default_redirect_type(page, locale)
- if page.redirect_url[locale]
- page.attributes[:redirect_type] ||= 301
- end
- end
-
- def build_editable_elements(page, locale)
- elements = page.editable_elements[locale] || {}
- elements.stringify_keys!
-
- elements.each do |name, content|
- segments = name.split('/')
- block, slug = segments[0..-2].join('/'), segments.last
- block = nil if block.blank?
-
- elements[name] = Filesystem::Models::EditableElement.new(block, slug, content)
- end
- end
-
- def modify_if_templatized(page, locale)
- if page.templatized?
- # change the slug of a templatized page
- page[:slug][locale] = 'content-type-template'
-
- # this also means to change the fullpath
- if page[:fullpath][locale]
- page[:fullpath][locale].gsub!(/[^\/]+$/, 'content-type-template')
- end
-
- # make sure its children will have its content type
- set_content_type(page._fullpath, page.content_type)
- elsif content_type = fetch_content_type(parent_fullpath(page))
- # not a templatized page but it becomes one because
- # its parent is one of them
- page[:content_type] = content_type
- end
- end
-
- def set_fullpath_for(page, locale)
- page._fullpath ||= page.attributes.delete(:_fullpath)
-
- slug = fullpath = page.slug[locale].try(:dasherize)
-
- return if slug.blank?
-
- if page.depth > 1
- base = parent_fullpath(page)
- fullpath = (fetch_localized_fullpath(base, locale) || base) + '/' + slug
- end
-
- set_localized_fullpath(page._fullpath, fullpath, locale)
- page[:fullpath][locale] = fullpath
- end
-
- def depth(page)
- return page.depth if page.depth
-
- page.depth = page[:_fullpath].split('/').size
-
- slug = get_slug(page)
-
- if page.depth == 1 && %w(index 404).include?(slug)
- page.depth = 0
- end
-
- page.depth
- end
-
- def get_slug(page)
- if page.slug.is_a?(Hash)
- page.slug.values.compact.first
- else
- page.slug
- end
- end
-
- def sorted_collection(collection)
- collection.sort_by { |page| depth(page) }
- end
-
- def parent_fullpath(page)
- page._fullpath.split('/')[0..-2].join('/')
- end
-
- def fetch_content_type(fullpath)
- @content_types[fullpath]
- end
-
- def set_content_type(fullpath, value)
- @content_types[fullpath] = value
- end
-
- def fetch_localized_fullpath(fullpath, locale)
- @localized[locale][fullpath]
- end
-
- def set_localized_fullpath(fullpath, value, locale)
- @localized[locale][fullpath] = value
- end
-
- end
-
- end
- end
- end
- end
- end
locomotive/steam/services/page_finder.rb b/lib/locomotive/steam/services/page_finder.rb +0 -2
@@ @@ -6,8 +6,6 @@ module Locomotive
include Locomotive::Steam::Services::Concerns::Decorator
- WILDCARD = 'content-type-template'.freeze
-
def find(path)
decorate do
repository.by_fullpath(path)
locomotive/steam/services/url_builder.rb b/lib/locomotive/steam/services/url_builder.rb +1 -1
@@ @@ -27,7 +27,7 @@ module Locomotive
path = page.fullpath
if page.templatized? && page.content_entry
- path.gsub('content-type-template', page.content_entry._slug)
+ path.gsub(Locomotive::Steam::WILDCARD, page.content_entry._slug)
elsif path == 'index'
same_locale ? '' : nil
else
spec/integration/repositories/page_repository_spec.rb +4 -4
@@ @@ -31,10 +31,10 @@ describe Locomotive::Steam::PageRepository do
it { expect(subject.title[:en]).to eq 'News archive' }
end
- # describe '#matching_fullpath' do
- # subject { repository.matching_fullpath(['songs/content_type_template', 'content_type_template/songs', 'songs/song-number-1']) }
- # it { expect(subject.size).to eq 2 }
- # end
+ describe '#matching_fullpath' do
+ subject { repository.matching_fullpath(['songs/content_type_template', 'content_type_template/songs', 'songs/song-number-1']) }
+ it { expect(subject.size).to eq 2 }
+ end
end
spec/unit/liquid/tags/link_to_spec.rb +1 -1
@@ @@ -89,7 +89,7 @@ describe Locomotive::Steam::Liquid::Tags::PathTo do
let(:entry_drop) { Locomotive::Steam::Liquid::Drops::ContentEntry.new(entry, [:_label, :_slug]) }
let(:entry) { liquid_instance_double('Article', attributes: { _label: { en: 'Hello world', fr: 'Bonjour monde' }, _slug: { en: 'hello-world', fr: 'bonjour-monde' } }) }
let(:drop) { Locomotive::Steam::Liquid::Drops::Page.new(page, [:fullpath]) }
- let(:page) { liquid_instance_double('ArticleTemplate', title: 'Template of an article', handle: 'article', attributes: { fullpath: { en: 'my-articles/content-type-template', fr: 'mes-articles/content-type-template' } }, localized_attributes: [:fullpath], content_entry: entry_drop.send(:_source), templatized?: true) }
+ let(:page) { liquid_instance_double('ArticleTemplate', title: 'Template of an article', handle: 'article', attributes: { fullpath: { en: 'my-articles/content_type_template', fr: 'mes-articles/content_type_template' } }, localized_attributes: [:fullpath], content_entry: entry_drop.send(:_source), templatized?: true) }
let(:source) { '{% link_to article %}' }
before do
spec/unit/liquid/tags/locale_switcher_spec.rb +1 -1
@@ @@ -49,7 +49,7 @@ describe Locomotive::Steam::Liquid::Tags::LocaleSwitcher do
let(:entry_drop) { Locomotive::Steam::Liquid::Drops::ContentEntry.new(entry, [:_label, :_slug]) }
let(:entry) { liquid_instance_double('Article', attributes: { _label: { en: 'Hello world', fr: 'Bonjour monde' }, _slug: { en: 'hello-world', fr: 'bonjour-monde' } }) }
let(:drop) { Locomotive::Steam::Liquid::Drops::Page.new(page, [:fullpath]) }
- let(:attributes) { { fullpath: { en: 'my-articles/content-type-template', fr: 'mes-articles/content-type-template' } } }
+ let(:attributes) { { fullpath: { en: 'my-articles/content_type_template', fr: 'mes-articles/content_type_template' } } }
let(:page) { liquid_instance_double('ArticleTemplate', title: 'Article template', attributes: attributes, content_entry: entry_drop.send(:_source), templatized?: true) }
let(:source) { '{% locale_switcher label: "title" %}' }
spec/unit/liquid/tags/path_to_spec.rb +1 -1
@@ @@ -78,7 +78,7 @@ describe Locomotive::Steam::Liquid::Tags::PathTo do
let(:entry_drop) { Locomotive::Steam::Liquid::Drops::ContentEntry.new(entry, [:_slug]) }
let(:entry) { liquid_instance_double('Article', attributes: { _slug: { en: 'hello-world', fr: 'bonjour-monde' } }) }
let(:drop) { Locomotive::Steam::Liquid::Drops::Page.new(page, [:fullpath]) }
- let(:page) { liquid_instance_double('ArticleTemplate', title: 'Template of an article', handle: 'article', attributes: { fullpath: { en: 'my-articles/content-type-template', fr: 'mes-articles/content-type-template' } }, localized_attributes: [:fullpath], content_entry: entry_drop.send(:_source), templatized?: true) }
+ let(:page) { liquid_instance_double('ArticleTemplate', title: 'Template of an article', handle: 'article', attributes: { fullpath: { en: 'my-articles/content_type_template', fr: 'mes-articles/content_type_template' } }, localized_attributes: [:fullpath], content_entry: entry_drop.send(:_source), templatized?: true) }
let(:source) { '{% path_to article %}' }
before do
spec/unit/repositories/page_repository_spec.rb +2 -2
@@ @@ -73,7 +73,7 @@ describe Locomotive::Steam::PageRepository do
it { expect(subject.content_type).to eq :articles }
it { expect(subject.slug[:en]).to eq 'comments' }
it { expect(subject.slug[:fr]).to eq nil }
- it { expect(subject.fullpath[:en]).to eq 'articles/content-type-template/comments' }
+ it { expect(subject.fullpath[:en]).to eq 'articles/content_type_template/comments' }
end
@@ @@ -127,7 +127,7 @@ describe Locomotive::Steam::PageRepository do
context 'templatized page' do
- let(:paths) { ['articles/content-type-template', 'content-type-template/hello-world', 'articles/hello-world'] }
+ let(:paths) { ['articles/content_type_template', 'content_type_template/hello-world', 'articles/hello-world'] }
let(:pages) do
[{ title: { en: 'Templatized article' }, slug: { en: 'template' }, content_type: 'articles', _fullpath: 'articles/template', template_path: { en: 'articles/template.liquid' } }]
spec/unit/services/url_builder_spec.rb +1 -1
@@ @@ -38,7 +38,7 @@ describe Locomotive::Steam::Services::UrlBuilder do
describe 'templatized page' do
let(:article) { instance_double('Article', _slug: 'hello-world') }
- let(:page) { instance_double('Template', fullpath: 'articles/content-type-template', templatized?: true, content_entry: article) }
+ let(:page) { instance_double('Template', fullpath: 'articles/content_type_template', templatized?: true, content_entry: article) }
it { is_expected.to eq '/articles/hello-world' }
end