sitemap_generator 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -3,6 +3,8 @@ SitemapGenerator
3
3
 
4
4
  This plugin enables ['enterprise-class'][enterprise_class] Google Sitemaps to be easily generated for a Rails site as a rake task, using a simple 'Rails Routes'-like DSL.
5
5
 
6
+ **Now supporting Rails 3 as of version 0.2.5!**
7
+
6
8
  Foreword
7
9
  -------
8
10
 
@@ -10,78 +12,52 @@ Unfortunately, Adam Salter passed away in 2009. Those who knew him know what an
10
12
 
11
13
  [Karl Varga](http://github.com/kjvarga) has taken over development of SitemapGenerator. The canonical repository is [http://github.com/kjvarga/sitemap_generator][canonical_repo]
12
14
 
13
- Raison d'être
14
- -------
15
-
16
- Most of the Sitemap plugins out there seem to try to recreate the Sitemap links by iterating the Rails routes. In some cases this is possible, but for a great deal of cases it isn't.
15
+ Installation
16
+ =======
17
17
 
18
- a) There are probably quite a few routes in your routes file that don't need inclusion in the Sitemap. (AJAX routes I'm looking at you.)
18
+ **Rails 3:**
19
19
 
20
- and
20
+ 1. Add the gem to your <tt>Gemspec</tt>
21
21
 
22
- b) How would you infer the correct series of links for the following route?
22
+ <code>gem 'sitemap_generator'</code>
23
23
 
24
- map.zipcode 'location/:state/:city/:zipcode', :controller => 'zipcode', :action => 'index'
24
+ 2. `$ rake sitemap:install`
25
25
 
26
- Don't tell me it's trivial, because it isn't. It just looks trivial.
27
-
28
- So my idea is to have another file similar to 'routes.rb' called 'sitemap.rb', where you can define what goes into the Sitemap.
29
-
30
- Here's my solution:
31
-
32
- Zipcode.find(:all, :include => :city).each do |z|
33
- sitemap.add zipcode_path(:state => z.city.state, :city => z.city, :zipcode => z)
34
- end
26
+ **Rails 2.x: As a gem**
35
27
 
36
- Easy hey?
37
-
38
- Other Sitemap settings for the link, like `lastmod`, `priority`, `changefreq` and `host` are entered automatically, although you can override them if you need to.
39
-
40
- Other "difficult" Sitemap issues, solved by this plugin:
41
-
42
- - Support for more than 50,000 urls (using a Sitemap Index file)
43
- - Gzip of Sitemap files
44
- - Variable priority of links
45
- - Paging/sorting links (e.g. my_list?page=3)
46
- - SSL host links (e.g. https:)
47
- - Rails apps which are installed on a sub-path (e.g. example.com/blog_app/)
48
-
49
- Installation
50
- =======
28
+ 1. Add the gem as a dependency in your <tt>config/environment.rb</tt>
51
29
 
52
- **As a gem**
53
-
54
- 1. Add the gem as a dependency in your config/environment.rb
55
-
56
- <code>config.gem 'sitemap_generator', :lib => false, :source => 'http://gemcutter.org'</code>
30
+ <code>config.gem 'sitemap_generator', :lib => false</code>
57
31
 
58
32
  2. `$ rake gems:install`
59
33
 
60
- 3. Add the following line to your RAILS_ROOT/Rakefile
34
+ 3. Add the following to your <tt>RAILS_ROOT/Rakefile</tt>
61
35
 
62
- <code>require 'sitemap_generator/tasks' rescue LoadError</code>
36
+ <pre>begin
37
+ require 'sitemap_generator/tasks'
38
+ rescue Exception => e
39
+ puts "Warning, couldn't load gem tasks: #{e.message}! Skipping..."
40
+ end</pre>
63
41
 
64
42
  4. `$ rake sitemap:install`
65
43
 
66
- **As a plugin**
67
-
68
- 1. Install plugin as normal
44
+ **Rails 2.x: As a plugin**
69
45
 
70
- <code>$ ./script/plugin install git://github.com/kjvarga/sitemap_generator.git</code>
46
+ 1. <code>$ ./script/plugin install git://github.com/kjvarga/sitemap_generator.git</code>
71
47
 
72
48
  ----
73
49
 
74
- Installation should create a 'config/sitemap.rb' file which will contain your logic for generation of the Sitemap files. (If you want to recreate this file manually run `rake sitemap:install`)
50
+ Installation creates a <tt>config/sitemap.rb</tt> file which will contain your logic for generating the Sitemap files. If you want to create this file manually run <code>rake sitemap:install</code>.
75
51
 
