Class: Queries::Person::Autocomplete

Inherits:
Query::Autocomplete show all
Includes:
Concerns::AlternateValues, Concerns::Tags
Defined in:
lib/queries/person/autocomplete.rb

Instance Attribute Summary collapse

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, **params) ⇒ Autocomplete

Returns a new instance of Autocomplete.

Parameters:

  • args (Hash)

    !! project_id is required!!



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

def initialize(string, **params)
  @role_type = params[:role_type]
  @in_project = boolean_param(params, :in_project)

  set_tags_params(params)
  set_alternate_value(params)
  super
end

Instance Attribute Details

#in_projectBoolean?

Returns true - restrict to only people used in this project nil, false - ignored.

Returns:

  • (Boolean, nil)

    true - restrict to only people used in this project nil, false - ignored



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

def in_project
  @in_project
end

#role_typeArray

Parameters:

  • limit_to_role (String, Array)

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

Returns:

  • (Array)


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

def role_type
  @role_type
end

Instance Method Details

#autocompleteArray

Returns:

  • (Array)


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/queries/person/autocomplete.rb', line 99

def autocomplete
  return [] if query_string.blank? || project_id.empty?

  queries = [
    [ autocomplete_exact_id, false ],
    [ autocomplete_exact_match&.limit(5), true ],
    [ autocomplete_exact_inverted&.limit(5), true ],
    [ autocomplete_identifier_cached_exact, false ],
    [ autocomplete_identifier_identifier_exact, false ],
    [ autocomplete_exact_last_name_match.limit(20), true ],
    [ autocomplete_alternate_values_last_name.limit(20), true ],
    [ autocomplete_alternate_values_first_name.limit(20), true ],
    [ autocomplete_ordered_wildcard_pieces_in_cached&.limit(5), true ],
    [ autocomplete_cached_wildcard_anywhere&.limit(20), true ], # in Queries::Query::Autocomplete
    [ autocomplete_cached.limit(20), true ]
  ]

  queries.compact!

  updated_queries = []

  pr_id = project_id.join(',') if project_id
  queries.each_with_index do |q, i|
    a = q[0]

    if !a.nil?
      if role_type.present?
        a = a.joins(:roles).where(role_match.to_sql)
      end

      if q[1] && query_string.length > 1 # do not use extended query for identifiers
        if project_id.present?
          a = a.left_outer_joins(:roles)
          .joins("LEFT OUTER JOIN sources ON roles.role_object_id = sources.id AND roles.role_object_type = 'Source'")
          .joins('LEFT OUTER JOIN project_sources ON sources.id = project_sources.source_id')
          .select("people.*, COUNT(roles.id) AS use_count, CASE WHEN roles.project_id IN (#{pr_id}) THEN roles.project_id WHEN project_sources.project_id IN (#{pr_id}) THEN project_sources.project_id ELSE NULL END AS in_project")
          .group('people.id, roles.project_id, project_sources.project_id')
          .order('in_project, use_count DESC')
          #a = a.left_outer_joins(:roles)
          #     .joins("LEFT OUTER JOIN sources ON roles.role_object_id = sources.id AND roles.role_object_type = 'Source'")
          #     .joins('LEFT OUTER JOIN project_sources ON sources.id = project_sources.source_id')
          #     .select("people.*, COUNT(roles.id) AS use_count, CASE WHEN MAX(roles.project_id) IN (#{pr_id}) THEN MAX(roles.project_id) WHEN MAX(project_sources.project_id) IN (#{pr_id}) THEN MAX(project_sources.project_id) ELSE NULL END AS in_project")
          #     .where("roles.project_id IN (#{pr_id}) OR project_sources.project_id IN (#{pr_id}) OR ( (roles.project_id NOT IN (#{pr_id}) OR roles.project_id IS NULL) AND (project_sources.project_id NOT IN (#{pr_id}) OR project_sources.project_id IS NULL))")
          #     .group('people.id')
          #     .order('in_project, use_count DESC')
        end
      end
    end
    updated_queries[i] = a
  end
  result = []
  updated_queries.compact!
  updated_queries.each do |q|
    result += q.to_a
    result.uniq!
    break if result.count > 19
  end
  result = result[0..19]
end

#autocomplete_alternate_values_first_nameObject



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

def autocomplete_alternate_values_first_name
  matching_alternate_value_on(:first_name)
end

#autocomplete_alternate_values_last_nameObject



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

def autocomplete_alternate_values_last_name
  matching_alternate_value_on(:last_name)
end

#autocomplete_exact_invertedScope

Returns:

  • (Scope)


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

def autocomplete_exact_inverted
  a = invert_name
  return nil if a.nil?
  base_query.where(
    table[:cached].eq(a).to_sql
  )
end

#autocomplete_exact_last_name_matchObject



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

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

#autocomplete_exact_matchScope

Returns:

  • (Scope)


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

def autocomplete_exact_match
  a = normalize_name
  return nil if a.nil?
  base_query.where(
    table[:cached].eq(normalize_name).to_sql
  )
end

#invert_nameString

TODO: Use Namae?

Returns:

  • (String)

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



85
86
87
88
89
# File 'lib/queries/person/autocomplete.rb', line 85

def invert_name
  a = normalize( query_string.split(/\s+/, 2).reverse.map(&:strip).join(', ') )
  return nil if a.nil?
  a
end

#normalize(string) ⇒ String?

TODO: Use bibtex parser!!

Parameters:

  • string (String)

Returns:

  • (String, nil)


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

def normalize(string)
  n = string.strip.split(/\s*\,\s*/, 2).join(', ')
  return nil unless n.include?(' ')
  n
end

#normalize_nameString

Returns:

  • (String)


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

def normalize_name
  a = normalize(query_string)
  return nil if a.nil?
  a
end

#role_matchArel::Nodes::Equatity

Returns:

  • (Arel::Nodes::Equatity)


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

def role_match
  a = roles_table[:type].in(role_type)
  a = a.and(roles_table[:project_id].in(project_id)) if in_project
  a
end

#roles_tableArel::Table

Returns:

  • (Arel::Table)


160
161
162
# File 'lib/queries/person/autocomplete.rb', line 160

def roles_table
  ::Role.arel_table
end