Add a simple sitemap generator
Clarke Brunsdon
committed Jan 23, 2012
commit a95dfb5208ef958401b13c74014a9297b918ea3b
Showing 6
changed files with
43 additions
and 3 deletions
app/controllers/cms_content_controller.rb
+3
-0
| @@ | @@ -19,6 +19,9 @@ class CmsContentController < ApplicationController |
| end | |
| end | |
| + | def render_sitemap |
| + | end |
| + | |
| def render_css | |
| render :text => @cms_layout.css, :content_type => 'text/css' | |
| end | |
app/views/cms_content/render_sitemap.xml.builder
+14
-0
| @@ | @@ -0,0 +1,14 @@ |
| + | xml.instruct! :xml, :version => '1.0', :encoding => 'UTF-8' |
| + | |
| + | xml.urlset :xmlns => 'http://www.sitemaps.org/schemas/sitemap/0.9' do |
| + | @cms_site.pages.published.each do |page| |
| + | xml.url do |
| + | xml.loc "http://#{@cms_site.hostname}#{"/" + @cms_site.path unless @cms_site.path.blank? }#{page.full_path}" |
| + | # just take some guesses the closer to the root means higher priority |
| + | # start subtracting 0.1 for every additional child page, max out at 0.1 |
| + | # "/" splits to 0, "/child_page" splits to 2, hence weird max -1 |
| + | xml.priority [1 - (0.1 * ( ( [page.full_path.split("/").count, 1].max - 1 ) ) ), 0.1].max |
| + | xml.lastmod page.updated_at.strftime('%Y-%m-%d') |
| + | end |
| + | end |
| + | end |
config/routes.rb
+1
-0
| @@ | @@ -34,6 +34,7 @@ Rails.application.routes.draw do |
| scope :controller => :cms_content do | |
| get 'cms-css/:site_id/:identifier' => :render_css, :as => 'cms_css' | |
| get 'cms-js/:site_id/:identifier' => :render_js, :as => 'cms_js' | |
| + | get '(:cms_path)/sitemap' => :render_sitemap, :as => 'cms_sitemap', :constraints => {:format => /xml/}, :format => :xml |
| get '/' => :render_html, :as => 'cms_html', :path => "(*cms_path)" | |
| end | |
test/fixtures/cms/layouts.yml
+2
-1
| @@ | @@ -36,4 +36,5 @@ child: |
| {{cms:page:right_column}} | |
| css: child_css | |
| js: child_js | |
| - | position: 0 |
| \ No newline at end of file | |
| + | position: 0 |
| + | |
test/fixtures/cms/sites.yml
+8
-1
| @@ | @@ -3,4 +3,11 @@ default: |
| identifier: default-site | |
| hostname: test.host | |
| path: | |
| - | is_mirrored: false |
| \ No newline at end of file | |
| + | is_mirrored: false |
| + | |
| + | site_with_path: |
| + | label: Site With Path |
| + | identifier: site-with-path |
| + | hostname: test.path.host |
| + | path: standard-path |
| + | is_mirrored: false |
test/functional/cms_content_controller_test.rb
+15
-1
| @@ | @@ -155,5 +155,19 @@ class CmsContentControllerTest < ActionController::TestCase |
| get :render_js, :site_id => cms_sites(:default).id, :identifier => 'bogus' | |
| assert_response 404 | |
| end | |
| - | |
| + | |
| + | def test_render_sitemap |
| + | get :render_sitemap, :format => :xml |
| + | assert_response :success |
| + | assert_match '<loc>http://test.host/child-page</loc>', response.body |
| + | end |
| + | |
| + | def test_render_sitemap_with_path |
| + | @request.host = 'test.path.host' |
| + | get :render_sitemap, :cms_path => cms_sites(:site_with_path).path, :format => :xml |
| + | assert_response :success |
| + | assert_equal cms_sites(:site_with_path), assigns(:cms_site) |
| + | assert !response.body.include?("<loc>"), "No pages, no loc's in the sitemap" |
| + | end |
| + | |
| end | |