generate the sitemap of a site (close #36)

did committed Mar 24, 2015
commit e749d76d12c0ea72d01b190c8a2ed776f74df568
Showing 4 changed files with 237 additions and 38 deletions
locomotive/steam/entities/page.rb b/lib/locomotive/steam/entities/page.rb +2 -1
@@ @@ -22,7 +22,8 @@ module Locomotive::Steam
redirect_url: {},
redirect_type: nil,
parent_id: nil,
- parent_ids: nil
+ parent_ids: nil,
+ updated_at: Time.now
}.merge(attributes))
end
locomotive/steam/middlewares/sitemap.rb b/lib/locomotive/steam/middlewares/sitemap.rb +20 -29
@@ @@ -7,7 +7,7 @@ module Locomotive::Steam
def _call
if env['PATH_INFO'] == '/sitemap.xml'
- render_response(build_xml.tap { |o| puts o }, 200, 'text/plain')
+ render_response(build_xml, 200, 'text/plain')
end
end
@@ @@ -31,7 +31,7 @@ module Locomotive::Steam
next if page.index? || page.not_found?
build_page_xml(page)
- end.flatten.join
+ end.flatten.join.strip
end
def build_page_xml(page)
@@ @@ -40,24 +40,37 @@ module Locomotive::Steam
site.locales.map do |locale|
_page.__locale__ = locale
+ next if _page.title.blank? # should be translated
+
if _page.templatized?
build_templatized_page_xml(_page, locale)
- elsif !_page.title.blank? # should be translated
+ else
page_to_xml(_page, locale)
end
end
end
- def build_templatized_page(page, locale)
- content_type = repositories.content_type.
- # TODO
+ def build_templatized_page_xml(page, locale)
+ content_type = repositories.content_type.find(page.content_type_id)
+
+ repositories.content_entry.with(content_type).all.map do |entry|
+ _entry = Locomotive::Steam::Decorators::I18nDecorator.new(entry, locale)
+
+ next if _entry._label.blank? # should be translated
+
+ page.content_entry = _entry
+
+ page_to_xml(page, locale)
+ end
end
def page_to_xml(page, locale)
+ last_modification = (page.content_entry || page).updated_at.to_date
+
<<-EOF
<url>
<loc>#{base_url}#{url_for(page, locale)}</loc>
- <lastmod>#{page.updated_at.to_date.to_s('%Y-%m-%d')}</lastmod>
+ <lastmod>#{last_modification.to_s('%Y-%m-%d')}</lastmod>
<priority>0.9</priority>
</url>
EOF
@@ @@ -77,27 +90,5 @@ module Locomotive::Steam
end
- # @pages.each do |page|
- # if not page.index_or_not_found?
- # if page.templatized?
- # page.fetch_target_entries(_visible: true).each do |c|
- # if c._slug.present?
- # xml.url do
- # xml.loc public_page_url(page, { content: c })
- # xml.lastmod c.updated_at.to_date.to_s('%Y-%m-%d')
- # xml.priority 0.9
- # end
- # end
- # end
- # else
- # xml.url do
- # xml.loc public_page_url(page)
- # xml.lastmod page.updated_at.to_date.to_s('%Y-%m-%d')
- # xml.priority 0.9
- # end
- # end
- # end
- # end
-
end
end
locomotive/steam/middlewares/templatized_page.rb b/lib/locomotive/steam/middlewares/templatized_page.rb +1 -1
@@ @@ -33,7 +33,7 @@ module Locomotive::Steam
end
def fetch_content_entry(slug)
- if type = content_type_repository.by_slug(page.content_type)
+ if type = content_type_repository.find(page.content_type_id)
decorate(content_entry_repository.with(type).by_slug(slug))
else
nil
spec/integration/server/sitemap_spec.rb +214 -7
@@ @@ -12,7 +12,9 @@ describe Locomotive::Steam::Server do
subject { get '/sitemap.xml'; last_response.body }
- it 'displays code for the first time' do
+ before { allow(Time).to receive(:now).and_return(Time.parse('2015/03/25 00:00:00')) }
+
+ it 'displays the full sitemap' do
is_expected.to eq <<-EOF
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
@@ @@ -20,19 +22,224 @@ describe Locomotive::Steam::Server do
<loc>http://example.org</loc>
<priority>1.0</priority>
</url>
+ <url>
+ <loc>http://example.org/about-us</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/fr/a-notre-sujet</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/nb/om-oss</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/music</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/fr/notre-musique</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/store</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/fr/magasin</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/contact</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/events</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/fr/evenements</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/filtered</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/basic</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/contest</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/grunge-bands</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/unlisted-pages</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/about-us/john-doe</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/fr/a-notre-sujet/jean-personne</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/songs/song-number-1</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/about-us/jane-doe</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/archives/news</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/songs/song-number-1</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/songs/song-number-2</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/songs/song-number-3</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/songs/song-number-4</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/songs/song-number-5</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/songs/song-number-6</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/songs/song-number-7</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/songs/song-number-8</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/fr/songs/song-number-1</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/fr/songs/song-number-2</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/fr/songs/song-number-3</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/fr/songs/song-number-4</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/fr/songs/song-number-5</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/fr/songs/song-number-6</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/fr/songs/song-number-7</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/fr/songs/song-number-8</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/songs/song-number-1/band</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/songs/song-number-2/band</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/songs/song-number-3/band</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/songs/song-number-4/band</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
+ <url>
+ <loc>http://example.org/songs/song-number-5/band</loc>
+ <lastmod>2015-03-25</lastmod>
+ <priority>0.9</priority>
+ </url>
<url>
- <loc>http://example.org/articles</loc>
- <lastmod>:now</lastmod>
+ <loc>http://example.org/songs/song-number-6/band</loc>
+ <lastmod>2015-03-25</lastmod>
<priority>0.9</priority>
</url>
<url>
- <loc>http://example.org/articles/hello-world</loc>
- <lastmod>:now</lastmod>
+ <loc>http://example.org/songs/song-number-7/band</loc>
+ <lastmod>2015-03-25</lastmod>
<priority>0.9</priority>
</url>
<url>
- <loc>http://example.org/articles/lorem-ipsum</loc>
- <lastmod>:now</lastmod>
+ <loc>http://example.org/songs/song-number-8/band</loc>
+ <lastmod>2015-03-25</lastmod>
<priority>0.9</priority>
</url>
</urlset>