Navigation Menu

Skip to content

Commit

Permalink
introduced update method which generated and adds index for only new …
Browse files Browse the repository at this point in the history
…items...
  • Loading branch information
Tobias Bielohlawek committed Dec 20, 2010
1 parent be82731 commit 0ffacfb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
41 changes: 30 additions & 11 deletions lib/big_sitemap.rb
Expand Up @@ -75,7 +75,8 @@ def initialize(options)
end

def add(model, options={})
options[:path] ||= table_name(model)
options[:path] ||= table_name(model)
options[:filename] ||= file_name(model)
@sources << [model, options.dup]
return self
end
Expand All @@ -94,16 +95,31 @@ def table_name(model)
end
end

def file_name(name)
name = table_name(name) unless name.is_a? String
"#{@file_path}/sitemap_#{name}#{'_kml' if @options[:geo]}"
end

def clean
Dir["#{@file_path}/sitemap_*.{xml,xml.gz}"].each do |file|
FileUtils.rm file
end
return self
end

def update
@sources.each do |model, options|
#next unless options[:partial_update]
if last_id = get_last_id(options[:filename])
@sources[model][:conditions] = [options[:conditions], "(id > #{last_id})"].compact.join(' AND ')
end
end
generate
end

def generate
for model, options in @sources
with_sitemap(table_name(model)) do |sitemap|
with_sitemap(model, options) do |sitemap|
count_method = pick_method(model, COUNT_METHODS)
find_method = pick_method(model, FIND_METHODS)
raise ArgumentError, "#{model} must provide a count_for_sitemap class method" if count_method.nil?
Expand Down Expand Up @@ -155,7 +171,8 @@ def generate
priority = options[:priority]
pri = priority.is_a?(Proc) ? priority.call(record) : priority

sitemap.add_url!(location, last_mod, freq, pri)
id = record.respond_to?(:id) ? record.id : nil
sitemap.add_url!(location, last_mod, freq, pri, id)
end
end
end
Expand Down Expand Up @@ -222,14 +239,11 @@ def root_url
private

def with_sitemap(name, options={})
options[:filename] = "#{@file_path}/sitemap_#{name}"
options[:filename] += "_kml" if @options[:geo]
options[:geo] = true if @options[:geo]
options[:max_urls] = @options[:max_per_sitemap]

unless options[:gzip] = @options[:gzip]
options[:indent] = 2
end
options[:filename] ||= file_name(name)
options[:geo] ||= @options[:geo]
options[:max_urls] ||= @options[:max_per_sitemap]
options[:gzip] ||= @options[:gzip]
options[:indent] = options[:gzip] ? 0 : 2

sitemap = Builder.new(options)

Expand All @@ -245,6 +259,11 @@ def strip_leading_slash(str)
str.sub(/^\//, '')
end

def get_last_id(filename)
last_file = Dir["#{filename}*.{xml,xml.gz}"].sort.last
last_file.to_s.scan(/#{filename}(.+).xml/).flatten.last
end

def pick_method(model, candidates)
method = nil
candidates.each do |candidate|
Expand Down
3 changes: 2 additions & 1 deletion lib/big_sitemap/builder.rb
Expand Up @@ -32,7 +32,8 @@ def geo?
!index? && @geo == true
end

def add_url!(url, time = nil, frequency = nil, priority = nil)
def add_url!(url, time = nil, frequency = nil, priority = nil, part_nr = nil)
@parts = part_nr if part_nr && @custom_part_nr
_rotate if @max_urls == @urls

tag!(index? ? 'sitemap' : 'url') do
Expand Down
11 changes: 11 additions & 0 deletions test/big_sitemap_test.rb
Expand Up @@ -246,6 +246,17 @@ def teardown
end
end

context 'partial update' do
should 'generate for all xml files in directory' do
create_sitemap
filename = "#{sitemaps_dir}/sitemap_file"
@sitemap.clean
File.open("#{filename}112312312.xml", 'w')
File.open("#{filename}223423423.xml.gz", 'w')
assert_equal "223423423", @sitemap.send(:get_last_id, filename)
end
end

private
def delete_tmp_files
FileUtils.rm_rf(sitemaps_dir)
Expand Down

0 comments on commit 0ffacfb

Please sign in to comment.