76
- You can run `rake sitemap:refresh` as needed to create Sitemap files. This will also ping all the ['major'][sitemap_engines] search engines. (if you want to disable all non-essential output run the rake task thusly `rake -s sitemap:refresh`)
52
+ You can run <code>rake sitemap:refresh</code> as needed to create Sitemap files. This will also ping all the ['major'][sitemap_engines] search engines. If you want to disable all non-essential output run the rake task with <code>rake -s sitemap:refresh</code>.
77
53
 
78
54
  Sitemaps with many urls (100,000+) take quite a long time to generate, so if you need to refresh your Sitemaps regularly you can set the rake task up as a cron job. Most cron agents will only send you an email if there is output from the cron task.
79
55
 
80
- Optionally, you can add the following to your robots.txt file, so that robots can find the sitemap file.
56
+ Optionally, you can add the following to your <code>public/robots.txt</code> file, so that robots can find the sitemap file.
81
57
 
82
58
  Sitemap: <hostname>/sitemap_index.xml.gz
83
59
 
84
- The robots.txt Sitemap URL should be the complete URL to the Sitemap Index, such as: `http://www.example.org/sitemap_index.xml.gz`
60
+ The Sitemap URL in the robots file should be the complete URL to the Sitemap Index, such as <tt>http://www.example.org/sitemap_index.xml.gz</tt>
85
61
 
86
62
 
87
63
  Example 'config/sitemap.rb'
@@ -130,12 +106,55 @@ Example 'config/sitemap.rb'
130
106
  file = File.join(Rails.root, 'vendor/plugins/cadability_client/config/sitemap.rb')
131
107
  eval(open(file).read, binding, file)
132
108
 
133
- Notes
109
+ Raison d'être
110
+ -------
111
+
112
+ Most of the Sitemap plugins out there seem to try to recreate the Sitemap links by iterating the Rails routes. In some cases this is possible, but for a great deal of cases it isn't.
113
+
114
+ a) There are probably quite a few routes in your routes file that don't need inclusion in the Sitemap. (AJAX routes I'm looking at you.)
115
+
116
+ and
117
+
118
+ b) How would you infer the correct series of links for the following route?
119
+
120
+ map.zipcode 'location/:state/:city/:zipcode', :controller => 'zipcode', :action => 'index'
121
+
122
+ Don't tell me it's trivial, because it isn't. It just looks trivial.
123
+
124
+ So my idea is to have another file similar to 'routes.rb' called 'sitemap.rb', where you can define what goes into the Sitemap.
125
+
126
+ Here's my solution:
127
+
128
+ Zipcode.find(:all, :include => :city).each do |z|
129
+ sitemap.add zipcode_path(:state => z.city.state, :city => z.city, :zipcode => z)
130
+ end
131
+
132
+ Easy hey?
133
+
134
+ Other Sitemap settings for the link, like `lastmod`, `priority`, `changefreq` and `host` are entered automatically, although you can override them if you need to.
135
+
136
+ Other "difficult" Sitemap issues, solved by this plugin:
137
+
138
+ - Support for more than 50,000 urls (using a Sitemap Index file)
139
+ - Gzip of Sitemap files
140
+ - Variable priority of links
141
+ - Paging/sorting links (e.g. my_list?page=3)
142
+ - SSL host links (e.g. https:)
143
+ - Rails apps which are installed on a sub-path (e.g. example.com/blog_app/)
144
+
145
+ Compatibility
134
146
  =======
135
147
 
136
- 1) Tested/working on Rails 1.x.x <=> 2.x.x, no guarantees made for Rails 3.0.
148
+ Tested and working on:
149
+
150
+ - **Rails** 3.0.0, sitemap_generator version >= 0.2.5
151
+ - **Rails** 1.x - 2.3.5 sitemap_generator version < 0.2.5
152
+ - **Ruby** 1.8.7, 1.9.1
153
+
154
+ Notes
155
+ =======
137
156
 
138
- 2) For large sitemaps it may be useful to split your generation into batches to avoid running out of memory. E.g.:
157
+ 1) For large sitemaps it may be useful to split your generation into batches to avoid running out of memory. E.g.:
139
158
 
140
159
  # add movies
141
160
  Movie.find_in_batches(:batch_size => 1000) do |movies|
@@ -144,7 +163,7 @@ Notes
144
163
  end
145
164
  end
146
165
 
147
- 3) New Capistrano deploys will remove your Sitemap files, unless you run `rake sitemap:refresh`. The way around this is to create a cap task:
166
+ 2) New Capistrano deploys will remove your Sitemap files, unless you run `rake sitemap:refresh`. The way around this is to create a cap task:
148
167
 
