Sha256: f8817a2f71093c44a27869876b99c2fc3dbc0530ea189e8c89b8abb3e1479fc6

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

module ActsAsFerret

  # mixed into the FerretResult and AR classes calling acts_as_ferret
  module ResultAttributes
    # holds the score this record had when it was found via
    # acts_as_ferret
    attr_accessor :ferret_score

    attr_accessor :ferret_rank
  end

  class FerretResult < ActsAsFerret::BlankSlate
    include ResultAttributes
    attr_accessor :id

    def initialize(model, id, score, rank, data = {})
      @model = model.constantize
      @id = id
      @ferret_score = score
      @ferret_rank  = rank
      @data = data
      @use_record = false
    end

    def inspect
      "#<FerretResult wrapper for #{@model} with id #{@id}>"
    end

    def method_missing(method, *args, &block)
      if (@ar_record && @use_record) || !@data.has_key?(method)
        to_record.send method, *args, &block
      else
        @data[method]
      end
    end

    def respond_to?(name)
      [ :ferret_score, :ferret_rank,
        :inspect, :method_missing, :respond_to?, :to_record, :to_param, :id
      ].include?(name) ||
        @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
        # don't try to fetch attributes from RDig based records
        @use_record = !@ar_record.class.included_modules.include?(::ActsAsFerret::RdigAdapter)
      end
      @ar_record
    end
    
    def to_param
      return @id
    end
    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
acts_as_ferret-0.5.4 lib/acts_as_ferret/ferret_result.rb
acts_as_ferret-0.5.3 lib/acts_as_ferret/ferret_result.rb