Class: Search::ResultObjects::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/plok/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.



10
11
12
13
# File 'lib/plok/search/result_objects/base.rb', line 10

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

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



6
7
8
# File 'lib/plok/search/result_objects/base.rb', line 6

def index
  @index
end

#search_contextObject (readonly)

Returns the value of attribute search_context.



6
7
8
# File 'lib/plok/search/result_objects/base.rb', line 6

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.



32
33
34
# File 'lib/plok/search/result_objects/base.rb', line 32

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

#hidden?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/plok/search/result_objects/base.rb', line 36

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

#labelObject



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

def label
  # We want to grab the name of the index from ContentText whenever
  # we're dealing with FlexibleContent stuff.
  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/plok/search/result_objects/base.rb', line 51

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

#namespaceObject



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

def namespace
  search_context.namespace.to_s.underscore
end

#partialObject



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

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

#partial_pathObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/plok/search/result_objects/base.rb', line 63

def partial_path
  # TODO: This is a fallback for older Plok versions whose result object
  # partials still reside in app/views/backens/search/*. Best to remove
  # this at a later stage, but for now they can be backwards compatible.
  if File.exists?("app/views/#{namespace}/search/_#{partial_target}.html.erb")
    return "#{namespace}/search/"
  end

  "plok/search/result_objects/#{namespace}"
end

#partial_targetObject



74
75
76
# File 'lib/plok/search/result_objects/base.rb', line 74

def partial_target
  index.searchable_type.underscore
end

#unpublished?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/plok/search/result_objects/base.rb', line 78

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

#urlObject



82
83
# File 'lib/plok/search/result_objects/base.rb', line 82

def url
end