Changeset 310
- Timestamp:
- 02/02/08 20:30:59 (8 months ago)
- Files:
-
- trunk/demo/test/unit/content_test.rb (modified) (1 diff)
- trunk/demo/test/unit/ferret_result_test.rb (modified) (1 diff)
- trunk/plugin/acts_as_ferret/lib/class_methods.rb (modified) (2 diffs)
- trunk/plugin/acts_as_ferret/lib/ferret_result.rb (modified) (2 diffs)
- trunk/plugin/acts_as_ferret/lib/search_results.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/demo/test/unit/content_test.rb
r308 r310 54 54 assert_equal 1, results.size 55 55 result = results.first 56 class << result 57 attr_accessor :ar_record # so we have a chance to check if it's been loaded... 58 end 56 59 assert ActsAsFerret::FerretResult === result 57 60 assert_equal 'A useless description', result.description 58 61 assert_nil result.instance_variable_get(:@ar_record) 59 62 assert_equal 'My Title', result.title 60 assert_not_nil result. instance_variable_get(:@ar_record)63 assert_not_nil result.ar_record 61 64 end 62 65 trunk/demo/test/unit/ferret_result_test.rb
r169 r310 10 10 11 11 def test_get_prefetched_fields_from_hash 12 fr = ActsAsFerret::FerretResult.new 'Content', '1', 0.5, :description => 'description from ferret index'12 fr = ActsAsFerret::FerretResult.new 'Content', '1', 0.5, 1, :description => 'description from ferret index' 13 13 assert_equal 'description from ferret index', fr.description 14 14 assert_equal 0.5, fr.ferret_score 15 assert_equal 1, fr.ferret_rank 15 16 assert_equal 'My Title', fr.title # triggers auto-load of the record 16 17 assert_equal 'A useless description', fr.description # description now comes from DB trunk/plugin/acts_as_ferret/lib/class_methods.rb
r304 r310 277 277 result = [] 278 278 279 rank = 0 279 280 if options[:lazy] 280 281 logger.warn "find_options #{find_options} are ignored because :lazy => true" unless find_options.empty? 281 282 total_hits = id_multi_search(query, additional_models, options) do |model, id, score, data| 282 result << FerretResult.new(model, id, score, data)283 result << FerretResult.new(model, id, score, rank += 1, data) 283 284 end 284 285 else 285 286 id_arrays = {} 286 rank = 0287 287 288 288 limit = options.delete(:limit) … … 353 353 def lazy_find_by_contents(q, options = {}) 354 354 result = [] 355 rank = 0 355 356 total_hits = find_id_by_contents(q, options) do |model, id, score, data| 356 result << FerretResult.new(model, id, score, data)357 result << FerretResult.new(model, id, score, rank += 1, data) 357 358 end 358 359 [ total_hits, result ] trunk/plugin/acts_as_ferret/lib/ferret_result.rb
r168 r310 10 10 end 11 11 12 class FerretResult 12 class FerretResult < BlankSlate 13 13 include ResultAttributes 14 14 attr_accessor :id 15 reveal :methods 15 16 16 def initialize(model, id, score, data = {})17 def initialize(model, id, score, rank, data = {}) 17 18 @model = model.constantize 18 19 @id = id 19 20 @ferret_score = score 21 @ferret_rank = rank 20 22 @data = data 21 23 end 22 24 23 def method_missing(method, *args) 24 if @ar_record || @data[method].nil? 25 ferret_load_record unless @ar_record 26 @ar_record.send method, *args 25 def method_missing(method, *args, &block) 26 if @ar_record || !@data.has_key?(method) 27 to_record.send method, *args, &block 27 28 else 28 29 @data[method] … … 30 31 end 31 32 32 def ferret_load_record 33 @ar_record = @model.find(id) 33 def respond_to?(name) 34 methods.include?(name.to_s) || @data.has_key?(name.to_sym) || to_record.respond_to?(name) 35 end 36 37 def to_record 38 unless @ar_record 39 @ar_record = @model.find(id) 40 @ar_record.ferret_rank = ferret_rank 41 @ar_record.ferret_score = ferret_score 42 end 43 @ar_record 34 44 end 35 45 end trunk/plugin/acts_as_ferret/lib/search_results.rb
r222 r310 3 3 # decorator that adds a total_hits accessor and will_paginate compatible 4 4 # paging support to search result arrays 5 class SearchResults 5 class SearchResults < BlankSlate 6 reveal :methods 6 7 attr_reader :current_page, :per_page, :total_hits 8 alias total_entries total_hits # will_paginate compatibility 7 9 8 10 def initialize(results, total_hits, current_page = 1, per_page = nil) … … 19 21 20 22 def respond_to?(name) 21 self.methods.include?(name) || @results.respond_to?(name)23 methods.include?(name.to_s) || @results.respond_to?(name) 22 24 end 23 25
