Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactored code.

  • Loading branch information...
commit 772f026217ca9cacfece2cb3e34737710fe05abd 1 parent 0af289e
@christianhellsten authored
View
12 README
@@ -1,12 +0,0 @@
-SitemapGenerator
-================
-
-A Rails plugin that makes it very easy to generate sitemaps for your Rails app.
-
-
-Example
-=======
-
-script/plugin install git@github.com:christianhellsten/sitemap-generator.git
-
-Copyright (c) 2009 Christian Hellsten (Aktagon Ltd.), released under the MIT license
View
2  init.rb
@@ -1 +1 @@
-# Include hook code here
+ActiveRecord::Base.send :include, SitemapGenerator::ActiveRecord
View
12 install.rb
@@ -1 +1,13 @@
# Install hook code here
+
+if !File.exist?(Options::CONFIG_FILE)
+ File.open(Options::CONFIG_FILE, 'w') do |file|
+ file << "domain: # For example: 'aktagon.com'"
+ file << "change_frequency: weekly"
+ file << "limit: 5000"
+ file << "priority: 1.0"
+ end
+end
+
+puts "SitemapGenerator installed."
+puts "NOTE: You need to specify the domain of your application in #{Options::CONFIG_FILE}"
View
7 lib/sitemap_generator.rb
@@ -1,9 +1,6 @@
-ENV['RAILS_ENV'] ||= "development"
-
-#require 'rubygems'
-#require 'config/environment'
-
module SitemapGenerator
+ DOMAIN = nil
+
module Version #:nodoc:
Major = 0
Minor = 1
View
87 lib/sitemap_generator/generator.rb
@@ -4,29 +4,80 @@
module SitemapGenerator
class Generator
- def initialize(host, filename = "#{RAILS_ROOT}/public/sitemap.xml")
- @host = host
+
+ DEFAULT_OPTIONS = {
+ :order => nil,
+ :limit => Options.limit,
+ :priority => Options.priority,
+ :change_frequency => Options.change_frequency
+ }
+
+ def initialize(filename = "#{RAILS_ROOT}/public/sitemap.xml")
@filename = filename
@old_size, @new_size = File.size(@filename) rescue 0
end
+ def find_models
+ models = []
+
+ files = Dir.glob(File.join(RAILS_ROOT, 'app', 'models', '*.rb')).delete_if {|c| c =~ /observer\.rb/ } #{|c| c < ActiveRecord::Base== false}
+
+ files.each do |file|
+ # Get the class from the filename
+ model = file.split('/').last[0..-4].classify.constantize
+ # Skip classes that don't have any sitemap options
+ next if !model.methods.include?('sitemap_options') || model.sitemap_options == nil
+
+ models << model
+ end
+
+ puts "Sitemap WARNING!! No models found. Have you included a call to the sitemap in your ActiveRecord models?" if models.empty?
+
+ models
+ end
+
+ def find_models_and_generate
+ 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}"
+
+ # 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
+ end
+ end
+
def generate(&block)
- default_url_options[:host] = @host
File.open(@filename, "w") do |file|
xml = Builder::XmlMarkup.new(:target => file, :indent => 2)
xml.instruct!
xml.urlset "xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" do
- block.call(@host, Sitemap.new(@host, xml))
+ block.call(Sitemap.new(xml))
end
end
@new_size = File.size(@filename)
- ping(host) if ping?
+ ping if ping?
- p "Sitemap '#{@filename}' generated successfully."
+ puts "Sitemap '#{@filename}' generated successfully."
end
def ping?
@@ -46,25 +97,31 @@ def valid?
true
end
- def ping(host)
+ def ping
# Ping sites http://en.wikipedia.org/wiki/Sitemaps
- [ "http://www.google.com/webmasters/tools/ping?sitemap=http://#{host}/sitemap.xml",
- "http://search.yahooapis.com/SiteExplorerService/V1/ping?sitemap=http://#{host}/sitemap.xml",
- "http://submissions.ask.com/ping?sitemap=http://#{host}/sitemap.xml",
- "http://webmaster.live.com/ping.aspx?siteMap=http://#{host}/sitemap.xml" ].each do |url|
+ # TODO support other names than sitemap.xml?
+
+ [ "http://www.google.com/webmasters/tools/ping?sitemap=http://#{Options.domain}/sitemap.xml",
+ "http://search.yahooapis.com/SiteExplorerService/V1/ping?sitemap=http://#{Options.domain}/sitemap.xml",
+ "http://submissions.ask.com/ping?sitemap=http://#{Options.domain}/sitemap.xml",
+ "http://webmaster.live.com/ping.aspx?siteMap=http://#{Options.domain}/sitemap.xml" ].each do |url|
open(url) do |f|
if f.status[0] == "200"
- p "Sitemap successfully submitted to #{url}"
+ puts "Sitemap successfully submitted to #{url}"
else
- p "Failed to submit sitemap to #{url}"
+ puts "Failed to submit sitemap to #{url}"
end
end
end
end
class << self
- def generate(host, &block)
- Generator.new(host).generate(&block)
+ def run
+ Generator.new.find_models_and_generate
+ end
+
+ def generate(&block)
+ Generator.new.generate(&block)
end
end
View
23 lib/sitemap_generator/sitemap.rb
@@ -10,28 +10,35 @@ class Sitemap
:never
]
- def initialize(host, xml)
+ def initialize(xml)
@xml = xml
- @host = host
+
+ raise "!!!!domain is nil. You need specify a value for it in #{Options::CONFIG_FILE}" if Options.domain.nil?
+
+# default_url_options[:domain] = Options.domain
end
def add(model_instance, priority = nil, change_freq = nil)
- last_modified = model_instance.read_attribute(:updated_on) || model_instance.read_attribute(:updated_at)
-
- if last_modified.nil?
- last_modified = model_instance.read_attribute(:created_on) || model_instance.read_attribute(:created_at)
- end
+ last_modified = find_last_modified(model_instance)
if !VALID_CHANGE_FREQ.include?(change_freq.to_sym) && !change_freq.nil?
raise "Invalid change frequency #{change_freq}, should be one of #{VALID_CHANGE_FREQ}"
end
@xml.url do
- @xml.loc "http://#{@host}#{Helpers.instance.url_for(model_instance)}"
+ @xml.loc "http://#{Options.domain}#{Helpers.instance.url_for(model_instance)}"
@xml.lastmod Helpers.instance.w3c_date(last_modified) if last_modified
@xml.changefreq change_freq.to_s if change_freq
@xml.priority priority if priority
end
end
+
+ def find_last_modified(model_instance)
+ last_modified = model_instance.read_attribute(:updated_on) || model_instance.read_attribute(:updated_at)
+
+ if last_modified.nil?
+ last_modified = model_instance.read_attribute(:created_on) || model_instance.read_attribute(:created_at)
+ end
+ end
end
end
View
13 tasks/sitemap_generator_tasks.rake
@@ -4,7 +4,13 @@ namespace :sitemap do
desc "Generates the sitemap"
task :generate do
- # TODO read configuration from a YAML file
+ require(File.join(RAILS_ROOT, 'config', 'environment'))
+
+ # Finds models and generates the sitemap
+ SitemapGenerator::Generator.run
+
+ # You can also generate a sitemap 'manually' like this:
+=begin
SitemapGenerator::Generator.generate 'config' do |host, data|
Post.all(:order => 'created_at desc', :limit => 5000).each do |post|
data.add post, 0.7, :monthly
@@ -17,11 +23,8 @@ namespace :sitemap do
Tag.all(:order => 'created_at desc', :limit => 5000).each do |tag|
data.add tag, 0.5, :weekly
end
-
- Brand.all(:order => 'created_at desc', :limit => 5000).each do |brand|
- data.add brand, 0.7, :weekly
- end
end
+=end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.