Skip to content

Commit

Permalink
Add support for lambdas when specifying lastmod
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrabarts committed Sep 7, 2009
1 parent 7f99fd9 commit 8463d4d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
7 changes: 4 additions & 3 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,14 @@ To ping search engines, call <code>ping_search_engines</code> after you generate
sitemap.generate
sitemap.ping_search_engines

=== Change Frequency and Priority
=== Change Frequency, Priority and Last Modified

You can control "changefreq" and "priority" values for each record individually by passing lambdas instead of fixed values:
You can control "changefreq", "priority" and "lastmod" values for each record individually by passing lambdas instead of fixed values:

sitemap.add(Posts,
:change_frequency => lambda {|post| ... },
:priority => lambda {|post| ... }
:priority => lambda {|post| ... },
:last_modified => lambda {|post| ... }
)

=== Find Methods
Expand Down
9 changes: 7 additions & 2 deletions lib/big_sitemap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,13 @@ def generate
find_options.update(:limit => limit, :offset => offset) if num_batches > 1

model.send(find_method, find_options).each do |record|
last_mod_method = pick_method(record, TIMESTAMP_METHODS)
last_mod = last_mod_method.nil? ? Time.now : record.send(last_mod_method)
last_mod = options[:last_modified]
if last_mod.is_a?(Proc)
last_mod = last_mod.call(record)
elsif last_mod.nil?
last_mod_method = pick_method(record, TIMESTAMP_METHODS)
last_mod = last_mod_method.nil? ? Time.now : record.send(last_mod_method)
end

param_method = pick_method(record, PARAM_METHODS)

Expand Down
5 changes: 5 additions & 0 deletions test/big_sitemap_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ def teardown
assert_equal TestModel.new.priority.to_s, elements(first_sitemaps_model_file, 'priority').first.text
end

should 'be able to use a lambda to specify lastmod' do
generate_one_sitemap_model_file(:last_modified => lambda {|m| m.updated_at})
assert_equal TestModel.new.updated_at.utc.strftime('%Y-%m-%dT%H:%M:%S+00:00'), elements(first_sitemaps_model_file, 'lastmod').first.text
end

should 'contain two loc element' do
generate_two_model_sitemap_files
assert_equal 2, num_elements(first_sitemaps_model_file, 'loc')
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/test_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def priority
0.8
end

def updated_at
Time.at(1000000000)
end

class << self
def count_for_sitemap
self.find_for_sitemap.size
Expand Down

0 comments on commit 8463d4d

Please sign in to comment.