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, #alphanumeric_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, #levenshtein_distance, #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, #string_fragments, #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)


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)


120
121
122
# File 'lib/queries/serial/autocomplete.rb', line 120

def alternate_value_table
  ::AlternateValue.arel_table
end

#autocompleteArray

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
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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
    if project_id
      # a = q.joins(:project_sources).where(member_of_project_id.to_sql) if project_id && limit_to_project
      a = a.select('serials.*, COUNT(project_sources.source_id) AS use_count, NULLIF(project_sources.project_id, NULL) as in_project')
           .left_outer_joins(:sources)
           .joins('LEFT OUTER JOIN project_sources ON sources.id = project_sources.source_id')
           .where('project_sources.project_id = ? OR project_sources.project_id IS NULL', project_id)
           .group('serials.id, project_sources.project_id')
           .order('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_begining_alternate_value(attribute = 'name', type = 'AlternateValue') ⇒ Object



103
104
105
106
107
108
109
# File 'lib/queries/serial/autocomplete.rb', line 103

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

#autocomplete_begining_nameActiveRecord::Relation

Returns match begining of name.

Returns:

  • (ActiveRecord::Relation)

    match begining of name



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

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

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



95
96
97
98
99
100
101
# File 'lib/queries/serial/autocomplete.rb', line 95

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

#autocomplete_exact_nameActiveRecord::Relation

Returns match exact name.

Returns:

  • (ActiveRecord::Relation)

    match exact name



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

def autocomplete_exact_name
  a = table[:name].eq(query_string) 
  base_query.where(a.to_sql).limit(4)
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



87
88
89
90
91
92
93
# File 'lib/queries/serial/autocomplete.rb', line 87

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

#autocomplete_ordered_wildcard_pieces_in_nameActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


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

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

#base_queryScope

Returns:

  • (Scope)


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

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

#tableArel::Table

Returns:

  • (Arel::Table)


125
126
127
# File 'lib/queries/serial/autocomplete.rb', line 125

def table
  ::Serial.arel_table
end