Class: Repository::Autocomplete
- Inherits:
-
Query::Autocomplete
- Object
- Query::Autocomplete
- Repository::Autocomplete
- Includes:
- Queries::Concerns::AlternateValues, Queries::Concerns::Identifiers
- Defined in:
- lib/queries/repository/autocomplete.rb
Instance Method Summary collapse
- #autocomplete ⇒ Array
- #autocomplete_acronym_match ⇒ Scope
- #autocomplete_alternate_values_acronym ⇒ Object
- #autocomplete_alternate_values_name ⇒ Object
- #autocomplete_exact_acronym ⇒ Scope
-
#initialize(string, **params) ⇒ Autocomplete
constructor
A new instance of Autocomplete.
Constructor Details
#initialize(string, **params) ⇒ Autocomplete
Returns a new instance of Autocomplete.
9 10 11 12 13 |
# File 'lib/queries/repository/autocomplete.rb', line 9 def initialize(string, **params) set_identifier_params(params) set_alternate_value(params) super end |
Instance Method Details
#autocomplete ⇒ Array
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 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/queries/repository/autocomplete.rb', line 38 def autocomplete t = Time.now return [] if query_string.blank? queries = [ [autocomplete_exact_id, nil ], [autocomplete_exactly_named, true], [autocomplete_exact_acronym, true], [autocomplete_identifier_identifier_exact, nil], [autocomplete_acronym_match.limit(10), true], [autocomplete_named, true ], [autocomplete_alternate_values_acronym.limit(20), true ], [autocomplete_alternate_values_name.limit(20), true ] ] queries.delete_if{|a,b| a.nil?} # Note this pattern differs because [[]] so we don't use compact. /lib/queries/source/autocomplete.rb follows same pattern result = [] pr_id = project_id.join(',') if project_id queries.each do |q, scope| a = q if project_id && scope && query_string.length > 2 a = a.select("repositories.*, COUNT(collection_objects.id) AS use_count, CASE WHEN collection_objects.project_id IN (#{pr_id}) THEN collection_objects.project_id ELSE NULL END AS in_project") .joins('LEFT OUTER JOIN collection_objects ON (repositories.id = collection_objects.repository_id OR repositories.id = collection_objects.current_repository_id)') .group('repositories.id, collection_objects.project_id') .order('in_project, use_count DESC') end a ||= q result += a.to_a result.uniq! break if result.count > 40 end t2 = Time.now.to_f - t.to_f puts t2 result[0..40] end |
#autocomplete_acronym_match ⇒ Scope
16 17 18 19 20 |
# File 'lib/queries/repository/autocomplete.rb', line 16 def autocomplete_acronym_match base_query.where( table[:acronym].matches_any(terms).to_sql ) end |
#autocomplete_alternate_values_acronym ⇒ Object
29 30 31 |
# File 'lib/queries/repository/autocomplete.rb', line 29 def autocomplete_alternate_values_acronym matching_alternate_value_on(:acronym) end |
#autocomplete_alternate_values_name ⇒ Object
33 34 35 |
# File 'lib/queries/repository/autocomplete.rb', line 33 def autocomplete_alternate_values_name matching_alternate_value_on(:name) end |
#autocomplete_exact_acronym ⇒ Scope
23 24 25 26 27 |
# File 'lib/queries/repository/autocomplete.rb', line 23 def autocomplete_exact_acronym base_query.where( table[:acronym].eq(query_string).to_sql ) end |