| 228 | | def add_self_to_model_list_if_necessary(models) |
|---|
| 229 | | models = [ models ] unless models.is_a? Array |
|---|
| 230 | | models << self unless models.include?(self) |
|---|
| 231 | | models |
|---|
| 232 | | end |
|---|
| 233 | | |
|---|
| 234 | | def find_records_lazy_or_not(q, options = {}, find_options = {}) |
|---|
| 235 | | if options[:lazy] |
|---|
| 236 | | logger.warn "find_options #{find_options} are ignored because :lazy => true" unless find_options.empty? |
|---|
| 237 | | lazy_find_by_contents q, options |
|---|
| 238 | | else |
|---|
| 239 | | ar_find_by_contents q, options, find_options |
|---|
| 240 | | end |
|---|
| 241 | | end |
|---|
| 242 | | |
|---|
| 243 | | def ar_find_by_contents(q, options = {}, find_options = {}) |
|---|
| 244 | | result_ids = {} |
|---|
| 245 | | total_hits = find_ids_with_ferret(q, options) do |model, id, score, data| |
|---|
| 246 | | # stores ids, index and score of each hit for later ordering of |
|---|
| 247 | | # results |
|---|
| 248 | | result_ids[id] = [ result_ids.size + 1, score ] |
|---|
| 249 | | end |
|---|
| 250 | | |
|---|
| 251 | | result = ActsAsFerret::retrieve_records( { self.name => result_ids }, find_options ) |
|---|
| 252 | | |
|---|
| 253 | | # count total_hits via sql when using conditions or when we're called |
|---|
| 254 | | # from an ActiveRecord association. |
|---|
| 255 | | if find_options[:conditions] or caller.find{ |call| call =~ %r{active_record/associations} } |
|---|
| 256 | | # chances are the ferret result count is not our total_hits value, so |
|---|
| 257 | | # we correct this here. |
|---|
| 258 | | if options[:limit] != :all || options[:page] || options[:offset] || find_options[:limit] || find_options[:offset] |
|---|
| 259 | | # our ferret result has been limited, so we need to re-run that |
|---|
| 260 | | # search to get the full result set from ferret. |
|---|
| 261 | | result_ids = {} |
|---|
| 262 | | find_ids_with_ferret(q, options.update(:limit => :all, :offset => 0)) do |model, id, score, data| |
|---|
| 263 | | result_ids[id] = [ result_ids.size + 1, score ] |
|---|
| 264 | | end |
|---|
| 265 | | # Now ask the database for the total size of the final result set. |
|---|
| 266 | | total_hits = count_records( { self.name => result_ids }, find_options ) |
|---|
| 267 | | else |
|---|
| 268 | | # what we got from the database is our full result set, so take |
|---|
| 269 | | # it's size |
|---|
| 270 | | total_hits = result.length |
|---|
| 271 | | end |
|---|
| 272 | | end |
|---|
| 273 | | |
|---|
| 274 | | [ total_hits, result ] |
|---|
| 275 | | end |
|---|
| 276 | | |
|---|
| 277 | | def lazy_find_by_contents(q, options = {}) |
|---|
| 278 | | logger.debug "lazy_find_by_contents: #{q}" |
|---|
| 279 | | result = [] |
|---|
| 280 | | rank = 0 |
|---|
| 281 | | total_hits = find_ids_with_ferret(q, options) do |model, id, score, data| |
|---|
| 282 | | logger.debug "model: #{model}, id: #{id}, data: #{data}" |
|---|
| 283 | | result << FerretResult.new(model, id, score, rank += 1, data) |
|---|
| 284 | | end |
|---|
| 285 | | [ total_hits, result ] |
|---|
| 286 | | end |
|---|
| | 205 | # def find_records_lazy_or_not(q, options = {}, find_options = {}) |
|---|
| | 206 | # if options[:lazy] |
|---|
| | 207 | # logger.warn "find_options #{find_options} are ignored because :lazy => true" unless find_options.empty? |
|---|
| | 208 | # lazy_find_by_contents q, options |
|---|
| | 209 | # else |
|---|
| | 210 | # ar_find_by_contents q, options, find_options |
|---|
| | 211 | # end |
|---|
| | 212 | # end |
|---|
| | 213 | # |
|---|
| | 214 | # def ar_find_by_contents(q, options = {}, find_options = {}) |
|---|
| | 215 | # result_ids = {} |
|---|
| | 216 | # total_hits = find_ids_with_ferret(q, options) do |model, id, score, data| |
|---|
| | 217 | # # stores ids, index and score of each hit for later ordering of |
|---|
| | 218 | # # results |
|---|
| | 219 | # result_ids[id] = [ result_ids.size + 1, score ] |
|---|
| | 220 | # end |
|---|
| | 221 | # |
|---|
| | 222 | # result = ActsAsFerret::retrieve_records( { self.name => result_ids }, find_options ) |
|---|
| | 223 | # |
|---|
| | 224 | # # count total_hits via sql when using conditions or when we're called |
|---|
| | 225 | # # from an ActiveRecord association. |
|---|
| | 226 | # if find_options[:conditions] or caller.find{ |call| call =~ %r{active_record/associations} } |
|---|
| | 227 | # # chances are the ferret result count is not our total_hits value, so |
|---|
| | 228 | # # we correct this here. |
|---|
| | 229 | # if options[:limit] != :all || options[:page] || options[:offset] || find_options[:limit] || find_options[:offset] |
|---|
| | 230 | # # our ferret result has been limited, so we need to re-run that |
|---|
| | 231 | # # search to get the full result set from ferret. |
|---|
| | 232 | # result_ids = {} |
|---|
| | 233 | # find_ids_with_ferret(q, options.update(:limit => :all, :offset => 0)) do |model, id, score, data| |
|---|
| | 234 | # result_ids[id] = [ result_ids.size + 1, score ] |
|---|
| | 235 | # end |
|---|
| | 236 | # # Now ask the database for the total size of the final result set. |
|---|
| | 237 | # total_hits = count_records( { self.name => result_ids }, find_options ) |
|---|
| | 238 | # else |
|---|
| | 239 | # # what we got from the database is our full result set, so take |
|---|
| | 240 | # # it's size |
|---|
| | 241 | # total_hits = result.length |
|---|
| | 242 | # end |
|---|
| | 243 | # end |
|---|
| | 244 | # |
|---|
| | 245 | # [ total_hits, result ] |
|---|
| | 246 | # end |
|---|
| | 247 | # |
|---|
| | 248 | # def lazy_find_by_contents(q, options = {}) |
|---|
| | 249 | # logger.debug "lazy_find_by_contents: #{q}" |
|---|
| | 250 | # result = [] |
|---|
| | 251 | # rank = 0 |
|---|
| | 252 | # total_hits = find_ids_with_ferret(q, options) do |model, id, score, data| |
|---|
| | 253 | # logger.debug "model: #{model}, id: #{id}, data: #{data}" |
|---|
| | 254 | # result << FerretResult.new(model, id, score, rank += 1, data) |
|---|
| | 255 | # end |
|---|
| | 256 | # [ total_hits, result ] |
|---|
| | 257 | # end |
|---|
| 294 | | def count_records(id_arrays, find_options = {}) |
|---|
| 295 | | count_options = find_options.dup |
|---|
| 296 | | count_options.delete :limit |
|---|
| 297 | | count_options.delete :offset |
|---|
| 298 | | count = 0 |
|---|
| 299 | | id_arrays.each do |model, id_array| |
|---|
| 300 | | next if id_array.empty? |
|---|
| 301 | | model = model.constantize |
|---|
| 302 | | # merge conditions |
|---|
| 303 | | conditions = ActsAsFerret::combine_conditions([ "#{model.table_name}.#{model.primary_key} in (?)", id_array.keys ], |
|---|
| 304 | | find_options[:conditions]) |
|---|
| 305 | | opts = find_options.merge :conditions => conditions |
|---|
| 306 | | opts.delete :limit; opts.delete :offset |
|---|
| 307 | | count += model.count opts |
|---|
| 308 | | end |
|---|
| 309 | | count |
|---|
| 310 | | end |
|---|
| | 265 | # def count_records(id_arrays, find_options = {}) |
|---|
| | 266 | # count_options = find_options.dup |
|---|
| | 267 | # count_options.delete :limit |
|---|
| | 268 | # count_options.delete :offset |
|---|
| | 269 | # count = 0 |
|---|
| | 270 | # id_arrays.each do |model, id_array| |
|---|
| | 271 | # next if id_array.empty? |
|---|
| | 272 | # model = model.constantize |
|---|
| | 273 | # # merge conditions |
|---|
| | 274 | # conditions = ActsAsFerret::combine_conditions([ "#{model.table_name}.#{model.primary_key} in (?)", id_array.keys ], |
|---|
| | 275 | # find_options[:conditions]) |
|---|
| | 276 | # opts = find_options.merge :conditions => conditions |
|---|
| | 277 | # opts.delete :limit; opts.delete :offset |
|---|
| | 278 | # count += model.count opts |
|---|
| | 279 | # end |
|---|
| | 280 | # count |
|---|
| | 281 | # end |
|---|