Permalink
Browse files

Make sure paths are normalized so it doesn't matter whether paths pas…

…sed in as arguments have a leading slash or not
  • Loading branch information...
1 parent d601a39 commit 759297049ab975eb643d2df5d5b3633f5c86aa71 @alexrabarts alexrabarts committed Mar 12, 2009
Showing with 26 additions and 5 deletions.
  1. +6 −2 lib/big_sitemap.rb
  2. +16 −3 test/big_sitemap_test.rb
  3. +4 −0 test/fixtures/test_model.rb
View
8 lib/big_sitemap.rb
@@ -21,7 +21,7 @@ def initialize(options)
@base_url = options.delete(:base_url)
@max_per_sitemap = options.delete(:max_per_sitemap) || 50000
@batch_size = options.delete(:batch_size) || 1001 # TODO: Set this to 1000 once DM offset 37000 bug is fixed
- @web_path = options.delete(:path) || 'sitemaps'
+ @web_path = strip_leading_slash(options.delete(:path) || 'sitemaps')
@ping_google = options[:ping_google].nil? ? true : options.delete(:ping_google)
@ping_yahoo = options[:ping_yahoo].nil? ? true : options.delete(:ping_yahoo)
@yahoo_app_id = options.delete(:yahoo_app_id)
@@ -42,7 +42,7 @@ def initialize(options)
def add(options)
raise ArgumentError, ':model and :path options must be provided' unless options[:model] && options[:path]
- @sources << options
+ @sources << options.update(:path => strip_leading_slash(options[:path]))
self # Chainable
end
@@ -127,6 +127,10 @@ def generate
end
private
+ def strip_leading_slash(str)
+ str.sub(/^\//, '')
+ end
+
def pick_method(klass, candidates)
method = nil
candidates.each do |candidate|
View
19 test/big_sitemap_test.rb
@@ -112,12 +112,21 @@ def teardown
assert_equal 1, num_elements(first_sitemaps_model_file, 'changefreq')
assert_equal 1, num_elements(second_sitemaps_model_file, 'changefreq')
end
+
+ should 'strip leading slashes from controller paths' do
+ create_sitemap
+ @sitemap.add(:model => TestModel, :path => '/test_controller').generate
+ assert(
+ !elements(single_sitemaps_model_file, 'loc').first.text.match(/\/\/test_controller\//),
+ 'URL does not contain a double-slash before the controller path'
+ )
+ end
end
context 'add method' do
should 'be chainable' do
create_sitemap
- assert_equal BigSitemap, @sitemap.add({:model => TestModel, :path => 'test_controller'}).class
+ assert_equal BigSitemap, @sitemap.add(:model => TestModel, :path => 'test_controller').class
end
end
@@ -198,8 +207,12 @@ def ns
{'s' => 'http://www.sitemaps.org/schemas/sitemap/0.9'}
end
- def num_elements(filename, el)
+ def elements(filename, el)
data = Nokogiri::XML.parse(Zlib::GzipReader.open(filename).read)
- data.search("//s:#{el}", ns).size
+ data.search("//s:#{el}", ns)
+ end
+
+ def num_elements(filename, el)
+ elements(filename, el).size
end
end
View
4 test/fixtures/test_model.rb
@@ -8,6 +8,10 @@ def count_for_sitemap
self.find_for_sitemap.size
end
+ def num_items
+ 10
+ end
+
def find_for_sitemap(options={})
instances = []
num_times = options.delete(:limit) || self.num_items

0 comments on commit 7592970

Please sign in to comment.