Class: MiddlewareAutocomplete::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/middleware_autocomplete/base.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.content_typeObject

Content type symbol, e.g. :json, :xml



58
59
60
# File 'lib/middleware_autocomplete/base.rb', line 58

def content_type
  @content_type
end

.namespaceObject

Returns the value of attribute namespace.



4
5
6
# File 'lib/middleware_autocomplete/base.rb', line 4

def namespace
  @namespace
end

.pathObject

Path without namespace



33
34
35
# File 'lib/middleware_autocomplete/base.rb', line 33

def path
  @path
end

.route_nameObject

Route name to access Autocomplete search, e.g. autocomplete_posts_path



38
39
40
# File 'lib/middleware_autocomplete/base.rb', line 38

def route_name
  @route_name
end

.search_keyObject

Returns the value of attribute search_key.



4
5
6
# File 'lib/middleware_autocomplete/base.rb', line 4

def search_key
  @search_key
end

Class Method Details

.call(env) ⇒ Object



20
21
22
23
24
25
# File 'lib/middleware_autocomplete/base.rb', line 20

def call(env)
  ActiveSupport::Notifications.instrument 'request.middleware_autocomplete' do
    result = perform(Rack::Request.new(env).params)
    [200, { 'Content-Type' => content_type_string }, [result]]
  end
end

.content_type_stringObject

Content type string, e.g. ‘application/json’, ‘application/xml’



63
64
65
# File 'lib/middleware_autocomplete/base.rb', line 63

def content_type_string
  Mime::Type.lookup_by_extension(content_type).to_s
end

.perform(params) ⇒ Object

Calls user defined method to return results Wraps this method with AR with_connection to prevent connection leaks

Attributes

  • params - Params hash passed to request



12
13
14
15
16
17
18
# File 'lib/middleware_autocomplete/base.rb', line 12

def perform(params)
  if use_with_connection
    with_connection { search(params) }
  else
    search(params)
  end
end

.routeObject

Full path to the Autocomplete class



28
29
30
# File 'lib/middleware_autocomplete/base.rb', line 28

def route
  [namespace, path].reject(&:blank?).join('/').prepend('/')
end

.search(params) ⇒ Object

Returns search results for autocompletition Result should be in the same format that your content_type Should be defined by user

Raises:

  • (NotImplementedError)


53
54
55
# File 'lib/middleware_autocomplete/base.rb', line 53

def search(params)
  raise NotImplementedError
end

.use_with_connectionObject



46
47
48
# File 'lib/middleware_autocomplete/base.rb', line 46

def use_with_connection
  @use_with_connection || MiddlewareAutocomplete.use_with_connection
end