fix issue #71 (Sitemap.xml is returning elements that are not translated)

did committed Jul 13, 2016
commit ce4210da7fcf60e90f67b6f97e062aa21c8ecfc8
Showing 5 changed files with 78 additions and 2 deletions
locomotive/steam/entities/content_type.rb b/lib/locomotive/steam/entities/content_type.rb +4 -0
@@ @@ -34,6 +34,10 @@ module Locomotive::Steam
fields.localized_names + select_fields.map(&:name)
end
+ def localized?
+ !fields.localized_names.blank?
+ end
+
def persisted_field_names
[].tap do |names|
fields_by_name.each do |name, field|
locomotive/steam/middlewares/sitemap.rb b/lib/locomotive/steam/middlewares/sitemap.rb +11 -0
@@ @@ -53,6 +53,8 @@ module Locomotive::Steam
def build_templatized_page_xml(page, locale)
content_type = repositories.content_type.find(page.content_type_id)
+ return nil unless build_templatized_page_xml?(page, content_type, locale)
+
repositories.content_entry.with(content_type).all.map do |entry|
_entry = Locomotive::Steam::Decorators::I18nDecorator.new(entry, locale)
@@ @@ -88,6 +90,15 @@ module Locomotive::Steam
services.url_builder.url_for(page, locale)
end
+ def build_templatized_page_xml?(page, content_type, locale)
+ return true if content_type.localized? || default_locale == locale
+
+ # does the templatized page have the same source
+ # (liquid template) as in the default locale?
+ # If so, no need to add a xml entry for this page
+ !page.source.blank?
+ end
+
def base_url
"#{request.scheme}://#{request.host_with_port}"
end
spec/integration/server/sitemap_spec.rb +1 -1
@@ @@ -21,7 +21,7 @@ describe Locomotive::Steam::Server do
it 'checks if it looks valid' do
expect(Nokogiri::XML(subject).errors.empty?).to eq true
- expect(subject.scan(/<url>/).size).to eq 48
+ expect(subject.scan(/<url>/).size).to eq 40
expect(subject).to match("<loc>http://example.org/songs/song-number-2/band</loc>")
expect(subject).to match((<<-EOF
<url>
spec/unit/entities/content_type_spec.rb +21 -0
@@ @@ -43,6 +43,27 @@ describe Locomotive::Steam::ContentType do
end
+
+ describe '#localized?' do
+
+ let(:names) { [] }
+
+ before { expect(repository).to receive(:localized_names).and_return(names) }
+
+ subject { content_type.localized? }
+
+ it { is_expected.to eq false }
+
+ context 'with one localized field' do
+
+ let(:names) { ['title'] }
+
+ it { is_expected.to eq true }
+
+ end
+
+ end
+
describe '#default' do
subject { content_type.fields_with_default }
spec/unit/middlewares/sitemap_spec.rb +41 -1
@@ @@ -6,19 +6,21 @@ require_relative '../../../lib/locomotive/steam/middlewares/sitemap'
describe Locomotive::Steam::Middlewares::Sitemap do
+ let(:site) { instance_double('Site', locales: ['en', 'fr'], default_locale: 'en') }
let(:pages) { [] }
let(:page_repository) { instance_double('PageRepository', published: pages) }
let(:app) { ->(env) { [200, env, 'app'] }}
let(:middleware) { described_class.new(app) }
before do
+ allow_any_instance_of(described_class).to receive(:site).and_return(site)
allow_any_instance_of(described_class).to receive(:base_url).and_return('http://localhost/')
allow_any_instance_of(described_class).to receive(:page_repository).and_return(page_repository)
end
describe '#call' do
- let(:env) { { 'PATH_INFO' => '/sitemap.xml', 'steam.page' => nil } }
+ let(:env) { { 'PATH_INFO' => '/sitemap.xml', 'steam.page' => nil, 'steam.site' => site } }
subject { middleware.call(env) }
describe 'no pages' do
@@ @@ -39,6 +41,44 @@ describe Locomotive::Steam::Middlewares::Sitemap do
end
+ describe '#build_templatized_page_xml?' do
+
+ let(:localized) { true }
+ let(:source) { '<h1>{{ post.title }}</h1>' }
+ let(:page) { instance_double('TemplatePage', source: source) }
+ let(:content_type) { instance_double('Post', localized?: localized) }
+ let(:locale) { 'fr' }
+
+ subject { middleware.send(:build_templatized_page_xml?, page, content_type, locale) }
+
+ it { is_expected.to eq true }
+
+ context 'current locale is equals to the site default locale' do
+
+ let(:locale) { 'en' }
+
+ it { is_expected.to eq true }
+
+ end
+
+ context 'the content type is not localized' do
+
+ let(:localized) { false }
+
+ it { is_expected.to eq true }
+
+ context 'the page has the same liquid template in all the locales' do
+
+ let(:source) { '' }
+
+ it { is_expected.to eq false }
+
+ end
+
+ end
+
+ end
+
end
end