Class: Queries::Source::Autocomplete

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

Instance Attribute Summary (collapse)

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, #wildcard_wrapped_years, #with_id, #with_identifier, #with_identifier_like, #with_project_id, #with_start_date, #year_letter, #years

Constructor Details

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

Returns a new instance of Autocomplete

Parameters:

  • args (Hash)


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

def initialize(string, project_id: nil, limit_to_project: false)
  @limit_to_project = limit_to_project
  super
end

Instance Attribute Details

- (Boolean) limit_to_project

Either match against all Sources (default) or just those with ProjectSource

Parameters:

  • limit_to_project (String)

    `true` or `false`

Returns:

  • (Boolean)


8
9
10
# File 'lib/queries/source/autocomplete.rb', line 8

def limit_to_project
  @limit_to_project
end

Instance Method Details

- (String) author_from_author_year

Returns:

  • (String)


145
146
147
# File 'lib/queries/source/autocomplete.rb', line 145

def author_from_author_year
  query_string.match(/^(.+?)\W/).to_a.last
end

- (Array) autocomplete

Returns:

  • (Array)


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/queries/source/autocomplete.rb', line 167

def autocomplete
  queries = [
    autocomplete_exact_author_year_letter,
    autocomplete_exact_author_year,
    autocomplete_wildcard_author_exact_year,
    autocomplete_exact_author,
    autocomplete_start_of_author,  
    autocomplete_wildcard_anywhere_exact_year,
    autocomplete_ordered_wildcard_pieces_in_cached,
    autocomplete_cached_wildcard_anywhere,
    autocomplete_start_of_title
  ]

  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

- (ActiveRecord::Relation) autocomplete_any_author

Returns author matches any full word exactly

Returns:

  • (ActiveRecord::Relation)

    author matches any full word exactly



30
31
32
33
# File 'lib/queries/source/autocomplete.rb', line 30

def autocomplete_any_author
  a = table[:cached_author_string].matches_regexp('\m' + query_string + '\M')
  base_query.where(a.to_sql).limit(20)
end

- (ActiveRecord::Relation) autocomplete_exact_author

Returns if and only iff author string matches

Returns:

  • (ActiveRecord::Relation)

    if and only iff author string matches



23
24
25
26
# File 'lib/queries/source/autocomplete.rb', line 23

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

- (ActiveRecord::Relation?) autocomplete_exact_author_year

Returns:

  • (ActiveRecord::Relation, nil)


93
94
95
96
97
98
99
# File 'lib/queries/source/autocomplete.rb', line 93

def autocomplete_exact_author_year
  a = match_exact_author
  b = match_year
  return nil if a.nil? || b.nil?
  c = a.and(b)
  base_query.where(c.to_sql).limit(10)
end

- (ActiveRecord::Relation?) autocomplete_exact_author_year_letter

Returns:

  • (ActiveRecord::Relation, nil)


83
84
85
86
87
88
89
90
# File 'lib/queries/source/autocomplete.rb', line 83

def autocomplete_exact_author_year_letter
  a = match_exact_author
  b = match_year_suffix
  c = match_year
  return nil if [a,b,c].include?(nil)
  d = a.and(b).and(c)
  base_query.where(d.to_sql).limit(2)
end

- (ActiveRecord::Relation) autocomplete_partial_author

Returns author matches partial string

Returns:

  • (ActiveRecord::Relation)

    author matches partial string



44
45
46
47
# File 'lib/queries/source/autocomplete.rb', line 44

def autocomplete_partial_author
  a = table[:cached_author_string].matches('%' + query_string + '%')
  base_query.where(a.to_sql).limit(5)
end

- (ActiveRecord::Relation) autocomplete_start_of_author

Returns author matches start

Returns:

  • (ActiveRecord::Relation)

    author matches start



37
38
39
40
# File 'lib/queries/source/autocomplete.rb', line 37

def autocomplete_start_of_author
  a = table[:cached_author_string].matches(query_string + '%')
  base_query.where(a.to_sql).limit(8)
end

- (ActiveRecord::Relation) autocomplete_start_of_title

Returns title matches start

Returns:

  • (ActiveRecord::Relation)

    title matches start



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

def autocomplete_start_of_title
  a = table[:title].matches(query_string + '%')
  base_query.where(a.to_sql).limit(5)
end

- (ActiveRecord::Relation?) autocomplete_wildcard_anywhere_exact_year

Returns:

  • (ActiveRecord::Relation, nil)


111
112
113
114
115
116
117
# File 'lib/queries/source/autocomplete.rb', line 111