149
168
  after "deploy:update_code", "deploy:copy_old_sitemap"
150
169
 
@@ -154,7 +173,7 @@ Notes
154
173
  end
155
174
  end
156
175
 
157
- 4) If generation of your sitemap fails for some reason, the old sitemap will remain in public/. This ensures that robots will always find a valid sitemap. Running silently (`rake -s sitemap:refresh`) and with email forwarding setup you'll only get an email if your sitemap fails to build, and no notification when everything is fine - which will be most of the time.
176
+ 3) If generation of your sitemap fails for some reason, the old sitemap will remain in public/. This ensures that robots will always find a valid sitemap. Running silently (`rake -s sitemap:refresh`) and with email forwarding setup you'll only get an email if your sitemap fails to build, and no notification when everything is fine - which will be most of the time.
158
177
 
159
178
  Known Bugs
160
179
  ========
data/Rakefile CHANGED
@@ -1,5 +1,7 @@
1
1
  require 'rake'
2
2
  require 'rake/rdoctask'
3
+ require 'rubygems'
4
+ gem 'rspec', '1.3.0'
3
5
  require 'spec/rake/spectask'
4
6
 
5
7
  begin
@@ -7,7 +9,7 @@ begin
7
9
  Jeweler::Tasks.new do |gem|
8
10
  gem.name = "sitemap_generator"
9
11
  gem.summary = %Q{Easily generate enterprise class Sitemaps for your Rails site using a simple 'Rails Routes'-like DSL and a single Rake task}
10
- gem.description = %Q{Installs as a plugin or Gem to easily generate enterprise class Sitemaps readable by all search engines. Automatically ping search engines to notify them of new sitemaps, including Google, Yahoo and Bing. Provides rake tasks to easily manage your sitemaps. Won't clobber your old sitemaps if the new one fails to generate. Setup a cron schedule and never worry about your sitemaps again.}
12
+ gem.description = %Q{A Rails 3-compatible gem to easily generate enterprise class Sitemaps readable by all search engines. Automatically ping search engines to notify them of new sitemaps, including Google, Yahoo and Bing. Provides rake tasks to easily manage your sitemaps. Won't clobber your old sitemaps if the new one fails to generate. Setup a cron schedule and never worry about your sitemaps again.}
11
13
  gem.email = "kjvarga@gmail.com"
12
14
  gem.homepage = "http://github.com/kjvarga/sitemap_generator"
13
15
  gem.authors = ["Adam Salter", "Karl Varga"]
@@ -25,6 +27,7 @@ task :default => :test
25
27
  namespace :test do
26
28
  task :gem => ['test:prepare:gem', 'multi_spec']
27
29
  task :plugin => ['test:prepare:plugin', 'multi_spec']
30
+ task :rails3 => ['test:prepare:rails3', 'multi_spec']
28
31
 
29
32
  task :multi_spec do
30
33
  Rake::Task['spec'].invoke
@@ -44,6 +47,11 @@ namespace :test do
44
47
  rm_rf(local_path('spec/mock_app_plugin/public/sitemap*'))
45
48
  end
46
49
 
50
+ task :rails3 do
51
+ ENV["SITEMAP_RAILS"] = 'rails3'
52
+ rm_rf(local_path('spec/mock_rails3_gem/public/sitemap*'))
53
+ end
54
+
47
55
  def local_path(path)
48
56
  File.join(File.dirname(__FILE__), path)
49
57
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.4
1
+ 0.2.5
@@ -1,7 +1,10 @@
1
1
  require 'sitemap_generator/mapper'
2
2
  require 'sitemap_generator/link'
3
- require 'sitemap_generator/link_set'
3
+ require 'sitemap_generator/rails_helper'
4
4
  require 'sitemap_generator/helper'
5
+ require 'sitemap_generator/link_set'
6
+
7
+ require 'sitemap_generator/railtie' if SitemapGenerator::RailsHelper.rails3?
5
8
 
6
9
  module SitemapGenerator
7
10
  silence_warnings do
@@ -19,5 +22,5 @@ module SitemapGenerator
19
22
  :sitemap_index => File.join(self.root, 'templates/sitemap_index.builder'),
20
23
  :sitemap_xml => File.join(self.root, 'templates/xml_sitemap.builder'),
21
24
  :sitemap_sample => File.join(self.root, 'templates/sitemap.rb'),
22
- }
23
- end
25
+ }
26
+ end
@@ -1,10 +1,7 @@
1
- require 'sitemap_generator'
2
- require 'action_controller'
3
-
4
1
  module SitemapGenerator
2
+ # UrlHelpers are included by the rake tasks. This is not ideal, but should
3
+ # suffice until things are better organized.
5
4
  module Helper
