Class: Rawscsi::SearchHelpers::ResultsActiveRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/rawscsi/search_helpers/results_active_record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, model) ⇒ ResultsActiveRecord

Returns a new instance of ResultsActiveRecord.



6
7
8
9
# File 'lib/rawscsi/search_helpers/results_active_record.rb', line 6

def initialize(response, model)
  @response = response
  @model = model
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



4
5
6
# File 'lib/rawscsi/search_helpers/results_active_record.rb', line 4

def response
  @response
end

Instance Method Details

#buildObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rawscsi/search_helpers/results_active_record.rb', line 11

def build
  id_array = @response["hits"]["hit"].map {|h| model_id(h["id"]) }
  return [] if id_array.empty?
  results =
    if ActiveRecord::VERSION::MAJOR > 2
      klass.where(:id => id_array).to_a  
    else
      klass.find_all_by_id(id_array)
    end
  results.index_by(&:id).slice(*id_array).values
end