Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

MongoID support #14

Closed
wants to merge 1 commit into from

2 participants

Sébastien Gruhier Alex Rabarts
Sébastien Gruhier

Hi
I added mongo ID support to big_sitemap, if you want to merge
Thanks
Seb

Alex Rabarts
Owner

Thanks for submitting this patch. Responsibility for finding records has now been removed from BigSitemap, as of version 1.0.0, so I'll close this request. Please see the README for the new syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

May 27, 2011
Sébastien Gruhier sgruhier quick mopngoid support 6ce9af7
This page is out of date. Refresh to see the latest.

Showing 2 changed files with 39 additions and 13 deletions. Show diff stats Hide diff stats

  1. +37 11 lib/big_sitemap.rb
  2. +2 2 lib/big_sitemap/builder.rb
48 lib/big_sitemap.rb
@@ -118,6 +118,18 @@ def generate
118 118 self
119 119 end
120 120
  121 + def init_find_options(options)
  122 + find_options = {}
  123 + [:conditions, :limit, :joins, :select, :order, :include, :group].each do |key|
  124 + find_options[key] = options.delete(key)
  125 + end
  126 + find_options
  127 + end
  128 +
  129 + def count_options(primary_column)
  130 + {:select => (primary_column || '*'), :include => nil}
  131 + end
  132 +
121 133 def generate_models
122 134 for model, options in @sources
123 135 with_sitemap(model, options.dup) do |sitemap|
@@ -127,18 +139,13 @@ def generate_models
127 139 raise ArgumentError, "#{model} must provide a count_for_sitemap class method" if count_method.nil?
128 140 raise ArgumentError, "#{model} must provide a find_for_sitemap class method" if find_method.nil?
129 141
130   - find_options = {}
131   - [:conditions, :limit, :joins, :select, :order, :include, :group].each do |key|
132   - find_options[key] = options.delete(key)
133   - end
  142 + find_options = init_find_options(options)
134 143
135 144 primary_column = options.delete(:primary_column)
136   -
137   - count = model.send(count_method, find_options.merge(:select => (primary_column || '*'), :include => nil))
  145 + count = model.send(count_method, find_options.merge(count_options(primary_column)))
138 146 count = find_options[:limit].to_i if find_options[:limit] && find_options[:limit].to_i < count
139 147 num_sitemaps = 1
140 148 num_batches = 1
141   -
142 149 if count > @options[:batch_size]
143 150 num_batches = (count.to_f / @options[:batch_size].to_f).ceil
144 151 num_sitemaps = (count.to_f / @options[:max_per_sitemap].to_f).ceil
@@ -149,8 +156,8 @@ def generate_models
149 156 # Work out the start and end batch numbers for this sitemap
150 157 batch_num_start = sitemap_num == 1 ? 1 : ((sitemap_num * batches_per_sitemap).ceil - batches_per_sitemap + 1).to_i
151 158 batch_num_end = (batch_num_start + [batches_per_sitemap, num_batches].min).floor - 1
152   -
153 159 for batch_num in batch_num_start..batch_num_end
  160 +
154 161 offset = (batch_num - 1) * @options[:batch_size]
155 162 limit = (count - offset) < @options[:batch_size] ? (count - offset) : @options[:batch_size]
156 163 find_options.update(:limit => limit, :offset => offset) if num_batches > 1
@@ -160,7 +167,6 @@ def generate_models
160 167 primary_column_value = escape_if_string last_id #escape '
161 168 find_options.update(:conditions => [find_options[:conditions], "(#{primary_column} > #{primary_column_value})"].compact.join(' AND '))
162 169 end
163   -
164 170 model.send(find_method, find_options).each do |record|
165 171 last_mod = options[:last_modified]
166 172 if last_mod.is_a?(Proc)
@@ -275,8 +281,7 @@ def with_sitemap(name, options={})
275 281 options[:type] ||= 'sitemap'
276 282 options[:max_urls] ||= @options["max_per_#{options[:type]}".to_sym]
277 283 options[:gzip] ||= @options[:gzip]
278   - options[:indent] = options[:gzip] ? 0 : 2
279   -
  284 + options[:indent] = 2 # options[:gzip] ? 2 : 2 even gzipped I like to have indentation
280 285 sitemap = if options[:type] == 'index'
281 286 IndexBuilder.new(options)
282 287 elsif options[:geo]
@@ -358,3 +363,24 @@ def table_name(model)
358 363 end
359 364
360 365 end
  366 +
  367 +class BigSitemapMongoid < BigSitemapRails
  368 +
  369 + def initialize(options={})
  370 + raise "No Mongoid Environment loaded" unless defined? Mongoid
  371 + super(options)
  372 + end
  373 +
  374 + def table_name(model)
  375 + model.to_s.downcase
  376 + end
  377 +
  378 + def init_find_options(options)
  379 + {}
  380 + end
  381 +
  382 + def count_options(primary_column)
  383 + {}
  384 + end
  385 +end
  386 +
4 lib/big_sitemap/builder.rb
@@ -131,12 +131,12 @@ def _close_document
131 131 end
132 132
133 133 def _indent
134   - return if @gzip
  134 + #return if @gzip even gzipped I like to have indentation
135 135 target!.print " " * @level
136 136 end
137 137
138 138 def _newline
139   - return if @gzip
  139 + #return if @gzip even gzipped I like to have indentation
140 140 target!.puts ''
141 141 end
142 142 end

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.