Class: Queries::Person::Autocomplete

Inherits:
Query
  • Object
show all
Defined in:
lib/queries/person/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_exact_id, #autocomplete_ordered_wildcard_pieces_in_cached, #autocomplete_start_date, #build_terms, #cached, #combine_or_clauses, #end_wildcard, #exactly_named, #fragments, #integers, #match_ordered_wildcard_pieces_in_cached, #match_wildcard_end_in_cached, #match_wildcard_in_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_cached, #with_cached_like, #with_id, #with_project_id, #with_start_date, #year_letter, #years

Constructor Details

- (Autocomplete) initialize(string, roles: :all)

Returns a new instance of Autocomplete

Parameters:

  • args (Hash)


13
14
15
16
# File 'lib/queries/person/autocomplete.rb', line 13

def initialize(string, roles: :all)
  @limit_to_roles = roles
  super
end

Instance Attribute Details

- (Array) limit_to_roles

Parameters:

  • limit_to_role (String)

    any Role class, like `TaxonNameAuthor`, `SourceAuthor`, `SourceEditor`, `Collector` … etc.

Returns:

  • (Array)


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

def limit_to_roles
  @limit_to_roles
end

Instance Method Details

- (Array) autocomplete

Returns:

  • (Array)


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/queries/person/autocomplete.rb', line 81

def autocomplete
  return [] if query_string.blank?
  queries = [
    autocomplete_exact_match,
    autocomplete_exact_inverted,
    autocomplete_exact_last_name_match,
    autocomplete_ordered_wildcard_pieces_in_cached,
    autocomplete_cached_wildcard_anywhere, # in Queries::Query
    autocomplete_cached
  ]

  queries.compact!

  updated_queries = []
  queries.each_with_index do |q ,i|
    a = q.joins(:roles).where(role_match.to_sql) if roles_assigned?
    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

- (Scope) autocomplete_exact_inverted

Returns:

  • (Scope)


49
50
51
52
53
# File 'lib/queries/person/autocomplete.rb', line 49

def autocomplete_exact_inverted
  base_query.where(
    table[:cached].eq(invert_name).to_sql
  ).limit(20)
end

- (Object) autocomplete_exact_last_name_match



42
43
44
45
46
# File 'lib/queries/person/autocomplete.rb', line 42

def autocomplete_exact_last_name_match
  base_query.where(
    table[:last_name].eq(query_string).to_sql
  ).limit(20)
end

- (Scope) autocomplete_exact_match

Returns:

  • (Scope)


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

def autocomplete_exact_match
  base_query.where(
    table[:cached].eq(normalize_name).to_sql
  ).limit(20)
end

- (Scope) base_query

Returns:

  • (Scope)


19
20
21
# File 'lib/queries/person/autocomplete.rb', line 19

def base_query
  ::Person.select('people.*')
end

- (String) invert_name

Returns simple name inversion given `Sarah Smith` return `Smith, Sarah`

Returns:

  • (String)

    simple name inversion given `Sarah Smith` return `Smith, Sarah`



64
65
66
67
68
# File 'lib/queries/person/autocomplete.rb', line 64

def invert_name
  normalize(
    query_string.split(/\s+/, 2).reverse.map(&:strip).join(', ')
  )
end

- (String) normalize(string)

TODO: Use bibtex parser!!

Parameters:

  • string (String)

Returns:

  • (String)


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

def normalize(string)
  string.strip.split(/\s*\,\s*/, 2).join(', ')
end

- (String) normalize_name

Returns:

  • (String)


71
72
73
# File 'lib/queries/person/autocomplete.rb', line 71

def normalize_name
  normalize(query_string)
end

- (Arel::Nodes::Equatity) role_match

Returns:

  • (Arel::Nodes::Equatity)


24
25
26
27
28
# File 'lib/queries/person/autocomplete.rb', line 24

def role_match
  a = roles_table[:type].eq_any(limit_to_roles)
  a = a.and(roles_table[:project_id].eq(project_id)) if !project_id.blank?
  a
end

- (Arel::Nodes::Equatity) role_project_match

Returns:

  • (Arel::Nodes::Equatity)


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

def role_project_match
  roles_table[:project_id].eq(project_id)
end

- (Boolean) roles_assigned?

Returns:

  • (Boolean)


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

def roles_assigned?
  limit_to_roles.kind_of?(Array) && limit_to_roles.any?
end

- (Arel::Table) roles_table

Returns:

  • (Arel::Table)


116
117
118
# File 'lib/queries/person/autocomplete.rb', line 116

def roles_table
  ::Role.arel_table
end

- (Arel::Table) table

Returns:

  • (Arel::Table)


111
112
113
# File 'lib/queries/person/autocomplete.rb', line 111

def table
  ::Person.arel_table
end