def autocomplete_wildcard_anywhere_exact_year
  a = match_year
  b = match_wildcard_cached
  return nil if a.nil? || b.nil?
  c = a.and(b)
  base_query.where(c.to_sql).limit(10)
end

- (ActiveRecord::Relation?) autocomplete_wildcard_author_exact_year

Returns:

  • (ActiveRecord::Relation, nil)


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

def autocomplete_wildcard_author_exact_year
  a = match_year
  b = match_wildcard_author
  return nil if a.nil? || b.nil?
  c = a.and(b)
  base_query.where(c.to_sql).limit(10)
end

- (ActiveRecord::Relation) autocomplete_wildcard_pieces_and_year

Returns author matches partial string

Returns:

  • (ActiveRecord::Relation)

    author matches partial string



65
66
67
68
69
70
71
# File 'lib/queries/source/autocomplete.rb', line 65

def autocomplete_wildcard_pieces_and_year
  a = match_ordered_wildcard_pieces_in_cached
  b = match_year
  return nil if a.nil? || b.nil?
  c = a.and(b)
  base_query.where(c.to_sql).limit(5)
end

- (ActiveRecord::Relation) autocomplete_year

Returns multi-year match? otherwise pointless

Returns:

  • (ActiveRecord::Relation)

    multi-year match? otherwise pointless



51
52
53
54
# File 'lib/queries/source/autocomplete.rb', line 51

def autocomplete_year
  a = table[:year].eq_any(years)
  base_query.where(a.to_sql).limit(5)
end

- (ActiveRecord::Relation?) autocomplete_year_letter

Returns:

  • (ActiveRecord::Relation, nil)


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

def autocomplete_year_letter
  a = match_year
  b = match_year_suffix
  return nil if a.nil? || b.nil?
  c = a.and(b)
  base_query.where(c.to_sql).limit(10)
end

- (Scope) base_query

Returns:

  • (Scope)


17
18
19
# File 'lib/queries/source/autocomplete.rb', line 17

def base_query
  ::Source.select('sources.*')
end

- (ActiveRecord::Relation?) fragment_year_matches

Returns if user provides 5 or fewer strings and any number of years look for any string && year

Returns:

  • (ActiveRecord::Relation, nil)

    if user provides 5 or fewer strings and any number of years look for any string && year



156
157
158
159
160
161
162
163
164
# File 'lib/queries/source/autocomplete.rb', line 156

def fragment_year_matches
  if fragments.any?
    s = table[:cached].matches_any(fragments)
    s = s.and(table[:year].eq_any(years)) if !years.empty?
    s
  else
    nil
  end
end

- (Arel::Nodes::Equatity) match_exact_author

Returns:

  • (Arel::Nodes::Equatity)


128
129
130
# File 'lib/queries/source/autocomplete.rb', line 128

def match_exact_author
  table[:cached_author_string].eq(author_from_author_year)
end

- (Arel::Nodes::Matches) match_wildcard_author

match ALL wildcards, but unordered, if 2 - 6 pieces provided

Returns:

  • (Arel::Nodes::Matches)


121
122
123
124
125
# File 'lib/queries/source/autocomplete.rb', line 121

def match_wildcard_author
  b = fragments
  return nil if b.empty?
  a = table[:cached_author_string].matches_all(b)
end

- (Arel::Nodes::Equatity) match_year

Returns:

  • (Arel::Nodes::Equatity)


138
139
140
141
142
# File 'lib/queries/source/autocomplete.rb', line 138

def match_year
  a = years.first
  return nil if a.nil?
  table[:year].eq(a)
end

- (Arel::Nodes::Equatity) match_year_suffix

Returns:

  • (Arel::Nodes::Equatity)


133
134
135
# File 'lib/queries/source/autocomplete.rb', line 133

def match_year_suffix
  table[:year_suffix].eq(year_letter)
end

- (Arel::Nodes::Equatity) member_of_project_id

Returns:

  • (Arel::Nodes::Equatity)


150
151
152
# File 'lib/queries/source/autocomplete.rb', line 150

def member_of_project_id
  project_sources_table[:project_id].eq(project_id)
end

- (Arel::Table) project_sources_table

Returns:

  • (Arel::Table)


204
205
206
# File 'lib/queries/source/autocomplete.rb', line 204

def project_sources_table
  ProjectSource.arel_table
end

- (Arel::Table) table

Returns:

  • (Arel::Table)


199
200
201
# File 'lib/queries/source/autocomplete.rb', line 199

def table
  ::Source.arel_table
end