| CODENOTIFIER | HelpYou are not signed inSign in |
Project: acts_as_ferret
Revision: 352
Author: jk
Date: 09 Jul 2008 05:18:11
Diff at Trac: http://projects.jkraemer.net/acts_as_ferret/changeset/352
Changes:merge and fix bug
Files:| ... | ...@@ -294,52 +294,40 @@ | |
| 294 | 294 | end |
| 295 | 295 | |
| 296 | 296 | def self.find(query, models_or_index_name, options = {}, ar_options = {}) |
| 297 | # TODO generalize local/remote index so we can remove the workaround below | |
| 298 | # (replace logic in class_methods#find_with_ferret) | |
| 299 | # maybe put pagination stuff in a module to be included by all index | |
| 300 | # implementations | |
| 301 | 297 | models = [ models_or_index_name ] if Class === models_or_index_name |
| 302 | # if models && models.size == 1 | |
| 303 | # return models.shift.find_with_ferret query, options, ar_options | |
| 304 | # end | |
| 305 | 298 | index = find_index(models_or_index_name) |
| 306 | 299 | multi = (MultiIndex === index or index.shared?) |
| 307 | if options[:per_page] | |
| 300 | unless options[:per_page] | |
| 301 | options[:limit] ||= ar_options.delete :limit | |
| 302 | options[:offset] ||= ar_options.delete :offset | |
| 303 | end | |
| 304 | if options[:limit] || options[:per_page] | |
| 308 | 305 | # need pagination |
| 309 | options[:page] = options[:page] ? options[:page].to_i : 1 | |
| 310 | limit = options[:per_page] | |
| 311 | offset = (options[:page] - 1) * limit | |
| 306 | options[:page] = if options[:per_page] | |
| 307 | options[:page] ? options[:page].to_i : 1 | |
| 308 | else | |
| 309 | nil | |
| 310 | end | |
| 311 | limit = options[:limit] || options[:per_page] | |
| 312 | offset = options[:offset] || (options[:page] ? (options[:page] - 1) * limit : 0) | |
| 313 | options.delete :offset | |
| 314 | options[:limit] = :all | |
| 312 | 315 | |
| 313 | 316 | if multi or ((ar_options[:conditions] || ar_options[:order]) && options[:sort]) |
| 314 | 317 | # do pagination as the last step after everything has been fetched |
| 315 | 318 | options[:late_pagination] = { :limit => limit, :offset => offset } |
| 316 | options[:offset] = nil | |
| 317 | options[:limit] = :all | |
| 318 | 319 | elsif ar_options[:conditions] or ar_options[:order] |
| 319 | # do pagination right in AR call (faster but only works in this case) | |
| 320 | ar_options[:limit] = limit | |
| 321 | ar_options[:offset] = offset | |
| 322 | options[:limit] = :all | |
| 323 | options.delete :offset | |
| 320 | # late limiting in AR call | |
| 321 | unless limit == :all | |
| 322 | ar_options[:limit] = limit | |
| 323 | ar_options[:offset] = offset | |
| 324 | end | |
| 324 | 325 | else |
| 325 | 326 | options[:limit] = limit |
| 326 | 327 | options[:offset] = offset |
| 327 | 328 | end |
| 328 | elsif ar_options[:conditions] | |
| 329 | if multi | |
| 330 | # multisearch does not use ar_options limit and offset directly but applies them later | |
| 331 | options[:limit] ||= ar_options.delete(:limit) | |
| 332 | options[:offset] ||= ar_options.delete(:offset) | |
| 333 | else | |
| 334 | # let the db do the limiting and offsetting for single-table searches | |
| 335 | unless options[:limit] == :all | |
| 336 | ar_options[:limit] ||= options.delete(:limit) | |
| 337 | end | |
| 338 | ar_options[:offset] ||= options.delete(:offset) | |
| 339 | options[:limit] = :all | |
| 340 | end | |
| 341 | 329 | end |
| 342 | ActsAsFerret::logger.debug "$$$$$$$$$ options: #{options.inspect}\nar_options: #{ar_options.inspect}" | |
| 330 | ActsAsFerret::logger.debug "options: #{options.inspect}\nar_options: #{ar_options.inspect}" | |
| 343 | 331 | total_hits, result = index.find_records query, options.merge(:models => models), ar_options |
| 344 | 332 | ActsAsFerret::logger.debug "Query: #{query}\ntotal hits: #{total_hits}, results delivered: #{result.size}" |
| 345 | 333 | SearchResults.new(result, total_hits, options[:page], options[:per_page]) |
| ... | ...@@ -13,8 +13,10 @@ | |
| 13 | 13 | end |
| 14 | 14 | if late_pagination |
| 15 | 15 | limit = late_pagination[:limit] |
| 16 | offset = late_pagination[:offset] | |
| 17 | result = result[offset..limit+offset-1] | |
| 16 | offset = late_pagination[:offset] || 0 | |
| 17 | end_index = limit == :all ? -1 : limit+offset-1 | |
| 18 | # puts "late pagination: #{offset} : #{end_index}" | |
| 19 | result = result[offset..end_index] | |
| 18 | 20 | end |
| 19 | 21 | return [total_hits, result] |
| 20 | 22 | end |
| ... | ...@@ -24,9 +24,9 @@ | |
| 24 | 24 | options[:limit] = :all |
| 25 | 25 | total_hits, result = super query, options, ar_options |
| 26 | 26 | total_hits = result.size if ar_options[:conditions] |
| 27 | if limit && limit != :all | |
| 28 | result = result[offset..limit+offset-1] | |
| 29 | end | |
| 27 | # if limit && limit != :all | |
| 28 | # result = result[offset..limit+offset-1] | |
| 29 | # end | |
| 30 | 30 | [total_hits, result] |
| 31 | 31 | end |
| 32 | 32 |