Class: Queries::Serial::Autocomplete

Inherits:
Query::Autocomplete show all
Defined in:
lib/queries/serial/autocomplete.rb

Instance Attribute Summary

Attributes inherited from Query::Autocomplete

#dynamic_limit, #project_id, #query_string

Attributes inherited from Query

#query_string, #terms

Instance Method Summary collapse

Methods inherited from Query::Autocomplete

#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, #combine_or_clauses, #common_name_name, #common_name_table, #common_name_wild_pieces, #exactly_named, #fragments, #integers, #least_levenshtein, #match_wildcard_end_in_cached, #match_wildcard_in_cached, #named, #only_ids, #only_integers?, #parent, #parent_child_join, #parent_child_where, #pieces, #scope, #string_fragments, #wildcard_wrapped_integers, #wildcard_wrapped_years, #with_cached, #with_cached_like, #with_id, #with_project_id, #year_letter, #years

Methods inherited from Query

#alphabetic_strings, #alphanumeric_strings, base_name, #base_name, #base_query, #build_terms, #cached_facet, #end_wildcard, #levenshtein_distance, #match_ordered_wildcard_pieces_in_cached, #no_terms?, referenced_klass, #referenced_klass, #referenced_klass_except, #referenced_klass_intersection, #referenced_klass_union, #start_and_end_wildcard, #start_wildcard, #table, #wildcard_pieces

Constructor Details

#initialize(string, project_id: nil) ⇒ Autocomplete

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)
  super
end

Instance Method Details

#alternate_value_tableArel::Table

Returns:

  • (Arel::Table)


115
116
117
# File 'lib/queries/serial/autocomplete.rb', line 115

def alternate_value_table
  ::AlternateValue.arel_table
end

#autocompleteArray

Returns:

  • (Array)


11
12
13
14
15
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
51
52
53
54
55
56
57
58
# File 'lib/queries/serial/autocomplete.rb', line 11

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_beginning_name,
    #autocomplete_beginning_alternate_value,
    autocomplete_beginning_alternate_value('name', 'AlternateValue::Translation'),
    autocomplete_beginning_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 = []
 pr_id = project_id.join(',') if project_id
 queries.each_with_index do |q ,i|
   a = q
   if project_id
     a = a.select("serials.*, COUNT(project_sources.source_id) AS use_count, CASE WHEN project_sources.project_id IN (#{pr_id}) THEN project_sources.project_id ELSE NULL END AS in_project")
          .left_outer_joins(:sources)
          .joins('LEFT OUTER JOIN project_sources ON sources.id = project_sources.source_id')
          .group('serials.id, project_sources.project_id')
          .order('in_project, use_count DESC')
   end
   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
  if result.first.try(:use_count).nil?
    return result[0..19]
  else
    return result.sort_by{|i| -i.use_count}[0..19]
  end
end

#autocomplete_beginning_alternate_value(attribute = 'name', type = 'AlternateValue') ⇒ Object



98
99
100
101
102
103
104
# File 'lib/queries/serial/autocomplete.rb', line 98

def autocomplete_beginning_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(20)
end

#autocomplete_beginning_nameActiveRecord::Relation

Returns match beginning of name.

Returns:

  • (ActiveRecord::Relation)

    match beginning of name



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

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

#autocomplete_exact_alternate_value(attribute = 'name', type = 'AlternateValue') ⇒ Object



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

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(20)
end

#autocomplete_exact_nameActiveRecord::Relation

Returns match exact name.

Returns:

  • (ActiveRecord::Relation)

    match exact name



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

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

#autocomplete_ordered_wildcard_alternate_value(attribute = 'name', type = 'AlternateValue') ⇒ Object

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



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

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(10)
end

#autocomplete_ordered_wildcard_pieces_in_nameActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


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

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