First attempt of specific decorator

arnaud sellenet committed Jun 16, 2014
commit 7dc5e9640df837ff2b93cdbf06a2a4345b6059d8
Showing 7 changed files with 112 additions and 66 deletions
locomotive/steam/decorators.rb b/lib/locomotive/steam/decorators.rb +1 -0
@@ @@ -0,0 +1 @@
+ require_relative 'decorators/page_decorator'
locomotive/steam/decorators/page_decorator.rb b/lib/locomotive/steam/decorators/page_decorator.rb +37 -0
@@ @@ -0,0 +1,37 @@
+ module Locomotive
+ module Steam
+ module Decorators
+ class PageDecorator < SimpleDelegator
+
+ # Return the fullpath dasherized and with the "*" character
+ # for the slug of templatized page.
+ #
+ # @return [ hash ] The safe full paths
+ #
+
+ def safe_fullpath
+ binding.pry
+ if index_or_404?
+ slug[current_locale]
+ else
+ base = parent.safe_fullpath
+ _slug = if templatized? && !templatized_from_parent
+ '*'
+ else
+ slug[current_locale]
+ end
+ (base == 'index' ? _slug : File.join(base, _slug)).dasherize
+ end
+ end
+
+ def parent
+ Locomotive::Steam::Decorators::PageDecorator.new(
+ Locomotive::Decorators::I18nDecorator.new(
+ __getobj__.parent, current_locale
+ )
+ )
+ end
+ end
+ end
+ end
+ end
locomotive/steam/entities/page.rb b/lib/locomotive/steam/entities/page.rb +12 -28
@@ @@ -8,8 +8,8 @@ module Locomotive
attributes :parent, :title, :slug, :fullpath, :redirect_url, :redirect_type,
:template, :handle, :listed, :searchable, :templatized, :content_type,
:published, :cache_strategy, :response_type, :position, :seo_title,
- :meta_keywords, :meta_description, :editable_elements, :site, :site_id,
- :parent_id
+ :meta_keywords, :meta_description, :editable_elements, :site,
+ :site_id, :parent_id
## aliases ##
alias :listed? :listed
@@ @@ -17,6 +17,7 @@ module Locomotive
alias :templatized? :templatized
alias :searchable? :searchable
+ attr_accessor :templatized_from_parent
# Tell if the page is either the index page.
#
@@ @@ -79,15 +80,16 @@ module Locomotive
#
# @param [ String ] fullpath The fullpath
#
- # def fullpath_with_setting_slug=(fullpath)
- # if fullpath && self.slug.nil?
- # self.slug = File.basename(fullpath)
- # end
-
- # self.fullpath_without_setting_slug = fullpath
- # end
+ def fullpath_with_setting_slug=(fullpath)
+ fullpath.each do |key, value|
+ if value && (self.slug ||= {})[key].nil?
+ self.slug[key] = File.basename(value)
+ end
+ end
+ self.fullpath_without_setting_slug = fullpath
+ end
- # alias_method_chain :fullpath=, :setting_slug
+ alias_method_chain :fullpath=, :setting_slug
# Modified setter in order to set inheritance fields from parent
#
@@ @@ -105,24 +107,6 @@ module Locomotive
# end
# alias_method_chain :parent=, :inheritance
- # Return the fullpath dasherized and with the "*" character
- # for the slug of templatized page.
- #
- # @return [ String ] The safe full path or nil if the page is not translated in the current locale
- #
- def safe_fullpath
- if index_or_404?
- slug
- else
- base = parent.safe_fullpath
- _slug = if templatized? && !templatized_from_parent
- '*'
- else
- slug
- end
- (base == 'index' ? _slug : File.join(base, _slug)).dasherize
- end
- end
# Set the source of the page without any pre-rendering. Used by the API reader.
#
# @param [ String ] content The HTML raw template
locomotive/steam/server.rb b/lib/locomotive/steam/server.rb +1 -0
@@ @@ -6,6 +6,7 @@ require_relative 'middlewares'
require 'locomotive/models'
require 'locomotive/decorators'
+ require_relative 'decorators'
module Locomotive::Steam
class Server
spec/unit/decorators/page_decorator_spec.rb +55 -0
@@ @@ -0,0 +1,55 @@
+ require 'spec_helper'
+
+ describe 'Locomotive::Steam::Decorators::PageDecorator' do
+
+ let(:locale) { :en }
+ it 'builds an empty decorator' do
+ build_page.should_not be_nil
+ end
+
+ describe '#safe_fullpath' do
+ let(:index_page) { build_page(fullpath: {en: 'index'}) }
+ let(:not_found_page) { build_page(fullpath: {en: '404'}) }
+ let(:about_page) { build_page(fullpath: {en: 'about_me'}, parent: index_page) }
+ let(:products_page) { build_page(fullpath: {en: 'products'}, parent: index_page, templatized: true) }
+
+ context 'not templatized' do
+ context 'index or 404' do
+ it { decorated(index_page).safe_fullpath.should eq 'index' }
+ it { decorated(not_found_page).safe_fullpath.should eq '404' }
+ end
+
+ context 'other' do
+ it { decorated(about_page).safe_fullpath.should eq 'about-me' }
+ end
+ end
+
+ context 'templatized' do
+ subject { decorated build_page(fullpath: {en: 'products'}, parent: index_page, templatized: true) }
+ its(:safe_fullpath) { should eq '*' }
+ end
+
+ context 'templatized with not templatized parent' do
+ subject { decorated build_page(fullpath: {en: 'about_me/contact'}, parent: about_page, templatized: true) }
+ its(:safe_fullpath) { should eq 'about-me/*' }
+ end
+
+ context 'templatized parent' do
+ subject { decorated build_page(fullpath: {en: 'products/detail'}, parent: products_page) }
+ its(:safe_fullpath) { should eq '*/detail' }
+ end
+ end
+
+ def decorated(page)
+ Locomotive::Steam::Decorators::PageDecorator.new(
+ Locomotive::Decorators::I18nDecorator.new(
+ page, locale
+ )
+ )
+ end
+
+ def build_page(attributes = {})
+ Locomotive::Steam::Entities::Page.new(attributes)
+ end
+
+ end
spec/unit/entities/page_spec.rb +5 -38
@@ @@ -1,6 +1,6 @@
require 'spec_helper'
- describe 'Locomotive::Steam::Entities::Page' do
+ describe 'Locomotive::Steam::Entities::Page', focused: true do
it 'builds an empty page' do
build_page.should_not be_nil
@@ @@ -29,53 +29,20 @@ describe 'Locomotive::Steam::Entities::Page' do
it { build_page(fullpath: {en: 'about/the/team'}).depth.should eq 3 }
end
- describe '#fullpath=', pending: true do
+ describe '#fullpath=' do
context 'when the page has no slug yet' do
it 'also sets the slug' do
- build_page(fullpath: 'this/is/the/page_full_path').slug.should eq 'page_full_path'
+ build_page(fullpath: {en: 'this/is/the/page_full_path'}).slug[:en].should eq 'page_full_path'
end
end
context 'when the slug is already set' do
- it 'keeps the original slug', pending: true do
- build_page(fullpath: 'this/is/the/page_full_path', slug: 'the_slug').slug.should eq 'the_slug'
+ it 'keeps the original slug' do
+ build_page(fullpath: {en: 'this/is/the/page_full_path'}, slug: {en: 'the_slug'}).slug[:en].should eq 'the_slug'
end
end
end
- describe '#safe_fullpath', pending: true do
- let(:index_page) { build_page(fullpath: 'index') }
- let(:not_found_page) { build_page(fullpath: '404') }
- let(:about_page) { build_page(fullpath: 'about_me', parent: index_page) }
- let(:products_page) { build_page(fullpath: 'products', parent: index_page, templatized: true) }
-
- context 'not templatized' do
- context 'index or 404' do
- it { index_page.safe_fullpath.should eq 'index' }
- it { not_found_page.safe_fullpath.should eq '404' }
- end
-
- context 'other' do
- it { about_page.safe_fullpath.should eq 'about-me' }
- end
- end
-
- context 'templatized' do
- subject { build_page(fullpath: 'products', parent: index_page, templatized: true) }
- its(:safe_fullpath) { should eq '*' }
- end
-
- context 'templatized with not templatized parent' do
- subject { build_page(fullpath: 'about_me/contact', parent: about_page, templatized: true) }
- its(:safe_fullpath) { should eq 'about-me/*' }
- end
-
- context 'templatized parent' do
- subject { build_page(fullpath: 'products/detail', parent: products_page) }
- its(:safe_fullpath) { should eq '*/detail' }
- end
- end
-
def build_page(attributes = {})
Locomotive::Steam::Entities::Page.new(attributes)
end
spec/unit/loaders/pages_loader_spec.rb +1 -0
@@ @@ -27,6 +27,7 @@ describe Locomotive::Steam::Loader::Yml::PagesLoader do
context 'records content' do
subject { Locomotive::Models[:pages]['index'] }
it { subject.title[:en].should eql 'Home page' }
+ it { subject.title[:fr].should eql 'Page d\'accueil' }
end
end
end