6
- include ActionController::UrlWriter
7
-
8
5
  def self.included(base)
9
6
  base.class_eval do
10
7
  def self.default_url_options(options = nil)
@@ -14,18 +11,18 @@ module SitemapGenerator
14
11
  end
15
12
 
16
13
  def load_sitemap_rb
17
- sitemap_mapper_file = File.join(RAILS_ROOT, 'config/sitemap.rb')
14
+ sitemap_mapper_file = File.join(Rails.root, 'config/sitemap.rb')
18
15
  eval(open(sitemap_mapper_file).read)
19
16
  end
20
-
17
+
21
18
  def url_with_hostname(path)
22
19
  URI.join(SitemapGenerator::Sitemap.default_host, path).to_s
23
20
  end
24
-
21
+
25
22
  def w3c_date(date)
26
23
  date.utc.strftime("%Y-%m-%dT%H:%M:%S+00:00")
27
24
  end
28
-
25
+
29
26
  def ping_search_engines(sitemap_index)
30
27
  require 'open-uri'
31
28
  index_location = CGI.escape(url_with_hostname(sitemap_index))
@@ -1,4 +1,5 @@
1
- require File.dirname(__FILE__) + '/helper'
1
+ require 'builder'
2
+ require 'action_view'
2
3
 
3
4
  module SitemapGenerator
4
5
  class LinkSet
@@ -62,7 +63,7 @@ module SitemapGenerator
62
63
  buffer = ""
63
64
  xml = Builder::XmlMarkup.new(:target => buffer)
64
65
  eval(File.read(SitemapGenerator.templates[:sitemap_xml]), binding)
65
- filename = File.join(RAILS_ROOT, "public", file)
66
+ filename = File.join(Rails.root, "public", file)
66
67
  write_file(filename, buffer)
67
68
  show_progress("Sitemap", filename, buffer) if verbose
68
69
  links.clear
@@ -74,7 +75,7 @@ module SitemapGenerator
74
75
  buffer = ""
75
76
  xml = Builder::XmlMarkup.new(:target => buffer)
76
77
  eval(File.read(SitemapGenerator.templates[:sitemap_index]), binding)
77
- filename = File.join(RAILS_ROOT, "public", index_file)
78
+ filename = File.join(Rails.root, "public", index_file)
78
79
  write_file(filename, buffer)
79
80
  show_progress("Sitemap Index", filename, buffer) if verbose
80
81
  links.clear
@@ -144,24 +145,24 @@ module SitemapGenerator
144
145
 
145
146
  # Copy templates/sitemap.rb to config if not there yet.
146
147
  def install_sitemap_rb
147
- if File.exist?(File.join(RAILS_ROOT, 'config/sitemap.rb'))
148
+ if File.exist?(File.join(Rails.root, 'config/sitemap.rb'))
148
149
  puts "already exists: config/sitemap.rb, file not copied"
149
150
  else
150
- FileUtils.cp(SitemapGenerator.templates[:sitemap_sample], File.join(RAILS_ROOT, 'config/sitemap.rb'))
151
+ FileUtils.cp(SitemapGenerator.templates[:sitemap_sample], File.join(Rails.root, 'config/sitemap.rb'))
151
152
  puts "created: config/sitemap.rb"
152
153
  end
153
154
  end
154
155
 
155
156
  # Remove config/sitemap.rb if exists.
156
157
  def uninstall_sitemap_rb
157
- if File.exist?(File.join(RAILS_ROOT, 'config/sitemap.rb'))
158
- File.rm(File.join(RAILS_ROOT, 'config/sitemap.rb'))
158
+ if File.exist?(File.join(Rails.root, 'config/sitemap.rb'))
159
+ File.rm(File.join(Rails.root, 'config/sitemap.rb'))
159
160
  end
160
161
  end
161
162
 
162
163
  # Clean sitemap files in output directory.
163
164
  def clean_files
164
- FileUtils.rm(Dir[File.join(RAILS_ROOT, 'public/sitemap*.xml.gz')])
165
+ FileUtils.rm(Dir[File.join(Rails.root, 'public/sitemap*.xml.gz')])
165
166
  end
166
167
 
167
168
  # Ping search engines passing sitemap location.
