To edit pages or tickets please login with username/password: aaf/aaf

Changeset 310

Show
Ignore:
Timestamp:
02/02/08 20:30:59 (8 months ago)
Author:
jk
Message:

#194

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/demo/test/unit/content_test.rb

    r308 r310  
    5454    assert_equal 1, results.size 
    5555    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 
    5659    assert ActsAsFerret::FerretResult === result 
    5760    assert_equal 'A useless description', result.description 
    5861    assert_nil result.instance_variable_get(:@ar_record) 
    5962    assert_equal 'My Title', result.title 
    60     assert_not_nil result.instance_variable_get(:@ar_record) 
     63    assert_not_nil result.ar_record 
    6164  end 
    6265 
  • trunk/demo/test/unit/ferret_result_test.rb

    r169 r310  
    1010   
    1111  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' 
    1313    assert_equal 'description from ferret index', fr.description 
    1414    assert_equal 0.5, fr.ferret_score 
     15    assert_equal 1, fr.ferret_rank 
    1516    assert_equal 'My Title', fr.title # triggers auto-load of the record 
    1617    assert_equal 'A useless description', fr.description # description now comes from DB 
  • trunk/plugin/acts_as_ferret/lib/class_methods.rb

    r304 r310  
    277277      result = [] 
    278278 
     279      rank = 0 
    279280      if options[:lazy] 
    280281        logger.warn "find_options #{find_options} are ignored because :lazy => true" unless find_options.empty? 
    281282        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) 
    283284        end 
    284285      else 
    285286        id_arrays = {} 
    286         rank = 0 
    287287 
    288288        limit = options.delete(:limit) 
     
    353353    def lazy_find_by_contents(q, options = {}) 
    354354      result = [] 
     355      rank   = 0 
    355356      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) 
    357358      end 
    358359      [ total_hits, result ] 
  • trunk/plugin/acts_as_ferret/lib/ferret_result.rb

    r168 r310  
    1010  end 
    1111 
    12   class FerretResult 
     12  class FerretResult < BlankSlate 
    1313    include ResultAttributes 
    1414    attr_accessor :id 
     15    reveal :methods 
    1516 
    16     def initialize(model, id, score, data = {}) 
     17    def initialize(model, id, score, rank, data = {}) 
    1718      @model = model.constantize 
    1819      @id = id 
    1920      @ferret_score = score 
     21      @ferret_rank  = rank 
    2022      @data = data 
    2123    end 
    2224     
    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 
    2728      else 
    2829        @data[method] 
     
    3031    end 
    3132 
    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 
    3444    end 
    3545  end 
  • trunk/plugin/acts_as_ferret/lib/search_results.rb

    r222 r310  
    33  # decorator that adds a total_hits accessor and will_paginate compatible  
    44  # paging support to search result arrays 
    5   class SearchResults 
     5  class SearchResults < BlankSlate 
     6    reveal :methods 
    67    attr_reader :current_page, :per_page, :total_hits 
     8    alias total_entries total_hits  # will_paginate compatibility 
    79 
    810    def initialize(results, total_hits, current_page = 1, per_page = nil) 
     
    1921 
    2022    def respond_to?(name) 
    21       self.methods.include?(name) || @results.respond_to?(name) 
     23      methods.include?(name.to_s) || @results.respond_to?(name) 
    2224    end 
    2325 

To edit pages or tickets please login with username/password: aaf/aaf