Class: Udongo::Search::ResultObjects::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/udongo/search/result_objects/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index, search_context: nil) ⇒ Base

Returns a new instance of Base.



12
13
14
15
# File 'lib/udongo/search/result_objects/base.rb', line 12

def initialize(index, search_context: nil)
  @index = index
  @search_context = search_context
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



8
9
10
# File 'lib/udongo/search/result_objects/base.rb', line 8

def index
  @index
end

#search_contextObject (readonly)

Returns the value of attribute search_context.



8
9
10
# File 'lib/udongo/search/result_objects/base.rb', line 8

def search_context
  @search_context
end

Instance Method Details

#build_htmlObject

Typically, an autocomplete requires 3 things:

  • A title indicating a resource name. Examples: Page#title, Product#name,…

  • A truncated summary providing a glimpse of the resource’s contents. Examples: Page#subtitle, Product#description,…

  • A link to the resource. Examples: edit_backend_page_path(37), product_path(37),…

However, this seems very restrictive to me. If I narrow down the data a dev can use in an autocomplete, it severely reduces options he/she has in how the autocomplete results look like. Think of autocompletes in a shop that require images or prices to be included in their result bodies.

This is why I chose to let ApplicationController.render work around the problem by letting the dev decide how the row should look.



34
35
36
# File 'lib/udongo/search/result_objects/base.rb', line 34

def build_html
  ApplicationController.render(partial: partial, locals: locals)
end

#hidden?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/udongo/search/result_objects/base.rb', line 38

def hidden?
  searchable.respond_to?(:visible?) && searchable.hidden?
end

#labelObject



42
43
44
45
46
47
48
49
# File 'lib/udongo/search/result_objects/base.rb', line 42

def label
  if index.name.include?('flexible_content')
    id = index.name.split(':')[1].to_i
    return ContentText.find(id).content
  end

  searchable.send(index.name)
end

#localsObject



51
52
53
# File 'lib/udongo/search/result_objects/base.rb', line 51

def locals
  { "#{partial_target}": index.searchable, index: index }
end

#partialObject



55
56
57
# File 'lib/udongo/search/result_objects/base.rb', line 55

def partial
  "#{partial_path}/#{partial_target}"
end

#partial_pathObject



59
60
61
# File 'lib/udongo/search/result_objects/base.rb', line 59

def partial_path
  "#{search_context.namespace.to_s.underscore}/search"
end

#partial_targetObject



63
64
65
# File 'lib/udongo/search/result_objects/base.rb', line 63

def partial_target
  index.searchable_type.underscore
end

#unpublished?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/udongo/search/result_objects/base.rb', line 67

def unpublished?
  searchable.respond_to?(:published?) && searchable.unpublished?
end

#urlObject



71
72
# File 'lib/udongo/search/result_objects/base.rb', line 71

def url
end