Class: ActiveKit::Search::SearchResult
- Inherits:
-
Object
- Object
- ActiveKit::Search::SearchResult
- Defined in:
- lib/active_kit/search/search_result.rb
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#current_page ⇒ Object
readonly
Returns the value of attribute current_page.
-
#documents ⇒ Object
readonly
Returns the value of attribute documents.
-
#ids ⇒ Object
readonly
Returns the value of attribute ids.
-
#keys ⇒ Object
readonly
Returns the value of attribute keys.
-
#next_page ⇒ Object
readonly
Returns the value of attribute next_page.
-
#previous_page ⇒ Object
readonly
Returns the value of attribute previous_page.
-
#records ⇒ Object
readonly
Returns the value of attribute records.
-
#term ⇒ Object
readonly
Returns the value of attribute term.
Instance Method Summary collapse
-
#initialize(term:, results:, offset:, limit:, page:, current_class:) ⇒ SearchResult
constructor
A new instance of SearchResult.
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
#count ⇒ Object (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_page ⇒ Object (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 |
#documents ⇒ Object (readonly)
Returns the value of attribute documents.
4 5 6 |
# File 'lib/active_kit/search/search_result.rb', line 4 def documents @documents end |
#ids ⇒ Object (readonly)
Returns the value of attribute ids.
4 5 6 |
# File 'lib/active_kit/search/search_result.rb', line 4 def ids @ids end |
#keys ⇒ Object (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_page ⇒ Object (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_page ⇒ Object (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 |
#records ⇒ Object (readonly)
Returns the value of attribute records.
4 5 6 |
# File 'lib/active_kit/search/search_result.rb', line 4 def records @records end |
#term ⇒ Object (readonly)
Returns the value of attribute term.
4 5 6 |
# File 'lib/active_kit/search/search_result.rb', line 4 def term @term end |