dynamic_sitemaps 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,89 @@
1
+ class SitemapsController < ApplicationController
2
+ def sitemap
3
+ new_page!
4
+ instance_eval &DynamicSitemaps::Sitemap.draw_block
5
+
6
+ if params[:page]
7
+ if pages.count > 1
8
+ page = pages[params[:page].to_i - 1]
9
+
10
+ if page
11
+ @urls = page.urls
12
+ else
13
+ @urls = []
14
+ end
15
+ else
16
+ @urls = []
17
+ end
18
+ elsif pages.count > 1
19
+ @pages = pages
20
+ render :index
21
+ else
22
+ @urls = pages.last.urls
23
+ end
24
+ end
25
+
26
+ protected
27
+
28
+ def pages
29
+ @pages ||= []
30
+ end
31
+
32
+ def new_page!
33
+ pages << DynamicSitemaps::Page.new
34
+ end
35
+
36
+ def current_page
37
+ pages.last
38
+ end
39
+
40
+ def per_page(size = nil)
41
+ @per_page ||= size || 50000
42
+ end
43
+
44
+ def url(loc, options = {})
45
+ if current_page.urls.count >= per_page
46
+ new_page!
47
+ end
48
+
49
+ loc = polymorphic_url(loc) unless loc.is_a?(String)
50
+
51
+ current_page.urls << DynamicSitemaps::Url.new(loc, options)
52
+ end
53
+
54
+ def autogenerate(*args)
55
+ options = args.extract_options!
56
+
57
+ args.flatten.each do |sym|
58
+ sym.to_s.singularize.camelize.constantize.send(:all).each do |obj|
59
+ url obj, convert_options(options, obj)
60
+ end
61
+ end
62
+ end
63
+
64
+ def convert_options(options, obj)
65
+ options.each do |key, value|
66
+ if value.is_a?(Symbol)
67
+ value = obj.send(value)
68
+ elsif value.is_a?(Proc)
69
+ value = value.call(obj)
70
+ end
71
+ options[key] = value
72
+ end
73
+ options
74
+ end
75
+
76
+ end
77
+
78
+
79
+
80
+
81
+
82
+
83
+
84
+
85
+
86
+
87
+
88
+
89
+
@@ -0,0 +1,11 @@
1
+ xml.instruct!
2
+ xml.sitemapindex "xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" do
3
+ @pages.each_with_index do |page, i|
4
+ xml.sitemap do
5
+ xml.loc url_for(:host => request.host, :page => i + 1, :format => :xml)
6
+ if page.last_mod
7
+ xml.lastmod page.last_mod.to_date
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ xml.instruct!
2
+ xml.urlset "xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" do
3
+ @urls.each do |url|
4
+ xml.url do
5
+ xml.loc url.loc
6
+
7
+ if url.last_mod
8
+ xml.lastmod url.last_mod.to_date
9
+ end
10
+
11
+ if url.change_freq
12
+ xml.changefreq url.change_freq
13
+ end
14
+
15
+ if url.priority
16
+ xml.priority url.priority
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ module DynamicSitemaps
2
+ class Page
3
+ def urls
4
+ @urls ||= []
5
+ end
6
+
7
+ def last_mod
8
+ mod = nil
9
+
10
+ urls.each do |url|
11
+ unless url.last_mod.nil?
12
+ if mod.nil? || url.last_mod > mod
13
+ mod = url.last_mod
14
+ end
15
+ end
16
+ end
17
+
18
+ mod
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,11 @@
1
+ module DynamicSitemaps
2
+ class Sitemap
3
+ class << self
4
+ attr_reader :draw_block
5
+
6
+ def draw(&block)
7
+ @draw_block = block
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module DynamicSitemaps
2
+ class Url
3
+ attr_accessor :loc, :last_mod, :change_freq, :priority
4
+
5
+ def initialize(loc, options = {})
6
+ @loc, @last_mod, @change_freq, @priority = loc, options[:last_mod], options[:change_freq], options[:priority]
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ class DynamicSitemapsGenerator < Rails::Generators::NamedBase
2
+ source_root File.expand_path('../templates', __FILE__)
3
+
4
+ def create_initializer
5
+ copy_file "initializer.rb", "config/initializers/#{file_name}.rb"
6
+ end
7
+
8
+ def create_route
9
+ route "match 'sitemap.xml' => 'sitemaps#sitemap'"
10
+ end
11
+ end
@@ -0,0 +1,28 @@
1
+ DynamicSitemaps::Sitemap.draw do
2
+
3
+ # default per_page is 50.000 which is the specified maximum.
4
+ # per_page 10
5
+
6
+ # url root_url, :last_mod => DateTime.now, :change_freq => 'daily', :priority => 1
7
+
8
+ # new_page!
9
+
10
+ # Product.all.each do |product|
11
+ # url product_url(product), :last_mod => product.updated_at, :change_freq => 'monthly', :priority => 0.8
12
+ # end
13
+
14
+ # new_page!
15
+
16
+ # autogenerate :products, :categories,
17
+ # :last_mod => :updated_at,
18
+ # :change_freq => 'monthly',
19
+ # :priority => 0.8
20
+
21
+ # new_page!
22
+
23
+ # autogenerate :users,
24
+ # :last_mod => :updated_at,
25
+ # :change_freq => lambda { |user| user.very_active? ? 'weekly' : 'monthly' },
26
+ # :priority => 0.5
27
+
28
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dynamic_sitemaps
3
+ version: !ruby/object:Gem::Version
4
+ hash: 21
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 1
10
+ version: 1.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Lasse Bunk
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-09-20 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: Dynamic Sitemaps is a plugin for Ruby on Rails that enables you to easily create flexible, dynamic sitemaps.
22
+ email: lassebunk@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/dynamic_sitemaps/page.rb
31
+ - lib/dynamic_sitemaps/sitemap.rb
32
+ - lib/dynamic_sitemaps/url.rb
33
+ - lib/generators/dynamic_sitemaps/dynamic_sitemaps_generator.rb
34
+ - lib/generators/dynamic_sitemaps/templates/initializer.rb
35
+ - app/controllers/sitemaps_controller.rb
36
+ - app/views/sitemaps/index.builder
37
+ - app/views/sitemaps/sitemap.builder
38
+ homepage: http://github.com/lassebunk/dynamic_sitemaps
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ hash: 3
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.8.10
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Dynamic sitemap generation plugin for Ruby on Rails.
71
+ test_files: []
72
+