#1027: Add sitemap.xml generation

This commit is contained in:
Paul Mackay
2017-01-10 11:39:17 +00:00
parent 6ae1272281
commit 5cb2194f5e
4 changed files with 43 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
class SitemapController < ApplicationController
layout nil
def index
headers['Content-Type'] = 'application/xml'
@pages = ['shops', 'map', 'producers', 'groups']
@enterprises = Enterprise.is_hub
@groups = EnterpriseGroup.all
respond_to :xml
end
end

View File

@@ -0,0 +1,18 @@
!!! XML
%urlset{xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9"}
- for page in @pages
%url
%loc= root_url + page
%changefreq monthly
- for enterprise in @enterprises
%url
%loc= enterprise_shop_url(enterprise)
%lastmod= enterprise.updated_at.strftime('%Y-%m-%d')
%changefreq weekly
- for group in @groups
%url
%loc= group_url(group)
%lastmod= enterprise.updated_at.strftime('%Y-%m-%d')
%changefreq yearly

View File

@@ -169,6 +169,8 @@ Openfoodnetwork::Application.routes.draw do
end
end
get 'sitemap.xml', to: 'sitemap#index', defaults: { format: 'xml' }
# Mount Spree's routes
mount Spree::Core::Engine, :at => '/'

View File

@@ -0,0 +1,12 @@
require 'spec_helper'
feature 'sitemap' do
let(:enterprise) { create(:distributor_enterprise) }
let!(:group) { create(:enterprise_group, enterprises: [enterprise], on_front_page: true) }
it "renders sitemap" do
visit '/sitemap.xml'
expect(page).to have_content enterprise_shop_url(enterprise)
expect(page).to have_content group_url(group)
end
end