Permalink
Browse files

fixed bugs: quote only string values and get correct starting value

  • Loading branch information...
1 parent 2d3a6c0 commit 17cc976e7c287c682a9be164da96aaf7adc2c717 Tobias Bielohlawek committed Jan 25, 2011
Showing with 54 additions and 6 deletions.
  1. +10 −6 lib/big_sitemap.rb
  2. +44 −0 test/big_sitemap_test.rb
View
16 lib/big_sitemap.rb
@@ -161,8 +161,8 @@ def generate_models
if last_id && primary_column
find_options.update(:limit => limit, :offset => nil)
- primary_column_value = last_id.to_s.gsub("'", %q(\\\')) #escape '
- find_options.update(:conditions => [find_options[:conditions], "(#{primary_column} > '#{primary_column_value}')"].compact.join(' AND '))
+ primary_column_value = escape_if_string last_id #escape '
+ find_options.update(:conditions => [find_options[:conditions], "(#{primary_column} > #{primary_column_value})"].compact.join(' AND '))
end
model.send(find_method, find_options).each do |record|
@@ -238,7 +238,7 @@ def ping_search_engines
"appid=#{@options[:yahoo_app_id]}&url=#{sitemap_uri}"
)
else
- $stderr.puts 'unable to ping Yahoo: no ":yahoo_app_id" provided'
+ STDERR.puts 'unable to ping Yahoo: no ":yahoo_app_id" provided'
end
end
@@ -267,9 +267,9 @@ def root_url
def prepare_update
@files_to_move = []
@sources.each do |model, options|
- if options[:partial_update] && primary_column = options[:primary_column] && last_id = get_last_id(options[:filename])
- primary_column_value = last_id.to_s.gsub("'", %q(\\\')) #escape '
- options[:conditions] = [options[:conditions], "(#{primary_column} >= '#{primary_column_value}')"].compact.join(' AND ')
+ if options[:partial_update] && (primary_column = options[:primary_column]) && (last_id = get_last_id(options[:filename]))
+ primary_column_value = escape_if_string last_id #escape '
+ options[:conditions] = [options[:conditions], "(#{primary_column} >= #{primary_column_value})"].compact.join(' AND ')
options[:start_part_id] = last_id
end
end
@@ -328,6 +328,10 @@ def pick_method(model, candidates)
method
end
+ def escape_if_string(value)
+ (value.to_i.to_s == value.to_s) ? value.to_i : "'#{value.gsub("'", %q(\\\'))}'"
+ end
+
def url_for_sitemap(path)
[root_url, @options[:path], File.basename(path)].compact.join('/')
end
View
44 test/big_sitemap_test.rb
@@ -287,6 +287,32 @@ def teardown
end
context 'partial update' do
+
+ context 'prepare_update' do
+ should 'generate correct condition for partial update' do
+ filename = "#{sitemaps_dir}/sitemap_test_models"
+
+ create_sitemap(:partial_update => true).clean
+ add_model(:num_items => 50) #TestModel
+
+ File.open("#{filename}_23.xml", 'w')
+ assert_equal "(id >= 23)", @sitemap.send(:prepare_update).first.last[:conditions]
+
+ File.open("#{filename}_42.xml", 'w')
+ assert_equal "(id >= 23) AND (id >= 42)", @sitemap.send(:prepare_update).first.last[:conditions]
+ end
+
+ should 'generate correct condition for partial update with custom column' do
+ filename = "#{sitemaps_dir}/sitemap_test_models"
+
+ create_sitemap(:partial_update => true).clean
+ add_model(:num_items => 50, :primary_column => 'name') #TestModel
+
+ File.open("#{filename}_666.xml", 'w')
+ assert_equal "(name >= 666)", @sitemap.send(:prepare_update).first.last[:conditions]
+ end
+ end
+
should 'generate for all xml files in directory and delete last file' do
TestModel.current_id = last_id = 27
filename = "#{sitemaps_dir}/sitemap_test_models"
@@ -354,6 +380,24 @@ def teardown
assert_equal 3, elements("#{filename}_46.xml", 'loc').size
end
+ context 'escape' do
+ should 'add if not number' do
+ create_sitemap
+ data = {
+ 42 => 42,
+ '23' => 23,
+ "test" => "'test'",
+ "test10" => "'test10'",
+ "10test" => "'10test'",
+ "10t' est" => "'10t\\' est'",
+ }
+ data.each do |key, value|
+ assert_equal value, @sitemap.send(:escape_if_string, key)
+ end
+
+ end
+ end
+
context 'lockfile' do
should 'create and delete lock file' do
sitemap = BigSitemap.new(:base_url => 'http://example.com', :document_root => tmp_dir)

0 comments on commit 17cc976

Please sign in to comment.