Skip to content
Browse files

add lashmod by updated_at

  • Loading branch information...
1 parent 87d8069 commit 3c3efdbb27d0961211746791ffa0731be2518e0c @flyerhzm committed
Showing with 18 additions and 14 deletions.
  1. +0 −10 lib/hack/resources.rb
  2. +2 −2 lib/sitemap.rb
  3. +16 −2 spec/sitemap_spec.rb
View
10 lib/hack/resources.rb
@@ -1,10 +0,0 @@
-module ActionController
- module Resources
- alias_method :origin_action_options_for, :action_options_for
-
- def action_options_for(action, resource, method = nil, resource_options = {})
- options = origin_action_options_for(action, resource, method, resource_options)
- resource.options.reject {|k, v| !['priority', 'changefreq'].include? k.to_s}.merge(options)
- end
- end
-end
View
4 lib/sitemap.rb
@@ -60,7 +60,7 @@ def to_xml(target)
xml.url do
xml.loc(@@host + result[:location])
xml.priority result[:priority]
- xml.lastmod now
+ xml.lastmod (result[:lastmod] || Time.now).strftime("%Y/%m/%d")
end
end
end
@@ -95,7 +95,7 @@ def parse_path_without_substitution(path, options, prefix, parent)
if items.size > 3
parse_path_without_substitution('/' + items[3..-1].join('/'), options, "#{prefix}/#{items[1]}/#{obj.to_param}", obj)
else
- add_result :location => "#{prefix}/#{items[1]}/#{obj.to_param}", :changefreq => options[:changefreq], :priority => options[:priority] || @@priority
+ add_result :location => "#{prefix}/#{items[1]}/#{obj.to_param}", :changefreq => options[:changefreq], :priority => options[:priority] || @@priority, :lastmod => (obj.respond_to?(:updated_at) ? obj.updated_at : nil)
end
end
return nil
View
18 spec/sitemap_spec.rb
@@ -39,8 +39,10 @@ def to_param
end
before(:each) do
- @post1 = Post.new(1, 'post1', DateTime.new(2009, 8, 9, 0, 0, 0))
- @post2 = Post.new(2, 'post2', DateTime.new(2009, 8, 10, 0, 0, 0))
+ @datetime1 = DateTime.new(2009, 8, 9, 0, 0, 0)
+ @datetime2 = DateTime.new(2009, 8, 10, 0, 0, 0)
+ @post1 = Post.new(1, 'post1', @datetime1)
+ @post2 = Post.new(2, 'post2', @datetime2)
Post.stubs(:all).returns([@post1, @post2])
end
@@ -192,4 +194,16 @@ def to_param
end
end
+ context "lastmod" do
+ it "should get lastmod by resource" do
+ now = Time.now
+ Time.stubs(:now).returns(now)
+ Sitemap::Routes.draw do |map|
+ map.resources :posts
+ end
+ Sitemap::Routes.parse
+ Sitemap::Routes.results.collect {|result| result[:lastmod]}.should == [nil, @datetime1, @datetime2]
+ end
+ end
+
end

0 comments on commit 3c3efdb

Please sign in to comment.
Something went wrong with that request. Please try again.