Class: Queries::Serial::Autocomplete

Inherits:
Query
  • Object
show all
Defined in:
lib/queries/serial/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, #autocomplete_cached, #autocomplete_cached_wildcard_anywhere, #autocomplete_identifier_cached_exact, #autocomplete_identifier_cached_like, #autocomplete_ordered_wildcard_pieces_in_cached, #autocomplete_start_date, #build_terms, #cached, #combine_or_clauses, #end_wildcard, #exactly_named, #fragments, #identifier_table, #integers, #match_ordered_wildcard_pieces_in_cached, #match_wildcard_cached, #named, #no_digits, #only_ids, #only_integers?, #parent, #parent_child_join, #parent_child_where, #pieces, #result, #scope, #simple_date, #start_and_end_wildcard, #start_wildcard, #wildcard_pieces, #wildcard_wrapped_integers, #with_id, #with_identifier, #with_identifier_like, #with_project_id, #with_start_date, #year_letter, #years

Constructor Details

- (Autocomplete) initialize(string, project_id: nil)

Returns a new instance of Autocomplete

Parameters:

  • args (Hash)


6
7
8
# File 'lib/queries/serial/autocomplete.rb', line 6

def initialize(string, project_id: nil) # Project_id does nothing yet
  super
end

Instance Method Details

- (Arel::Table) alternate_value_table

Returns:

  • (Arel::Table)


107
108
109
# File 'lib/queries/serial/autocomplete.rb', line 107

def alternate_value_table
  ::AlternateValue.arel_table
end

- (Array) autocomplete

Returns:

  • (Array)


16
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
47
48
49
50
# File 'lib/queries/serial/autocomplete.rb', line 16

def autocomplete
  queries = [
    autocomplete_exact_name,
    autocomplete_exact_alternate_value,
    autocomplete_exact_alternate_value('name', 'AlternateValue::Translation'),
    autocomplete_exact_alternate_value('name', 'AlternateValue::Abbreviation'),

    autocomplete_begining_name,
    autocomplete_begining_alternate_value,
    autocomplete_begining_alternate_value('name', 'AlternateValue::Translation'),
    autocomplete_begining_alternate_value('name', 'AlternateValue::Abbreviation'),

    autocomplete_ordered_wildcard_pieces_in_name,
    autocomplete_ordered_wildcard_alternate_value,
    autocomplete_ordered_wildcard_alternate_value('name', 'AlternateValue::Translation'),
    autocomplete_ordered_wildcard_alternate_value('name', 'AlternateValue::Abbreviation')
  ]

  queries.compact!

  updated_queries = []
  queries.each_with_index do |q ,i|
    # a = q.joins(:project_sources).where(member_of_project_id.to_sql) if project_id && limit_to_project
    a ||= q
    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..19]
end

- (Object) autocomplete_begining_alternate_value(attribute = 'name', type = 'AlternateValue')



90
91
92
93
94
95
96
# File 'lib/queries/serial/autocomplete.rb', line 90

def autocomplete_begining_alternate_value(attribute = 'name', type = 'AlternateValue')
  ::Serial.joins(:alternate_values).where(
    alternate_value_table[:type].eq(type).and(
    alternate_value_table[:alternate_value_object_attribute].eq(attribute).
    and(alternate_value_table[:value].matches(query_string + '%'))).to_sql
  ).limit(5)
end

- (ActiveRecord::Relation) autocomplete_begining_name

Returns match begining of name

Returns:

  • (ActiveRecord::Relation)

    match begining of name



61
62
63
64
# File 'lib/queries/serial/autocomplete.rb', line 61

def autocomplete_begining_name
  a = table[:name].matches(query_string + '%') 
  base_query.where(a.to_sql).limit(10)
end

- (Object) autocomplete_exact_alternate_value(attribute = 'name', type = 'AlternateValue')



82
83
84
85
86
87
88
# File 'lib/queries/serial/autocomplete.rb', line 82

def autocomplete_exact_alternate_value(attribute = 'name', type = 'AlternateValue')
  ::Serial.joins(:alternate_values).where(
    alternate_value_table[:type].eq(type).and(
    alternate_value_table[:alternate_value_object_attribute].eq(attribute).
    and(alternate_value_table[:value].eq(query_string))).to_sql
  ).limit(5)
end

- (ActiveRecord::Relation) autocomplete_exact_name

Returns match exact name

Returns:

  • (ActiveRecord::Relation)

    match exact name



54
55
56
57
# File 'lib/queries/serial/autocomplete.rb', line 54

def autocomplete_exact_name
  a = table[:name].eq(query_string) 
  base_query.where(a.to_sql).limit(4)
end

- (Object) autocomplete_ordered_wildcard_alternate_value(attribute = 'name', type = 'AlternateValue')

TODO: these three should be factored out to just the join, made more Arel like



74
75
76
77
78
79
80
# File 'lib/queries/serial/autocomplete.rb', line 74

def autocomplete_ordered_wildcard_alternate_value(attribute = 'name', type = 'AlternateValue')
  ::Serial.joins(:alternate_values).where(
    alternate_value_table[:type].eq(type).and(
    alternate_value_table[:alternate_value_object_attribute].eq(attribute).
    and(alternate_value_table[:value].matches(wildcard_pieces))).to_sql
  ).limit(5)
end

- (ActiveRecord::Relation) autocomplete_ordered_wildcard_pieces_in_name

Returns:

  • (ActiveRecord::Relation)


67
68
69
70
# File 'lib/queries/serial/autocomplete.rb', line 67

def autocomplete_ordered_wildcard_pieces_in_name
  a = table[:name].matches(wildcard_pieces)
  base_query.where( a.to_sql).limit(6)
end

- (Scope) base_query

Returns:

  • (Scope)


11
12
13
# File 'lib/queries/serial/autocomplete.rb', line 11

def base_query
  ::Serial.select('serials.*')
end

- (Arel::Table) table

Returns:

  • (Arel::Table)


112
113
114
# File 'lib/queries/serial/autocomplete.rb', line 112

def table
  ::Serial.arel_table
end