ar_sitemapper 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +9 -0
  3. data/.ruby-gemset +1 -0
  4. data/.ruby-version +1 -0
  5. data/Changelog.rdoc +28 -0
  6. data/Gemfile +4 -0
  7. data/Gemfile.lock +98 -0
  8. data/LICENSE +24 -0
  9. data/README.rdoc +176 -0
  10. data/Rakefile +23 -0
  11. data/ar_sitemapper.gemspec +26 -0
  12. data/install.rb +5 -0
  13. data/lib/ar_sitemapper.rb +11 -0
  14. data/lib/sitemapper/active_record/builder.rb +54 -0
  15. data/lib/sitemapper/engine.rb +26 -0
  16. data/lib/sitemapper/generator.rb +100 -0
  17. data/lib/sitemapper/index.rb +84 -0
  18. data/lib/sitemapper/loader.rb +39 -0
  19. data/lib/sitemapper/pinger.rb +21 -0
  20. data/lib/sitemapper/sitemap.rb +22 -0
  21. data/lib/sitemapper/urlset.rb +76 -0
  22. data/lib/sitemapper/version.rb +5 -0
  23. data/lib/tasks/sitemapper.rake +26 -0
  24. data/templates/sitemaps.yml +34 -0
  25. data/test/dummy/README.rdoc +261 -0
  26. data/test/dummy/Rakefile +7 -0
  27. data/test/dummy/app/assets/javascripts/application.js +15 -0
  28. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  29. data/test/dummy/app/controllers/application_controller.rb +3 -0
  30. data/test/dummy/app/helpers/application_helper.rb +2 -0
  31. data/test/dummy/app/mailers/.gitkeep +0 -0
  32. data/test/dummy/app/models/.gitkeep +0 -0
  33. data/test/dummy/app/views/layouts/application.html.erb +24 -0
  34. data/test/dummy/config.ru +4 -0
  35. data/test/dummy/config/application.rb +50 -0
  36. data/test/dummy/config/boot.rb +10 -0
  37. data/test/dummy/config/database.yml +25 -0
  38. data/test/dummy/config/environment.rb +5 -0
  39. data/test/dummy/config/environments/development.rb +24 -0
  40. data/test/dummy/config/environments/production.rb +69 -0
  41. data/test/dummy/config/environments/test.rb +37 -0
  42. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  43. data/test/dummy/config/initializers/inflections.rb +15 -0
  44. data/test/dummy/config/initializers/mime_types.rb +5 -0
  45. data/test/dummy/config/initializers/secret_token.rb +7 -0
  46. data/test/dummy/config/initializers/session_store.rb +8 -0
  47. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  48. data/test/dummy/config/locales/en.yml +5 -0
  49. data/test/dummy/config/routes.rb +2 -0
  50. data/test/dummy/config/sitemaps.yml +36 -0
  51. data/test/dummy/db/schema.rb +74 -0
  52. data/test/dummy/lib/assets/.gitkeep +0 -0
  53. data/test/dummy/log/.gitkeep +0 -0
  54. data/test/dummy/public/404.html +26 -0
  55. data/test/dummy/public/422.html +26 -0
  56. data/test/dummy/public/500.html +25 -0
  57. data/test/dummy/public/favicon.ico +0 -0
  58. data/test/dummy/script/rails +6 -0
  59. data/test/support/app/models/foo_bar.rb +24 -0
  60. data/test/test_helper.rb +13 -0
  61. data/test/unit/engine_test.rb +19 -0
  62. data/test/unit/generator_test.rb +14 -0
  63. data/test/unit/loader_test.rb +21 -0
  64. data/test/unit/map_test.rb +9 -0
  65. data/test/unit/pinger_test.rb +11 -0
  66. data/test/unit/sitemapper_test.rb +22 -0
  67. data/test/unit/urlset_test.rb +9 -0
  68. metadata +221 -0
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MmU4N2FhNWNmNjgxOWY5NzlkNmU2YTdkOGIxYTQ5N2U3MGRiYTk1Zg==
5
+ data.tar.gz: !binary |-
6
+ MTUzMzE5OWU3YWRmZTI3YzBlY2I0OTE1YzkyMzU0MDA4OWIzYjI3ZQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NWU3Y2ZiOTE4Njg5NDA5NGUxN2IyODliYzg0ZWRlZTZkYTY4NmZkMDBiN2Ey
10
+ ZjhiNGU0YWQwOTM5ZjAxZTkyNTE0ODdiYWUxNjQwMThmMWNkOTgxZWY0NzY1
11
+ YTFkNzBhZjQwNmU5YjNhN2JiM2NiNGE2OTRjZmUyYjNhZDVkMWY=
12
+ data.tar.gz: !binary |-
13
+ N2NkMWQ2OWM3ZmFhMDY5MjhmNjkyNGQ3NzM1MWE0MWI4MWY3OThiYWZiZGQw
14
+ NmRiNzFmMWYzZGVkZDkxYmRiNDJjYzY2MjY0MDdhY2NlZTdkMWU2NzVhMzE4
15
+ ZDZjMWI1ODhkY2UyNmE1MjMwNzYyNmJjN2I5ZjE2MDQwZjk5YzY=
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ .DS_Store
2
+ nbproject
3
+
4
+ *~
5
+ *.swp
6
+
7
+ test/dummy/.sass-cache
8
+ test/dummy/log/*.log
9
+ test/dummy/db/*.sqlite3
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ ar_sitemapper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 1.9.3
data/Changelog.rdoc ADDED
@@ -0,0 +1,28 @@
1
+ == CHANGELOG
2
+
3
+ === v1.0.0 // 2013-09-28
4
+ * Transformed into a gem (just for sport)
5
+ * Removed Rails 2'ism
6
+ * Splits sitemaps with over 50,000 entries
7
+ * Rails integration as engine
8
+ * Removed hardcoded sitemaps.yml configuration
9
+
10
+ === v0.5 // 2010-10-25
11
+ * supports conditions and named scopes for build_sitemap via YML file
12
+ * documentation and samples
13
+
14
+ === v0.4 // 2010-09-24
15
+ * supports Proc code in sitemap yaml for dynamic url generation based on model iteration
16
+ * additional files can now be added to the sitemap index file.
17
+ * last-modified for models' sitemaps within the sitemap index file is now based on :created_at when a corresponding column is found
18
+ * added barebone fake app for running tests
19
+
20
+ === v0.3 // 2010-09-22
21
+ * classes restructured
22
+
23
+ === v0.3 // 2010-07-20
24
+ * added Map class
25
+ * filename will be auto-derived by modelname by default now
26
+
27
+ === v0.1 // 2010-07-16
28
+ * Initial version
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ar_sitemapper.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,98 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ar_sitemapper (1.0.0)
5
+ activesupport (>= 3.0, < 4.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionmailer (3.2.14)
11
+ actionpack (= 3.2.14)
12
+ mail (~> 2.5.4)
13
+ actionpack (3.2.14)
14
+ activemodel (= 3.2.14)
15
+ activesupport (= 3.2.14)
16
+ builder (~> 3.0.0)
17
+ erubis (~> 2.7.0)
18
+ journey (~> 1.0.4)
19
+ rack (~> 1.4.5)
20
+ rack-cache (~> 1.2)
21
+ rack-test (~> 0.6.1)
22
+ sprockets (~> 2.2.1)
23
+ activemodel (3.2.14)
24
+ activesupport (= 3.2.14)
25
+ builder (~> 3.0.0)
26
+ activerecord (3.2.14)
27
+ activemodel (= 3.2.14)
28
+ activesupport (= 3.2.14)
29
+ arel (~> 3.0.2)
30
+ tzinfo (~> 0.3.29)
31
+ activeresource (3.2.14)
32
+ activemodel (= 3.2.14)
33
+ activesupport (= 3.2.14)
34
+ activesupport (3.2.14)
35
+ i18n (~> 0.6, >= 0.6.4)
36
+ multi_json (~> 1.0)
37
+ arel (3.0.2)
38
+ builder (3.0.4)
39
+ erubis (2.7.0)
40
+ hike (1.2.3)
41
+ i18n (0.6.5)
42
+ journey (1.0.4)
43
+ json (1.8.0)
44
+ mail (2.5.4)
45
+ mime-types (~> 1.16)
46
+ treetop (~> 1.4.8)
47
+ metaclass (0.0.1)
48
+ mime-types (1.25)
49
+ mocha (0.14.0)
50
+ metaclass (~> 0.0.1)
51
+ multi_json (1.8.0)
52
+ polyglot (0.3.3)
53
+ rack (1.4.5)
54
+ rack-cache (1.2)
55
+ rack (>= 0.4)
56
+ rack-ssl (1.3.3)
57
+ rack
58
+ rack-test (0.6.2)
59
+ rack (>= 1.0)
60
+ rails (3.2.14)
61
+ actionmailer (= 3.2.14)
62
+ actionpack (= 3.2.14)
63
+ activerecord (= 3.2.14)
64
+ activeresource (= 3.2.14)
65
+ activesupport (= 3.2.14)
66
+ bundler (~> 1.0)
67
+ railties (= 3.2.14)
68
+ railties (3.2.14)
69
+ actionpack (= 3.2.14)
70
+ activesupport (= 3.2.14)
71
+ rack-ssl (~> 1.3.2)
72
+ rake (>= 0.8.7)
73
+ rdoc (~> 3.4)
74
+ thor (>= 0.14.6, < 2.0)
75
+ rake (10.1.0)
76
+ rdoc (3.12.2)
77
+ json (~> 1.4)
78
+ sprockets (2.2.2)
79
+ hike (~> 1.2)
80
+ multi_json (~> 1.0)
81
+ rack (~> 1.0)
82
+ tilt (~> 1.1, != 1.3.0)
83
+ sqlite3 (1.3.8)
84
+ thor (0.18.1)
85
+ tilt (1.4.1)
86
+ treetop (1.4.15)
87
+ polyglot
88
+ polyglot (>= 0.3.1)
89
+ tzinfo (0.3.37)
90
+
91
+ PLATFORMS
92
+ ruby
93
+
94
+ DEPENDENCIES
95
+ ar_sitemapper!
96
+ mocha
97
+ rails (>= 3.0, < 4.0)
98
+ sqlite3
data/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ Copyright (c) 2010, Carsten Zimmermann
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+ * Redistributions of source code must retain the above copyright
7
+ notice, this list of conditions and the following disclaimer.
8
+ * Redistributions in binary form must reproduce the above copyright
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
+ * Neither the name of the original author / copyright holder nor the
12
+ names of its contributors may be used to endorse or promote products
13
+ derived from this software without specific prior written permission.
14
+
15
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
19
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.rdoc ADDED
@@ -0,0 +1,176 @@
1
+ == ActiveRecord Sitemapper
2
+ === Low-level sitemap-building
3
+
4
+ The standard approach to generate XML sitemaps for a Ruby on Rails application
5
+ seems to be creating a special SitemapsController with xml views and a bit
6
+ of routes.rb magic.
7
+
8
+ I used to generate sitemaps created thus via a nightly rake task to have them
9
+ cached and delivered statically by Apache. That felt wrong, I wanted a more
10
+ "low-level" approach. Actually, as low-level as possible and I ended up at
11
+ ActiveRecord::Base: this plugin enhances your ActiveRecord model to export
12
+ its data to a sitemap in an intuitive way.
13
+
14
+ Also yes, I know there are probably a gazillion projects covering this already.
15
+
16
+ == Installation
17
+
18
+ To add *ar_sitemapper* to your Rails 3 project, add it to your Gemfile:
19
+
20
+ gem 'ar_sitemapper', '~> 1.0.0'
21
+
22
+ == Usage
23
+
24
+ === ActiveRecord drop-in
25
+
26
+ You can use +build_sitemap+ directly on any ActiveRecord model. The following
27
+ example will create a sitemap of all your Content objects as Rails.root/public/sitemap_contents.xml.gz:
28
+
29
+ Content.build_sitemap :all do |content, xml|
30
+ xml.loc content_url(content)
31
+ xml.changefreq 'weekly'
32
+ xml.lastmod content.updated_at.strftime("%Y-%m-%d")
33
+ xml.priority 0.5
34
+ end
35
+
36
+ You can pass ActiveRecord::Base.find options as arguments to +build_sitemap+
37
+ to fine-tune the selection of objects you want to extract:
38
+
39
+ Post.build_sitemap :all, :conditions => ["published IS TRUE"], :order => :name do |post, xml|
40
+ # do stuff here
41
+ end
42
+
43
+ *Note*: if you want to use your named route helpers in the block, be sure to have
44
+ ActionController::UrlWriter included. Setting default_url_options[:host]
45
+ is helpful, too.
46
+
47
+ === Output
48
+
49
+ *ar_sitemapper* will derive its sitemap filename from the model's name and will
50
+ default to Gzip compressed output. If can write to +stdout+ as well if +file+
51
+ is explicitely set to +nil+ or +false+.
52
+
53
+ It will automatically compress your XML sitemap with Gzip if the
54
+ filename parameter suggests it (ie. ends with ".gz"). Alternatively, you can
55
+ enforce or disable Gzip compression by passing true or false to the +gzip+ option.
56
+
57
+ Both of the following examples will create a gzip'ed file named "test.xml.gz":
58
+
59
+ Content.build_sitemap :all, :file => "test.xml.gz" { |content, xml| ... }
60
+ Content.build_sitemap :all, :file => "test.xml", :gzip => true { |content, xml| ... }
61
+
62
+ === Sitemaps for static data
63
+
64
+ Sitemap creation is done via a block, so you can do whatever you want in it.
65
+ For added flexibility, you can use *ar_sitemapper* with arbitrary data
66
+ collections (ie. generating sitemaps for static content). Future versions will
67
+ support YAML configuration of such static content.
68
+
69
+ sites = [
70
+ { :url => "http://example.com/your/static/content1.html", :freq => "always", :prio => "1.0" },
71
+ { :url => "http://example.com/your/static/content2.html", :freq => "monthly", :prio => "0.3" },
72
+ ]
73
+
74
+ AegisNet::Sitemapper::Generator.create(sites) do |site, xml|
75
+ xml.loc site[:url]
76
+ xml.changefreq site[:freq]
77
+ xml.priority site[:prio]
78
+ end
79
+
80
+ === YAML configuration
81
+ Sitemapper supports configuration via a YAML file which is expected to reside in
82
+ <tt>Rails.root/config/sitemaps.yml</tt>.
83
+
84
+ ==== Supported top level configuration options
85
+ * +default_host+: base hostname used for url generation (mandatory).
86
+ * +local_path+: where to store the generated sitemaps on the local system (mandatory).
87
+ * +ping+: boolean whether or not to ping search engines on successful sitemap generation.
88
+ * +index+: options for the main sitemap index file, ie. the file that references all others.
89
+ * +static+: options for the sitemap that lists URLs to static content (ie. URLs not
90
+ necessarily related to AR models)
91
+ * +models+: options for model-related URLset sitemaps
92
+ * +ping+: a list of search engines' ping services.
93
+
94
+ ==== The <tt>index</tt> option
95
+ * +sitemapfile+: the name of the the generated sitemap.
96
+ * +includes+: an array of sitemaps that should be included in the sitemap index but
97
+ are _not_ generated directly through AR:Sitemapper (eg. a KML-sitemap).
98
+
99
+ ==== The <tt>static</tt> option
100
+ * +sitemapfile+: the name of the the generated sitemap.
101
+ * +urlset+: a list of static pages to include into the sitemap. Every item must
102
+ have a +loc+ element. A +changefreq+ and +priority+ element is optional and
103
+ defaults to _weekly_ and <i>0.5</i>, respectively.
104
+
105
+ ==== The <tt>model</tt> option
106
+ A list of models to create sitemaps for. The key must be the downcased und
107
+ underscored name of the model. Supported / required options for every model-based
108
+ sitemap are:
109
+ * +sitemapfile+: the name of the the generated sitemap (mandatory).
110
+ * +loc+: a Proc definition to generate the URLs for each object with (mandatory).
111
+ * +changefreq+: optional, defaults to 'weekly'
112
+ * +priority+: optional, defaults to '0.5'
113
+ * +lastmod+: you can use ERB to insert the date you desire.
114
+ * +conditions+: conditions to be merged into the finder of +build_sitemap+ (optional).
115
+ * +scope+: a named scope to find objects with
116
+
117
+ ==== Sample YAML file
118
+
119
+ default_host: "www.example.com"
120
+ local_path: <%= File.join Rails.root, "public", "sitemaps" %>
121
+ ping: true
122
+ index:
123
+ sitemapfile: "sitemap_index.xml"
124
+ includes:
125
+ -
126
+ loc: some_other_sitemap.xml
127
+ static:
128
+ sitemapfile: "sitemap_static.xml.gz"
129
+ urlset:
130
+ -
131
+ loc: "http://www.example.com/static/content"
132
+ changefreq: weekly
133
+ priority: 1.0
134
+ -
135
+ loc: "http://www.example.com/another/page"
136
+ changefreq: weekly
137
+ priority: 1.0
138
+ models:
139
+ foo_bar:
140
+ sitemapfile: sitemap_foo_bars.xml.gz
141
+ lastmod: <%= 2.days.ago %>
142
+ loc: Proc.new {|object| foo_bar_path(object) }
143
+ changefreq: weekly
144
+ priority: 0.7
145
+ conditions: "foo > 1"
146
+ acme:
147
+ sitemapfile: sitemap_proctests.xml.gz
148
+ scope: liquid
149
+ lastmod: <%= Acme.find(:last, :order => :updated_at).updated_at %>
150
+ loc: Proc.new {|object| acme_path(object) }
151
+ changefreq: weekly
152
+ priority: 0.8
153
+ pings:
154
+ - http://submissions.ask.com/ping?sitemap=
155
+ - http://www.google.com/webmasters/sitemaps/ping?sitemap=
156
+ - http://search.yahooapis.com/SiteExplorerService/V1/updateNotification?appid=YahooDemo&url=
157
+ - http://www.bing.com/webmaster/ping.aspx?siteMap=
158
+
159
+
160
+ === Rake task
161
+ The gem provides a rake task to rebuild all sitemaps from its config file:
162
+
163
+ rake sitemapper:rebuild
164
+
165
+
166
+ == .plan
167
+ * support KML files
168
+ * allow for a custom iterator supplied as Proc for Generator::create (ie. to make use of ARs batch finding)
169
+ * cleanup
170
+ * more flexible syntax for conditions in yml file
171
+ * guess RESTful object path and make Proc object for loc in models optional
172
+ * Install a sample sitemap.yml file
173
+
174
+ ---
175
+
176
+ Copyright (c) 2010-2013 Carsten Zimmermann, released under a BSD-type license
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+ require 'rdoc/task'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the ar_sitemapper plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the ar_sitemapper plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'ActiveRecord Sitemapper'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README*')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ require "sitemapper/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ar_sitemapper"
8
+ spec.version = AegisNet::Sitemapper::VERSION
9
+ spec.authors = ["Carsten Zimmermann"]
10
+ spec.email = ["cz@aegisnet.de"]
11
+ spec.description = %q{Faciliates generating static sitemap XML files from ActiveRecord}
12
+ spec.summary = %q{Faciliates generating static sitemap XML files from ActiveRecord}
13
+ spec.homepage = ""
14
+ spec.license = "BSD 3-Clause"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'activesupport', '>= 3.0', '< 4.0'
22
+
23
+ spec.add_development_dependency 'sqlite3'
24
+ spec.add_development_dependency 'mocha'
25
+ spec.add_development_dependency 'rails', '>= 3.0', '< 4.0'
26
+ end
data/install.rb ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'config/environment'
4
+ FileUtils.cp File.join(File.dirname(__FILE__), "templates", "sitemapper.rake"), File.join(Rails.root, "tasks")
5
+ FileUtils.cp File.join(File.dirname(__FILE__), "templates", "sitemaps.yml"), File.join(Rails.root, "config")
@@ -0,0 +1,11 @@
1
+ require 'sitemapper/engine'
2
+
3
+ module AegisNet
4
+ module Sitemapper
5
+ mattr_accessor :configuration, :sitemap_file
6
+
7
+ def self.configure
8
+ yield self if block_given?
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,54 @@
1
+ require 'sitemapper/loader'
2
+
3
+ module AegisNet
4
+ module Sitemapper
5
+ module ActiveRecord
6
+ module Builder
7
+ def self.included(base)
8
+ base.extend SingletonMethods
9
+ end
10
+
11
+ module SingletonMethods
12
+ # Adds sitemap building functionality to ActiveRecord models.
13
+ #
14
+ # The option +:filename+ is derived from the model name and will be set to
15
+ # Rails.root/public/sitemap_modelnames.xml.gz by default. Set +:filename+
16
+ # to +nil+ or +false+ explicitely if you don't want the filename to be
17
+ # guessed (ie. if you want output to stdout).
18
+ #
19
+ # === Parameters
20
+ # * +scope+: :all, :first, :last or a named scope
21
+ # * +options+: a Hash with options to pass to ActiveRecord::Base.find and AegisNet::Sitemapper::Generator
22
+ #
23
+ # === Supported Options
24
+ # * see AegisNet::Sitemapper::Generator::VALID_GENERATOR_OPTIONS
25
+ #
26
+ # === Example
27
+ # Content.build_sitemap :all, :file => "sitemap_content.xml" do |content, xml|
28
+ # xml.loc content_path(content)
29
+ # xml.changefreq "weekly"
30
+ # xml.priority 0.5
31
+ # end
32
+ #
33
+ def build_sitemap scope, options = {}
34
+ scope = scope.to_sym
35
+ raise(ArgumentError, "Unknown ActiveRecord finder: #{scope}") unless self.respond_to?(scope)
36
+ valid_find_options = [ :conditions, :include, :joins, :limit, :offset,
37
+ :order, :select, :group, :having, :from ]
38
+ options = options.symbolize_keys!
39
+ options.assert_valid_keys(Generator::VALID_GENERATOR_OPTIONS, valid_find_options)
40
+
41
+ find_options = options.select{|option, value| valid_find_options.include?(option) }
42
+ sitemap_opts = options.delete_if{|k, v| find_options.keys.include?(k)}
43
+
44
+ # Extra treatment for the filename option
45
+ sitemap_opts[:file] = sitemap_opts.keys.include?(:file) ? sitemap_opts[:file] : AegisNet::Sitemapper::Generator.default_filename(self.class)
46
+
47
+ entries = self.send(scope, find_options).to_a # get an array for :first and :last, too
48
+ AegisNet::Sitemapper::Generator.create(entries, sitemap_opts) { |entry, xml| yield entry, xml }
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end