@@ -0,0 +1,28 @@
1
+ module SitemapGenerator
2
+ module RailsHelper
3
+ # Returns whether this environment is using ActionPack
4
+ # version 3.0.0 or greater.
5
+ #
6
+ # @return [Boolean]
7
+ def self.rails3?
8
+ # The ActionPack module is always loaded automatically in Rails >= 3
9
+ return false unless defined?(ActionPack) && defined?(ActionPack::VERSION)
10
+
11
+ version =
12
+ if defined?(ActionPack::VERSION::MAJOR)
13
+ ActionPack::VERSION::MAJOR
14
+ else
15
+ # Rails 1.2
16
+ ActionPack::VERSION::Major
17
+ end
18
+
19
+ # 3.0.0.beta1 acts more like ActionPack 2
20
+ # for purposes of this method
21
+ # (checking whether block helpers require = or -).
22
+ # This extra check can be removed when beta2 is out.
23
+ version >= 3 &&
24
+ !(defined?(ActionPack::VERSION::TINY) &&
25
+ ActionPack::VERSION::TINY == "0.beta")
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,7 @@
1
+ module SitemapGenerator
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks do
4
+ load File.expand_path('../../../tasks/sitemap_generator_tasks.rake', __FILE__)
5
+ end
6
+ end
7
+ end
@@ -21,6 +21,18 @@ namespace :sitemap do
21
21
  task 'refresh:no_ping' => ['sitemap:create']
22
22
 
23
23
  task :create => [:environment] do
24
+ # TODO: Move away from auto-instantiating SitemapGenerator::Sitemap
25
+ # and move to a more natural Sitemap.new or similar.
26
+ if SitemapGenerator::RailsHelper.rails3?
27
+ SitemapGenerator::Sitemap.class_eval do
28
+ include Rails.application.routes.url_helpers
29
+ end
30
+ else
31
+ require 'action_controller'
32
+ SitemapGenerator::Sitemap.class_eval do
33
+ include ActionController::UrlWriter
34
+ end
35
+ end
24
36
  SitemapGenerator::Sitemap.create_files
25
37
  end
26
38
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sitemap_generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 5
9
+ version: 0.2.5
5
10
  platform: ruby
6
11
  authors:
7
12
  - Adam Salter
@@ -10,20 +15,22 @@ autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
17
 
13
- date: 2010-04-01 00:00:00 -07:00
18
+ date: 2010-04-19 00:00:00 -07:00
14
19
  default_executable:
15
20
  dependencies:
16
21
  - !ruby/object:Gem::Dependency
17
22
  name: rspec
18
- type: :development
19
- version_requirement:
20
- version_requirements: !ruby/object:Gem::Requirement
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
21
25
  requirements:
22
26
  - - ">="
23
27
  - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
24
30
  version: "0"
25
- version:
26
- description: Installs as a plugin or Gem to easily generate enterprise class Sitemaps readable by all search engines. Automatically ping search engines to notify them of new sitemaps, including Google, Yahoo and Bing. Provides rake tasks to easily manage your sitemaps. Won't clobber your old sitemaps if the new one fails to generate. Setup a cron schedule and never worry about your sitemaps again.
31
+ type: :development
32
+ version_requirements: *id001
33
+ description: A Rails 3-compatible gem to easily generate enterprise class Sitemaps readable by all search engines. Automatically ping search engines to notify them of new sitemaps, including Google, Yahoo and Bing. Provides rake tasks to easily manage your sitemaps. Won't clobber your old sitemaps if the new one fails to generate. Setup a cron schedule and never worry about your sitemaps again.
27
34
  email: kjvarga@gmail.com
28
35
  executables: []
29
36
 
@@ -41,6 +48,8 @@ files:
41
48
  - lib/sitemap_generator/link.rb
42
49
  - lib/sitemap_generator/link_set.rb
43
50
  - lib/sitemap_generator/mapper.rb
51
+ - lib/sitemap_generator/rails_helper.rb
52
+ - lib/sitemap_generator/railtie.rb
44
53
  - lib/sitemap_generator/tasks.rb
45
54
  - rails/install.rb
46
55
  - rails/uninstall.rb
@@ -61,18 +70,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
61
70
  requirements:
62
71
  - - ">="
63
72
  - !ruby/object:Gem::Version
73
+ segments:
74
+ - 0
64
75
  version: "0"
65
- version:
66
76
  required_rubygems_version: !ruby/object:Gem::Requirement
67
77
  requirements:
68
78
  - - ">="
69
79
  - !ruby/object:Gem::Version
80
+ segments:
81
+ - 0
70
82
  version: "0"
71
- version:
72
83
  requirements: []
73
84
 
74
85
  rubyforge_project:
75
- rubygems_version: 1.3.5
86
+ rubygems_version: 1.3.6
76
87
  signing_key:
77
88
  specification_version: 3
78
89
  summary: Easily generate enterprise class Sitemaps for your Rails site using a simple 'Rails Routes'-like DSL and a single Rake task