From db600b33d94fe7f596f5d944ea193ada1d3650cb Mon Sep 17 00:00:00 2001 From: jk Date: Sat, 2 Feb 2008 20:30:59 +0000 Subject: [PATCH] #194 git-svn-id: svn://projects.jkraemer.net/acts_as_ferret/trunk/plugin/acts_as_ferret@310 7326d000-0a0e-0410-9cad-a9b28e7838dc --- lib/class_methods.rb | 7 ++++--- lib/ferret_result.rb | 26 ++++++++++++++++++-------- lib/search_results.rb | 6 ++++-- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/lib/class_methods.rb b/lib/class_methods.rb index 60f2670..c3e0776 100644 --- a/lib/class_methods.rb +++ b/lib/class_methods.rb @@ -276,14 +276,14 @@ module ActsAsFerret def _multi_search(query, additional_models = [], options = {}, find_options = {}) result = [] + rank = 0 if options[:lazy] logger.warn "find_options #{find_options} are ignored because :lazy => true" unless find_options.empty? total_hits = id_multi_search(query, additional_models, options) do |model, id, score, data| - result << FerretResult.new(model, id, score, data) + result << FerretResult.new(model, id, score, rank += 1, data) end else id_arrays = {} - rank = 0 limit = options.delete(:limit) offset = options.delete(:offset) || 0 @@ -352,8 +352,9 @@ module ActsAsFerret def lazy_find_by_contents(q, options = {}) result = [] + rank = 0 total_hits = find_id_by_contents(q, options) do |model, id, score, data| - result << FerretResult.new(model, id, score, data) + result << FerretResult.new(model, id, score, rank += 1, data) end [ total_hits, result ] end diff --git a/lib/ferret_result.rb b/lib/ferret_result.rb index 457926c..f15cb04 100644 --- a/lib/ferret_result.rb +++ b/lib/ferret_result.rb @@ -9,28 +9,38 @@ module ActsAsFerret attr_accessor :ferret_rank end - class FerretResult + class FerretResult < BlankSlate include ResultAttributes attr_accessor :id + reveal :methods - def initialize(model, id, score, data = {}) + def initialize(model, id, score, rank, data = {}) @model = model.constantize @id = id @ferret_score = score + @ferret_rank = rank @data = data end - def method_missing(method, *args) - if @ar_record || @data[method].nil? - ferret_load_record unless @ar_record - @ar_record.send method, *args + def method_missing(method, *args, &block) + if @ar_record || !@data.has_key?(method) + to_record.send method, *args, &block else @data[method] end end - def ferret_load_record - @ar_record = @model.find(id) + def respond_to?(name) + methods.include?(name.to_s) || @data.has_key?(name.to_sym) || to_record.respond_to?(name) + end + + def to_record + unless @ar_record + @ar_record = @model.find(id) + @ar_record.ferret_rank = ferret_rank + @ar_record.ferret_score = ferret_score + end + @ar_record end end end diff --git a/lib/search_results.rb b/lib/search_results.rb index 8f985e5..db731db 100644 --- a/lib/search_results.rb +++ b/lib/search_results.rb @@ -2,8 +2,10 @@ module ActsAsFerret # decorator that adds a total_hits accessor and will_paginate compatible # paging support to search result arrays - class SearchResults + class SearchResults < BlankSlate + reveal :methods attr_reader :current_page, :per_page, :total_hits + alias total_entries total_hits # will_paginate compatibility def initialize(results, total_hits, current_page = 1, per_page = nil) @results = results @@ -18,7 +20,7 @@ module ActsAsFerret end def respond_to?(name) - self.methods.include?(name) || @results.respond_to?(name) + methods.include?(name.to_s) || @results.respond_to?(name) end -- 2.11.4.GIT