Class: ActiveKit::Search::SearchResult

Inherits:
Object
  • Object
show all
Defined in:
lib/active_kit/search/search_result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(term:, results:, offset:, limit:, page:, current_class:) ⇒ SearchResult

Returns a new instance of SearchResult.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/active_kit/search/search_result.rb', line 6

def initialize(term:, results:, offset:, limit:, page:, current_class:)
  @term = term

  if results
    @count = results.shift
    @documents = results.each_slice(2).map { |key, attributes| [key, attributes.each_slice(2).to_h] }.to_h

    if page.present?
      @current_page = page
      @previous_page = @current_page > 1 ? (@current_page - 1) : nil
      @next_page = (offset + limit) < count ? (@current_page + 1) : nil
    end
  else
    @count = 0
    @documents = {}
  end

  @keys = @documents.keys
  @ids = @documents.map { |key, value| key.split(":").last }

  # Return records from database.
  # This also ensures that any left over document_ids in redis that have been deleted in the database are left out of the results.
  # This orders the records in the order of executed search.
  @records = current_class.where(id: ids).reorder(Arel.sql("FIELD(#{current_class.table_name}.id, #{ids.join(', ')})"))
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



4
5
6
# File 'lib/active_kit/search/search_result.rb', line 4

def count
  @count
end

#current_pageObject (readonly)

Returns the value of attribute current_page.



4
5
6
# File 'lib/active_kit/search/search_result.rb', line 4

def current_page
  @current_page
end

#documentsObject (readonly)

Returns the value of attribute documents.



4
5
6
# File 'lib/active_kit/search/search_result.rb', line 4

def documents
  @documents
end

#idsObject (readonly)

Returns the value of attribute ids.



4
5
6
# File 'lib/active_kit/search/search_result.rb', line 4

def ids
  @ids
end

#keysObject (readonly)

Returns the value of attribute keys.



4
5
6
# File 'lib/active_kit/search/search_result.rb', line 4

def keys
  @keys
end

#next_pageObject (readonly)

Returns the value of attribute next_page.



4
5
6
# File 'lib/active_kit/search/search_result.rb', line 4

def next_page
  @next_page
end

#previous_pageObject (readonly)

Returns the value of attribute previous_page.



4
5
6
# File 'lib/active_kit/search/search_result.rb', line 4

def previous_page
  @previous_page
end

#recordsObject (readonly)

Returns the value of attribute records.



4
5
6
# File 'lib/active_kit/search/search_result.rb', line 4

def records
  @records
end

#termObject (readonly)

Returns the value of attribute term.



4
5
6
# File 'lib/active_kit/search/search_result.rb', line 4

def term
  @term
end