Sha256: 73e13f9cc737757bbf92e854c175a093626f12247a74d78ca503b17dc8ed26fb

Contents?: true

Size: 1.36 KB

Versions: 12

Compression:

Stored size: 1.36 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
    reveal :methods

    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)
      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
        # don't try to fetch attributes from RDig based records
        @use_record = !@ar_record.class.included_modules.include?(ActsAsFerret::RdigAdapter)
      end
      @ar_record
    end
  end
end

Version data entries

12 entries across 12 versions & 3 rubygems

Version Path
cehoffman-acts_as_ferret-0.4.4 lib/ferret_result.rb
jkraemer-acts_as_ferret-0.4.4 lib/ferret_result.rb
acts_as_ferret-0.5.2 lib/acts_as_ferret/ferret_result.rb
acts_as_ferret-0.4.8.2 lib/ferret_result.rb
acts_as_ferret-0.4.8.1 lib/ferret_result.rb
acts_as_ferret-0.5.1 lib/acts_as_ferret/ferret_result.rb
acts_as_ferret-0.5 lib/acts_as_ferret/ferret_result.rb
acts_as_ferret-0.4.8.rails3 lib/ferret_result.rb
acts_as_ferret-0.4.8 lib/ferret_result.rb
acts_as_ferret-0.4.7 lib/ferret_result.rb
acts_as_ferret-0.4.6 lib/ferret_result.rb
acts_as_ferret-0.4.4 lib/ferret_result.rb