Skip to content

mauriciozaffari/sitemap-generator

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rails Sitemap Generator

The Rails Sitemap Generator plugin makes it very easy to generate sitemaps for your Rails app.

In addition to simplifying sitemap creation the plugin also includes the following features:

  • Sitemap validation. A sitemap can contain about 50 000 items and be no more than x MB.
  • Search engine ping. The plugin notifies search engines each time your sitemap is updated.

Installation

Installation is done with the script/plugin script:

  
  $ script/plugin install  git://github.com/christianhellsten/sitemap-generator.git
  

After installing the plugin you need to:

  • Configure the plugin (config/sitemap.yml)
  • Include a call to the sitemap method from each model you want to include in the sitemap

Configuring the plugin

A configuration file (config/sitemap.yml) is automatically created by the installation script.

Below is a sample configuration file, which also contains the default values for the models:

  
  domain: aktagon.com

  limit: 5000
  priority: 1
  change_frequency: weekly
  

The options:

  • domain: before using the sitemap generator you need to tell the plugin the domain where your application is deployed. This is because the sitemap must contain the full URL, not just the URI, of all resources.
  • limit: specifies how many model instances you want to include in the sitemap
  • priority: specifies the priority of the model
  • change_frequency: specifies how often the data changes: always, hourly, daily, weekly, monthly, yearly or never

You can find a more comprehensive description of these concepts on the Wikipedia page on sitemaps

Configuring ActiveRecord models

Each model you want included in the sitemap must call the sitemap(options) method.

  
  class Post < ActiveRecord::Base
    sitemap 
  end
  

The default options for each model are taken from the configuration file (config/sitemap.yml). You’ll most likely want each model to have a different priority and change_frequency. The plugin allows you to override the default options as shown in this example:

  
  class Post < ActiveRecord::Base
    sitemap :change_frequency => :weekly, :limit => 1000, :priority => 0.5
  end
  

Order

The plugin will automatically order the data using the most appropriate Rails magic column, which is the first of the following columns it can find: updated_at, updated_on, created_at or created_on.

You can override this behavior when calling the sitemap method:

  
  class Post < ActiveRecord::Base
    sitemap :order => 'points DESC'
  end
  

Custom sitemaps

Custom sitemaps can be generated by passing a block to the sitemap method. A builder instance representing the sitemap is passed to this block. Inside the block you can do whatever is needed:

  

  sitemap do |xml|

    Post.all.each do |place|
      xml.url do
        xml.loc "http://#{SitemapGenerator::Options.domain}/#{post.to_param}'"

        xml.lastmod SitemapGenerator::Helpers.instance.w3c_date(post.updated_at)
        xml.changefreq 'weekly'
        xml.priority '1'
      end  
    end  

  end

  

Usage

After configuring the plugin you can generate the sitemap with rake:

  
  $ rake sitemap:generate
  

You can also generate the sitemap programatically like this:

  
  SitemapGenerator::Generator.run
  

Scheduled updates with cron

Open the cron configuration with the following command:


$ sudo crontab -e

Add the following:


PATH=/usr/local/bin:/usr/bin:/bin
SHELL=/bin/bash

# m h  dom mon dow   command

# Update the sitemap every day at 24:00

00 00 * * * cd /var/www/xxx/current && RAILS_ENV=production rake sitemap:generate

Todo

Author

Christian Hellsten (Aktagon Ltd.)

Contributors

Dan Hawkins

About

sitemap-generator is a Rails plugin that makes it easy to generate sitemaps.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 100.0%