Permalink
Browse files

added named_scope to fetch pages for sitemap; specced page model and …

…sitemap controller
  • Loading branch information...
1 parent 4f4ce94 commit e284a8877dff932ad256d1b7c54dfdf13b54720a @cristi cristi committed May 25, 2010
@@ -5,9 +5,7 @@ def sitemap
respond_to do |format|
format.xml {
@host = Radiant::Config['sitemap_xml_domain'] || "#{request.protocol}#{request.host_with_port}"
- @pages = Page.find(:all,
- :conditions => {:sitemap => true, :status_id => 100, :virtual => false},
- :order => "updated_at DESC")
+ @pages = Page.for_sitemap_xml
render :layout => false
}
end
@@ -0,0 +1,10 @@
+module SitemapXml
+ module PageExtensions
+
+ def self.included(base)
+ base.class_eval do
+ named_scope :for_sitemap_xml, :conditions => { :sitemap => true, :status_id => 100, :virtual => false }, :order => "updated_at DESC"
+ end
+ end
+ end
+end
View
@@ -31,7 +31,8 @@ class SitemapXmlExtension < Radiant::Extension
["1.0","1.0"]
]
- def activate
+ def activate
+ Page.send(:include, SitemapXml::PageExtensions)
admin.page.edit.add :extended_metadata, 'admin/pages/sitemap_xml_form'
end
@@ -0,0 +1,29 @@
+require 'spec_helper'
+
+describe SitemapXmlController do
+ describe "handling GET sitemap" do
+ before(:each) do
+ @pages = (1..3).map { mock_model(Page) }
+ end
+
+ def do_get
+ get :sitemap
+ end
+
+ it "assigns the host for the view" do
+ do_get
+ assigns[:host].should == "http://test.host"
+ end
+
+ it "finds the pages to be shown in sitemap and assigns them for the view" do
+ Page.should_receive(:for_sitemap_xml).and_return(@pages)
+ do_get
+ assigns[:pages].should == @pages
+ end
+
+ it "renders the sitemap template" do
+ do_get
+ response.should render_template(:sitemap)
+ end
+ end
+end
View
@@ -0,0 +1,43 @@
+require 'spec_helper'
+
+describe Page do
+ describe "for_sitemap_xml named_scope" do
+ before(:each) do
+ @visible_page = Factory.create(:page_visible_in_sitemap)
+ end
+
+ it "fetches the visible page (sitemap attribute set to true)" do
+ Page.for_sitemap_xml.should == [@visible_page]
+ end
+
+ it "does not fetch pages with false sitemap attribute" do
+ page_not_visible = Factory.create(:page_not_visible_in_sitemap)
+ Page.for_sitemap_xml.should == [@visible_page]
+ end
+
+ it "does not fetch draft pages" do
+ page_not_visible = Factory.create(:draft_page)
+ Page.for_sitemap_xml.should == [@visible_page]
+ end
+
+ it "does not fetch scheduled pages" do
+ page_not_visible = Factory.create(:scheduled_page)
+ Page.for_sitemap_xml.should == [@visible_page]
+ end
+
+ it "does not fetch hidden pages" do
+ page_not_visible = Factory.create(:hidden_page)
+ Page.for_sitemap_xml.should == [@visible_page]
+ end
+
+ it "does not fetch reviewed pages" do
+ page_not_visible = Factory.create(:reviewed_page)
+ Page.for_sitemap_xml.should == [@visible_page]
+ end
+
+ it "does not fetch virtual pages" do
+ page_not_visible = Factory.create(:virtual_page)
+ Page.for_sitemap_xml.should == [@visible_page]
+ end
+ end
+end
View
@@ -11,13 +11,17 @@
end
require "#{RADIANT_ROOT}/spec/spec_helper"
-Dataset::Resolver.default << (File.dirname(__FILE__) + "/datasets")
+require 'factory_girl'
+Dir[File.expand_path(File.join(File.dirname(__FILE__), '/../features/support/factories', '*.rb'))].each {|f| require f}
+
+require "database_cleaner"
if File.directory?(File.dirname(__FILE__) + "/matchers")
Dir[File.dirname(__FILE__) + "/matchers/*.rb"].each {|file| require file }
end
Spec::Runner.configure do |config|
+ DatabaseCleaner.clean_with(:truncation)
# config.use_transactional_fixtures = true
# config.use_instantiated_fixtures = false
# config.fixture_path = RAILS_ROOT + '/spec/fixtures'

0 comments on commit e284a88

Please sign in to comment.