Class: Queries::Loan::Autocomplete

Inherits:
Query
  • Object
show all
Defined in:
lib/queries/loan/autocomplete.rb

Instance Attribute Summary

Attributes inherited from Query

#dynamic_limit, #options, #project_id, #query_string, #terms

Instance Method Summary collapse

Methods inherited from Query

#alphabetic_strings, #attribute_exact_facet, #autocomplete_cached, #autocomplete_cached_wildcard_anywhere, #autocomplete_common_name_exact, #autocomplete_common_name_like, #autocomplete_exact_id, #autocomplete_exactly_named, #autocomplete_named, #autocomplete_ordered_wildcard_pieces_in_cached, #build_terms, #cached, #combine_or_clauses, #common_name_name, #common_name_table, #common_name_wild_pieces, #end_wildcard, #exactly_named, #fragments, #integers, #match_ordered_wildcard_pieces_in_cached, #match_wildcard_end_in_cached, #match_wildcard_in_cached, #named, #no_terms?, #only_ids, #only_integers?, #parent, #parent_child_join, #parent_child_where, #pieces, #result, #scope, #start_and_end_wildcard, #start_wildcard, #wildcard_pieces, #wildcard_wrapped_integers, #wildcard_wrapped_years, #with_cached, #with_cached_like, #with_id, #with_project_id, #year_letter, #years

Constructor Details

#initialize(string, project_id: nil) ⇒ Autocomplete

Returns a new instance of Autocomplete.

Parameters:

  • args (Hash)

7
8
9
# File 'lib/queries/loan/autocomplete.rb', line 7

def initialize(string, project_id: nil)
  super
end

Instance Method Details

#autocompleteArray

Returns:

  • (Array)

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/queries/loan/autocomplete.rb', line 17

def autocomplete
  queries = [
    autocomplete_recipient_email,
    autocomplete_exact_date_sent,
    autocomplete_exact_date_requested,
    autocomplete_exact_date_received,
    autocomplete_exact_id,
    autocomplete_identifier_identifier_exact,
    autocomplete_identifier_cached_like,
    autocomplete_identifier_matching_cached_fragments_anywhere,
    autocomplete_by_role
  ]
  queries.compact!

  updated_queries = []
  queries.each_with_index do |q,i|
    a = q
    a = q.where(project_id: project_id) if project_id
    updated_queries[i] = a
  end

  result = []
  updated_queries.each do |q|
    result += q.to_a
    result.uniq!
    break if result.count > 19
  end

  result[0..40]
end

#autocomplete_by_roleObject


68
69
70
71
72
# File 'lib/queries/loan/autocomplete.rb', line 68

def autocomplete_by_role
  r = ::Person.arel_table
  o = r[:cached].matches('%' + query_string + '%')
  ::Loan.joins(:people).where(roles: { type: ['LoanRecipient', 'LoanSupervisor']}).where(o.to_sql)
end

#autocomplete_exact_date_receivedObject


63
64
65
66
# File 'lib/queries/loan/autocomplete.rb', line 63

def autocomplete_exact_date_received
  o = table[:date_received].eq(query_string)
  ::Loan.where(o.to_sql)
end

#autocomplete_exact_date_requestedObject


58
59
60
61
# File 'lib/queries/loan/autocomplete.rb', line 58

def autocomplete_exact_date_requested
  o = table[:date_requested].eq(query_string)
  ::Loan.where(o.to_sql)
end

#autocomplete_exact_date_sentObject


53
54
55
56
# File 'lib/queries/loan/autocomplete.rb', line 53

def autocomplete_exact_date_sent
  o = table[:date_sent].eq(query_string)
  ::Loan.where(o.to_sql)
end

#autocomplete_recipient_emailObject


48
49
50
51
# File 'lib/queries/loan/autocomplete.rb', line 48

def autocomplete_recipient_email
  o = table[:recipient_email].matches('%' + query_string + '%')
  ::Loan.where(o.to_sql)
end

#base_queryScope

Returns:

  • (Scope)

75
76
77
# File 'lib/queries/loan/autocomplete.rb', line 75

def base_query
  ::Loan.select('loans.*')
end

#tableArel::Table

Returns:

  • (Arel::Table)

80
81
82
# File 'lib/queries/loan/autocomplete.rb', line 80

def table
  ::Loan.arel_table
end

#where_sqlString

Returns:

  • (String)

12
13
14
# File 'lib/queries/loan/autocomplete.rb', line 12

def where_sql
  with_project_id.and(or_and).to_sql
end