Sha256: 11d8f8dc2ae81bdfdebff629652c2d8e01eea0f3fdd5123be6fecdf2c01fafd2

Contents?: true

Size: 1.11 KB

Versions: 4

Compression:

Stored size: 1.11 KB

Contents

class ThinkingSphinx::Search::Translator
  attr_reader :raw, :excerpter

  def initialize(raw, excerpter)
    @raw, @excerpter = raw, excerpter
  end

  def to_active_record
    results_for_models # load now to avoid segfaults
    raw.collect { |row|
      ThinkingSphinx::Search::Glaze.new result_for(row), excerpter, row
    }
  end

  private

  def ids_for_model(model_name)
    raw.select { |row|
      row['sphinx_internal_class'] == model_name
    }.collect { |row|
      row['sphinx_internal_id']
    }
  end

  def model_names
    @model_names ||= raw.collect { |row|
      row['sphinx_internal_class']
    }.uniq
  end

  def result_for(row)
    results_for_models[row['sphinx_internal_class']].detect { |record|
      record.id == row['sphinx_internal_id']
    }
  end

  def results_for_models
    @results_for_models ||= model_names.inject({}) { |hash, name|
      ids = ids_for_model(name)
      hash[name] = name.constantize.where(:id => ids)

      stale_ids = ids - hash[name].collect(&:id)
      if stale_ids.any?
        raise ThinkingSphinx::Search::StaleIdsException, stale_ids
      end

      hash
    }
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
thinking-sphinx-3.0.1 lib/thinking_sphinx/search/translator.rb
thinking-sphinx-3.0.0 lib/thinking_sphinx/search/translator.rb
thinking-sphinx-3.0.0.rc lib/thinking_sphinx/search/translator.rb
thinking-sphinx-3.0.0.pre lib/thinking_sphinx/search/translator.rb