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_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, roles: :all)

Returns a new instance of Autocomplete

Parameters:

  • args (Hash)


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

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)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/queries/person/autocomplete.rb', line 76

def autocomplete
  queries = [
    autocomplete_exact_match,
    autocomplete_exact_inverted,
    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)


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

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

- (Scope) autocomplete_exact_match

Returns:

  • (Scope)


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

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

- (Scope) base_query

Returns:

  • (Scope)


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

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`



59
60
61
62
63
# File 'lib/queries/person/autocomplete.rb', line 59

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)


53
54
55
# File 'lib/queries/person/autocomplete.rb', line 53

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

- (String) normalize_name

Returns:

  • (String)


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

def normalize_name
  normalize(query_string)
end

- (Arel::Nodes::Equatity) role_match

Returns:

  • (Arel::Nodes::Equatity)


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

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)


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

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

- (Boolean) roles_assigned?

Returns:

  • (Boolean)


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

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

- (Arel::Table) roles_table

Returns:

  • (Arel::Table)


109
110
111
# File 'lib/queries/person/autocomplete.rb', line 109

def roles_table
  ::Role.arel_table
end

- (Arel::Table) table

Returns:

  • (Arel::Table)


104
105
106
# File 'lib/queries/person/autocomplete.rb', line 104

def table
  ::Person.arel_table
end