Module: AegisNet::Sitemapper::ActiveRecord::Builder::SingletonMethods

Defined in:
lib/sitemapper/active_record/builder.rb

Instance Method Summary collapse

Instance Method Details

#build_sitemap(scope, options = {}) ⇒ Object

Adds sitemap building functionality to ActiveRecord models.

The option :filename is derived from the model name and will be set to Rails.root/public/sitemap_modelnames.xml.gz by default. Set :filename to nil or false explicitely if you don’t want the filename to be guessed (ie. if you want output to stdout).

Parameters

  • scope: :all, :first, :last or a named scope

  • options: a Hash with options to pass to ActiveRecord::Base.find and AegisNet::Sitemapper::Generator

Supported Options

  • see AegisNet::Sitemapper::Generator::VALID_GENERATOR_OPTIONS

Example

Content.build_sitemap :all, :file => "sitemap_content.xml" do |content, xml|
  xml.loc content_path(content)
  xml.changefreq "weekly"
  xml.priority 0.5
end

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sitemapper/active_record/builder.rb', line 33

def build_sitemap scope, options = {}
  scope = scope.to_sym
  raise(ArgumentError, "Unknown ActiveRecord finder: #{scope}") unless self.respond_to?(scope)
  valid_find_options = [ :conditions, :include, :joins, :limit, :offset,
                         :order, :select, :group, :having, :from ]
  options = options.symbolize_keys!
  options.assert_valid_keys(Generator::VALID_GENERATOR_OPTIONS, valid_find_options)

  find_options = options.select{|option, value| valid_find_options.include?(option) }
  sitemap_opts = options.delete_if{|k, v| find_options.keys.include?(k)}

  # Extra treatment for the filename option
  sitemap_opts[:file] = sitemap_opts.keys.include?(:file) ? sitemap_opts[:file] : AegisNet::Sitemapper::Generator.default_filename(self.class)

  entries = self.send(scope, find_options).to_a # get an array for :first and :last, too
  AegisNet::Sitemapper::Generator.create(entries, sitemap_opts) { |entry, xml|  yield entry, xml }
end