Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

sitemap generator

  • Loading branch information...
commit a2b0b58072cb2fd6616ca5f63860bcfe3b5ae236 1 parent db7b1da
@fguillen authored
View
5 app/controllers/sitemaps_controller.rb
@@ -0,0 +1,5 @@
+class SitemapsController < ApplicationController
+ def show
+ render :text => SitemapGenerator.do, :layout => false, :content_type => "text/xml"
+ end
+end
View
1  config/environment.rb
@@ -27,6 +27,7 @@
config.gem 'will_paginate', :version => '~> 2.3.11', :source => 'http://gemcutter.org'
config.gem 'pdf-writer', :lib => 'pdf/writer'
config.gem 'bluecloth'
+ config.gem 'factory_girl', :source => 'http://gemcutter.org', :version => '1.2.4'
# config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
# config.gem "sqlite3-ruby", :lib => "sqlite3"
View
3  config/environments/test.rb
@@ -20,6 +20,3 @@
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test
-
-
-require 'factory_girl'
View
2  config/routes.rb
@@ -47,6 +47,8 @@
map.resources :location_searchs, :only => :create
map.resource :avaliable_places_counter, :only => :show
map.resources :static_pages
+
+ map.resource :sitemap, :only => :show
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
View
88 lib/sitemap_generator.rb
@@ -0,0 +1,88 @@
+class SitemapGenerator
+ def self.do
+ host = "http://#{APP_CONFIG[:site_domain]}"
+ result = ""
+
+ xml = Builder::XmlMarkup.new( :target => result, :indent => 2 )
+ xml.instruct!
+ xml.urlset "xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" do
+
+ xml.comment! 'root'
+ xml.url do
+ xml.loc host
+ xml.lastmod Paper.visible.first(:order => 'updated_at desc').updated_at.to_date
+ end
+
+ xml.comment! 'calendar'
+ xml.url do
+ xml.loc "#{host}/calendar"
+ xml.lastmod Paper.visible.first(:order => 'updated_at desc').updated_at.to_date
+ end
+
+ xml.comment! 'talks'
+ xml.comment! 'talks:index'
+ xml.url do
+ xml.loc "#{host}/talks"
+ xml.lastmod Paper.visible.first(:order => 'updated_at desc').updated_at.to_date
+ end
+
+ xml.comment! 'talks:each'
+ Paper.visible.not_break.date_ordered.each do |paper|
+ xml.url do
+ xml.loc "#{host}/talks/#{paper.to_param}"
+ xml.lastmod paper.updated_at.to_date
+ end
+ end
+
+ xml.comment! 'people'
+ xml.comment! 'people:all'
+ xml.comment! 'people:all:index'
+ (1..((User.ordered.activated.public_profile.count/User.per_page.to_f).ceil)).each do |page|
+ xml.url do
+ xml.loc "#{host}/people?page=#{page}"
+ xml.lastmod User.ordered.activated.public_profile.first(:order => 'updated_at desc').updated_at.to_date
+ end
+ end
+
+ xml.comment! 'people:all:each'
+ User.ordered.activated.public_profile.each do |user|
+ xml.url do
+ xml.loc "#{host}/people/#{user.to_param}"
+ xml.lastmod user.updated_at.to_date
+ end
+ end
+
+ xml.comment! 'people:attendees'
+ xml.comment! 'people:attendees:index'
+ if Event.count > 0
+ (1..((User.ordered.public_profile.has_paid( 1 ).count/User.per_page.to_f).ceil)).each do |page|
+ xml.url do
+ xml.loc "#{host}/people?event_id=1&page=#{page}&search=event_attendees"
+ xml.lastmod User.ordered.public_profile.has_paid( 1 ).first(:order => 'updated_at desc').updated_at.to_date
+ end
+ end
+ end
+
+ xml.comment! 'people:speakers'
+ xml.comment! 'people:speakers:index'
+ (1..((User.ordered.public_speaker.count/User.per_page.to_f).ceil)).each do |page|
+ xml.url do
+ xml.loc "#{host}/people?page=#{page}&search=speakers"
+ xml.lastmod User.ordered.public_speaker.first(:order => 'updated_at desc').updated_at.to_date
+ end
+ end
+
+
+ xml.comment! 'static_pages'
+ StaticPage.all.each do |static_page|
+ xml.url do
+ xml.loc "#{host}/static_pages/#{static_page.permalink}"
+ xml.lastmod static_page.updated_at.to_date
+ end
+ end
+ end
+
+
+ return result
+ end
+end
View
6 lib/tasks/sitemap.rake
@@ -0,0 +1,6 @@
+namespace :sitemap do
+ desc "Craete and return the sitemap.xml"
+ task :do => :environment do
+ puts SitemapGenerator.do
+ end
+end
View
26 test/factories.rb
@@ -11,7 +11,12 @@
Factory.define :cart do |f|
f.association :user, :factory => :user
- f.status Cart::STATUS[:ON_SESSION] # no puedo hacer esto: Post::STATUS_PUBLISHED
+ f.status Cart::STATUS[:ON_SESSION]
+end
+
+Factory.define :carts_event do |f|
+ f.association :cart
+ f.association :event
end
Factory.define :event do |f|
@@ -29,3 +34,22 @@
f.role User::ROLE[:USER]
f.public_profile true
end
+
+Factory.define :paper do |f|
+ f.sequence(:title) { |n| "Paper Title #{n}" }
+ f.sequence(:description) { |n| "Paper Description #{n}" }
+ f.family Paper::FAMILY[:TUTORIAL]
+ f.status Paper::STATUS[:CONFIRMED]
+ f.association :creator, :factory => :user
+end
+
+Factory.define :static_page do |f|
+ f.sequence(:title) { |n| "Static Page #{n}" }
+ f.sequence(:permalink) { |n| "static_page_#{n}" }
+ f.content 'Static Page Content'
+end
+
+Factory.define :speaker do |f|
+ f.association :paper
+ f.association :user
+end
View
65 test/fixtures/sitemap.xml
@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
+ <!-- root -->
+ <url>
+ <loc>http://127.0.0.1:3000</loc>
+ <lastmod>2010-08-09</lastmod>
+ </url>
+ <!-- calendar -->
+ <url>
+ <loc>http://127.0.0.1:3000/calendar</loc>
+ <lastmod>2010-08-09</lastmod>
+ </url>
+ <!-- talks -->
+ <!-- talks:index -->
+ <url>
+ <loc>http://127.0.0.1:3000/talks</loc>
+ <lastmod>2010-08-09</lastmod>
+ </url>
+ <!-- talks:each -->
+ <url>
+ <loc>http://127.0.0.1:3000/talks/1-paper-1</loc>
+ <lastmod>2010-08-09</lastmod>
+ </url>
+ <url>
+ <loc>http://127.0.0.1:3000/talks/2-paper-2</loc>
+ <lastmod>2010-08-09</lastmod>
+ </url>
+ <!-- people -->
+ <!-- people:all -->
+ <!-- people:all:index -->
+ <url>
+ <loc>http://127.0.0.1:3000/people?page=1</loc>
+ <lastmod>2010-08-09</lastmod>
+ </url>
+ <url>
+ <loc>http://127.0.0.1:3000/people?page=2</loc>
+ <lastmod>2010-08-09</lastmod>
+ </url>
+ <!-- people:all:each -->
+ <url>
+ <loc>http://127.0.0.1:3000/people/1-user-1</loc>
+ <lastmod>2010-08-09</lastmod>
+ </url>
+ <url>
+ <loc>http://127.0.0.1:3000/people/2-user-2</loc>
+ <lastmod>2010-08-09</lastmod>
+ </url>
+ <!-- people:attendees -->
+ <!-- people:attendees:index -->
+ <url>
+ <loc>http://127.0.0.1:3000/people?event_id=1&amp;page=1&amp;search=event_attendees</loc>
+ <lastmod>2010-08-09</lastmod>
+ </url>
+ <!-- people:speakers -->
+ <!-- people:speakers:index -->
+ <url>
+ <loc>http://127.0.0.1:3000/people?page=1&amp;search=speakers</loc>
+ <lastmod>2010-08-09</lastmod>
+ </url>
+ <!-- static_pages -->
+ <url>
+ <loc>http://127.0.0.1:3000/static_pages/static_page</loc>
+ <lastmod>2010-08-09</lastmod>
+ </url>
+</urlset>
View
13 test/functional/sitemaps_controller_test.rb
@@ -0,0 +1,13 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class SitemapsControllerTest < ActionController::TestCase
+ def test_show
+ SitemapGenerator.expects(:do).returns( 'wadus sitemap' )
+
+ get( :show )
+
+ assert_response :success
+ assert_equal( 'wadus sitemap', @response.body )
+ assert_equal( 'text/xml', @response.content_type )
+ end
+end
View
31 test/unit/sitemap_generator_test.rb
@@ -0,0 +1,31 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class SitemapGeneratorTest < ActiveSupport::TestCase
+ def test_do
+ Paper.delete_all
+ User.delete_all
+ StaticPage.delete_all
+
+ User.stubs(:per_page).returns(1)
+
+ user_1 = Factory(:user, :public_profile => true, :activated_at => Time.parse( '2010-01-01' ), :id => 1, :name => 'User 1')
+ user_2 = Factory(:user, :public_profile => true, :activated_at => Time.parse( '2010-01-01' ), :id => 2, :name => 'User 2')
+
+ event = Factory(:event, :id => 1)
+ cart = Factory(:cart, :user => user_1, :status => Cart::STATUS[:COMPLETED] )
+ carts_events = Factory(:carts_event, :cart => cart, :event => event )
+
+ paper_1 = Factory( :paper, :title => 'paper 1', :status => Paper::STATUS[:CONFIRMED], :family => Paper::FAMILY[:TUTORIAL], :creator => user_1, :id => 1 )
+ paper_2 = Factory( :paper, :title => 'paper 2', :status => Paper::STATUS[:CONFIRMED], :family => Paper::FAMILY[:TUTORIAL], :creator => user_1, :id => 2 )
+
+ speaker = Factory( :speaker, :user => user_1, :paper => paper_1 )
+
+ static_page_1 = Factory( :static_page, :title => 'static page', :permalink => 'static_page' )
+
+ # File.open( "#{RAILS_ROOT}/test/fixtures/sitemap.xml", 'w' ) do |f|
+ # f.write SitemapGenerator.do
+ # end
+
+ assert_equal( File.read( "#{RAILS_ROOT}/test/fixtures/sitemap.xml" ), SitemapGenerator.do )
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.