Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Adding support for static and dynamic collections #33

Open
wants to merge 1 commit into from

2 participants

@wesg

Adding support for static and dynamic collections. (Dynamic collections can call a method to get a URL from each object in the collection - keeps things DRY)

Background - I've been using Big_sitemap for generating a sitemap dynamically, passing in collections returned by scope, and then calling a method to get the URL for that object instance. Different models have slightly different methods to call for vanity URLs (and some classes have multiple methods for different URLs).

Building a sitemap this way using blocks in the BigSitemap.generate() block was getting a bit messy so I reopened the BigSitemap class and added a couple of methods to DRY things up ........ submitting the pull request as I think other may find it useful - I'm prepared to tweak, incorporate feedback and try a second explanation in the README if it's not clear enough.

Regards,
Wesley
@wesg

@wesg wesg Adding support for static and dynamic collections. (Dynamic collectio…
…ns can call a method to get a URL from each object in the collection - keeps things DRY).
dd8e503
@wesg

Hi @alexrabarts - any feedback on this commit?

@alexrabarts
Owner

Hi @wesg, sorry for the late response - this project has taken somewhat of a backseat to massive_sitemap. I like the idea of being able to handle static collections. It might make sense to include this into the add method, rather than adding a new method.

I was trying to remove logic that required passing method names around - hence the deprecation of the find_method option and moving to the block syntax. I'm not hugely keen on adding this back in. Also, I'm guessing most people won't have a method on their models that return the record's URL.

@wesg

Thanks for the feedback @alexrabarts - hadn't seen massive_sitemap. I might rework my pull request according to your feedback, resubmit and then see if there's anything useful from it that might be of benefit in massive_sitemap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 8, 2012
  1. @wesg

    Adding support for static and dynamic collections. (Dynamic collectio…

    wesg authored
    …ns can call a method to get a URL from each object in the collection - keeps things DRY).
This page is out of date. Refresh to see the latest.
View
7 README.rdoc
@@ -21,6 +21,13 @@ BigSitemap is best run periodically through a Rake/Thor task.
Product.find(:all).each do |product|
add product_path(product), :change_frequency => 'daily', :priority => 0.5
end
+
+ # Add a dynamic collection (using a scope) and calling a method for (vanity) URL to add
+ add_collection Product.live_products, 'method_to_call_for_url'
+
+ # Add a static collection of pages - useful for batching similar pages together and keeping things DRY and managable
+ static_pages_for_sitemap = %w(/terms_and_conditions /privacy_policy /contact_us)
+ add_static_collection static_pages_for_sitemap, :last_modified => Time.now, :change_frequency => 'weekly', :priority => 0.5
end
The code above will create a minimum of two files:
View
12 lib/big_sitemap.rb
@@ -47,6 +47,18 @@ def first_id_of_last_sitemap
def add(path, options={})
@sitemap.add_path(path, options)
end
+
+ def add_collection(collection, url_method='url_for_sitemap', options={})
+ collection.each do |member|
+ add member.send(url_method), options
+ end
+ end
+
+ def add_static_collection(collection, options={})
+ collection.each do |member|
+ add member, options
+ end
+ end
end
def initialize(options={})
View
21 test/big_sitemap_test.rb
@@ -52,6 +52,27 @@ def teardown
assert_equal 'http://example.com/navigation/about/us', elems.last.text
end
+ should 'add to dynamic collection to sitemap' do
+ generate_sitemap do
+ add_collection TestModel.find_for_sitemap(:limit => 3), 'url_for_sitemap', {:last_modified => Time.now, :change_frequency => 'weekly', :priority => 0.5}
+ end
+
+ elems = elements first_sitemap_file, 'loc'
+ assert_equal 3, elems.size
+ end
+
+ should 'add to static collection to sitemap' do
+ generate_sitemap do
+ static_pages_for_sitemap = %w(/terms_and_conditions /privacy_policy /contact_us)
+ add_static_collection static_pages_for_sitemap, {:last_modified => Time.now, :change_frequency => 'weekly', :priority => 0.5}
+ end
+
+ elems = elements first_sitemap_file, 'loc'
+ assert_equal 3, elems.size
+ assert_equal 'http://example.com/terms_and_conditions', elems.first.text
+ assert_equal 'http://example.com/contact_us', elems.last.text
+ end
+
context 'Sitemap index file' do
should 'contain one sitemapindex element' do
generate_sitemap { add '/' }
View
4 test/fixtures/test_model.rb
@@ -19,6 +19,10 @@ def updated_at
Time.at(1000000000)
end
+ def url_for_sitemap
+ '/test_model/' + id.to_s
+ end
+
class << self
def table_name
'test_models'
View
2  test/test_helper.rb
@@ -4,7 +4,7 @@
require 'test/unit'
require 'shoulda'
require 'mocha'
-require 'test/fixtures/test_model'
+require File.expand_path('fixtures/test_model')
require 'big_sitemap'
Something went wrong with that request. Please try again.