fix unit specs + make the regexp used to isolate the id of a content type within the class name of a content entry a constant

did committed Mar 24, 2015
commit 9de1d48e7dba6eb405193daa7c00b6361cafbe35
Showing 7 changed files with 26 additions and 10 deletions
locomotive/steam.rb b/lib/locomotive/steam.rb +2 -0
@@ @@ -22,6 +22,8 @@ module Locomotive
WILDCARD = 'content_type_template'.freeze
+ CONTENT_ENTRY_ENGINE_CLASS_NAME = /^Locomotive::ContentEntry(.*)$/o
+
class << self
attr_writer :configuration
end
locomotive/steam/entities/content_type_field.rb b/lib/locomotive/steam/entities/content_type_field.rb +1 -1
@@ @@ -37,7 +37,7 @@ module Locomotive::Steam
def target_id
return @target_id if @target_id
- @target_id = if self.target =~ /^Locomotive::ContentEntry(.*)$/o
+ @target_id = if self.target =~ Locomotive::Steam::CONTENT_ENTRY_ENGINE_CLASS_NAME
$1
else
self.target
locomotive/steam/entities/page.rb b/lib/locomotive/steam/entities/page.rb +2 -1
@@ @@ -31,7 +31,8 @@ module Locomotive::Steam
def templatized?; !!templatized; end
def content_type_id
- # TODO
+ self.target_klass_name =~ Locomotive::Steam::CONTENT_ENTRY_ENGINE_CLASS_NAME
+ $1 || self.target_klass_name
end
def index?
locomotive/steam/liquid/drops/page.rb b/lib/locomotive/steam/liquid/drops/page.rb +2 -2
@@ @@ -38,9 +38,9 @@ module Locomotive
end
def content_type
- if @_source.content_type
+ if @_source.templatized?
# content_type can be either the slug of a content type or a content type
- content_type = content_type_repository.by_slug(@_source.content_type)
+ content_type = content_type_repository.find(@_source.content_type_id)
ContentEntryCollection.new(content_type)
else
nil
spec/unit/entities/page_spec.rb +13 -0
@@ @@ -40,4 +40,17 @@ describe Locomotive::Steam::Page do
end
+ describe '#content_type_id' do
+
+ let(:attributes) { { target_klass_name: '42' } }
+ subject { page.content_type_id }
+
+ it { is_expected.to eq '42' }
+
+ context 'with a Locomotive Engine class name like' do
+ let(:attributes) { { target_klass_name: 'Locomotive::ContentEntryBigNumber' } }
+ it { is_expected.to eq 'BigNumber' }
+ end
+ end
+
end
spec/unit/liquid/drops/page_spec.rb +2 -2
@@ @@ -84,7 +84,7 @@ describe Locomotive::Steam::Liquid::Drops::Page do
let(:entry) { liquid_instance_double('ContentEntry', _label: 'First Article', _slug: 'first-article') }
let(:assigns) { { 'entry' => entry } }
- let(:page) { instance_double('Page', id: 42, title: 'Index', slug: 'index', localized_attributes: [], templatized?: true, content_type: 'articles') }
+ let(:page) { instance_double('Page', id: 42, title: 'Index', slug: 'index', localized_attributes: [], templatized?: true, content_type_id: 42) }
it { expect(subject.title).to eq 'First Article' }
it { expect(subject.slug).to eq 'first-article' }
@@ @@ -92,7 +92,7 @@ describe Locomotive::Steam::Liquid::Drops::Page do
describe '#content_type' do
before do
- allow(services.repositories.content_type).to receive(:by_slug).with('articles').and_return('Articles')
+ allow(services.repositories.content_type).to receive(:find).with(42).and_return('Articles')
end
it { expect(subject.content_type).not_to eq nil }
spec/unit/repositories/page_repository_spec.rb +4 -4
@@ @@ -69,7 +69,7 @@ describe Locomotive::Steam::PageRepository do
subject { repository.find(1) }
it { expect(subject.templatized?).to eq true }
- it { expect(subject.content_type).to eq :articles }
+ it { expect(subject.content_type_id).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' }
@@ @@ -159,8 +159,8 @@ describe Locomotive::Steam::PageRepository do
let(:pages) do
[
- { title: { en: 'Article template' }, content_type: 'articles', slug: { en: 'articles/content_type_template' }, _fullpath: 'articles/template', template_path: { en: 'articles/template.liquid' } },
- { title: { en: 'Archived article template' }, handle: 'archive', content_type: 'articles', slug: { en: 'archived/articles/content_type_template' }, _fullpath: 'archived/articles/template', template_path: { en: 'archived/articles/template.liquid' } },
+ { title: { en: 'Article template' }, content_type: 'Articles', slug: { en: 'articles/content_type_template' }, _fullpath: 'articles/template', template_path: { en: 'articles/template.liquid' } },
+ { title: { en: 'Archived article template' }, handle: 'archive', content_type: 'Articles', slug: { en: 'archived/articles/content_type_template' }, _fullpath: 'archived/articles/template', template_path: { en: 'archived/articles/template.liquid' } },
{ title: { en: 'Home' }, handle: 'home', slug: { en: 'index' }, _fullpath: 'index', template_path: { en: 'index.liquid' } }
]
end
@@ @@ -173,7 +173,7 @@ describe Locomotive::Steam::PageRepository do
context 'both existing entry and page' do
- let(:entry) { instance_double('Article', content_type_slug: 'articles', _slug: { en: 'hello-world' }) }
+ let(:entry) { instance_double('Article', _class_name: 'Locomotive::ContentEntryArticles', _slug: { en: 'hello-world' }) }
it { expect(subject.title[:en]).to eq 'Article template' }
it { expect(subject.content_entry).to eq entry }