Skip to content

Commit

Permalink
Support for custom sitemaps
Browse files Browse the repository at this point in the history
  • Loading branch information
christianhellsten committed Aug 18, 2009
1 parent a02b777 commit d39d376
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 16 deletions.
29 changes: 29 additions & 0 deletions README.textile
Expand Up @@ -83,6 +83,31 @@ You can override this behavior when calling the sitemap method:
</code>
</pre>

h2. 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:

<pre>
<code>

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

</code>
</pre>


h2. Usage

After configuring the plugin you can generate the sitemap with rake:
Expand Down Expand Up @@ -126,6 +151,10 @@ SHELL=/bin/bash
</code>
</pre>

h2. Todo

"Sitemap validation":http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=35738

h2. Author

"Christian Hellsten":http://christianhellsten.com ("Aktagon Ltd.":http://aktagon.com)
4 changes: 2 additions & 2 deletions lib/sitemap_generator.rb
Expand Up @@ -2,8 +2,8 @@ module SitemapGenerator
DOMAIN = nil

module Version #:nodoc:
Major = 0
Minor = 1
Major = 1
Minor = 0
Tiny = 0

String = [Major, Minor, Tiny].join('.')
Expand Down
6 changes: 5 additions & 1 deletion lib/sitemap_generator/active_record.rb
Expand Up @@ -11,7 +11,11 @@ def self.included(base)
end

module ClassMethods
def sitemap(options = {})
def sitemap(options = {}, &block)

# Use a custom generator if given a block
options[:generator] = block if block_given?

self.sitemap_options = options
end
end
Expand Down
38 changes: 25 additions & 13 deletions lib/sitemap_generator/generator.rb
Expand Up @@ -37,31 +37,43 @@ def find_models
end

def find_models_and_generate
# TODO rename data to sitemap
self.generate do |data|
self.find_models.each do |model|
model_columns = model.columns.map(&:name)

# Use defaults
options = DEFAULT_OPTIONS.merge(model.sitemap_options)

# Find a column for ordering
if options[:order] == nil
order = ['updated_at', 'updated_on', 'created_at', 'created_on'].delete_if { |x| !model_columns.include?(x) }
options[:order] = "#{order.first} ASC"
end

puts "Sitemap #{model} #{options.inspect}"
# A user defined block that handles sitemap generation
custom_generator = options[:generator]

# This is where we create the sitemap.
# Find and add model instances to the sitemap
model.all(:order => options[:order], :limit => options[:limit]).each do |o|
data.add o, options[:priority], options[:change_frequency]
if custom_generator
custom_generator.call(data.xml)
else
auto_generate(model, data, options)
end

end
end
end

def auto_generate(model, data, options)
model_columns = model.columns.map(&:name)

# Find a column for ordering
if options[:order] == nil
order = ['updated_at', 'updated_on', 'created_at', 'created_on'].delete_if { |x| !model_columns.include?(x) }
options[:order] = "#{order.first} ASC"
end

puts "Sitemap #{model} #{options.inspect}"

# This is where we create the sitemap.
# Find and add model instances to the sitemap
model.all(:order => options[:order], :limit => options[:limit]).each do |o|
data.add o, options[:priority], options[:change_frequency]
end
end

def generate(&block)

File.open(@filename, "w") do |file|
Expand Down
5 changes: 5 additions & 0 deletions lib/sitemap_generator/sitemap.rb
@@ -1,5 +1,8 @@


module SitemapGenerator
class Sitemap

VALID_CHANGE_FREQ = [
:always,
:hourly,
Expand All @@ -9,6 +12,8 @@ class Sitemap
:yearly,
:never
]

attr_accessor :xml

def initialize(xml)
@xml = xml
Expand Down

0 comments on commit d39d376

Please sign in to comment.