From fb0b31c67e36b216d924a4197c2fc34a7bc5f9e8 Mon Sep 17 00:00:00 2001 From: Bethany Connor Date: Tue, 13 Oct 2020 22:08:27 +0000 Subject: [PATCH 1/4] Re-add resource autocomplete --- dashboard/lib/resources_autocomplete.rb | 12 ++++++++++ dashboard/scripts/generate_fixtures.rb | 2 ++ dashboard/test/fixtures/resource.yml | 20 ++++++++++++++++ .../test/lib/resources_autocomplete_test.rb | 24 +++++++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 dashboard/lib/resources_autocomplete.rb create mode 100644 dashboard/test/fixtures/resource.yml create mode 100644 dashboard/test/lib/resources_autocomplete_test.rb diff --git a/dashboard/lib/resources_autocomplete.rb b/dashboard/lib/resources_autocomplete.rb new file mode 100644 index 0000000000000..2840b8a816483 --- /dev/null +++ b/dashboard/lib/resources_autocomplete.rb @@ -0,0 +1,12 @@ +class ResourcesAutocomplete < AutocompleteHelper + def self.get_search_matches(query, limit) + limit = format_limit(limit) + + rows = Resource.limit(limit) + query = format_query(query) + return [] if query.length < MIN_WORD_LENGTH + 2 + rows = rows. + where("MATCH(name,url) AGAINST(? in BOOLEAN MODE)", query) + return rows.map(&:attributes) + end +end diff --git a/dashboard/scripts/generate_fixtures.rb b/dashboard/scripts/generate_fixtures.rb index accabe677b357..24168f111a0b0 100644 --- a/dashboard/scripts/generate_fixtures.rb +++ b/dashboard/scripts/generate_fixtures.rb @@ -47,6 +47,7 @@ @level_concept_difficulty = {} @level_sources = {} @callouts = {} +@resources = {} def handle_level(level) attributes = level.attributes.clone @@ -140,3 +141,4 @@ def yamlize(hsh) File.new("#{prefix}level_concept_difficulty.yml", 'w').write(yamlize(@level_concept_difficulty)) File.new("#{prefix}level_source.yml", 'w').write(yamlize(@level_sources)) File.new("#{prefix}callout.yml", 'w').write(yamlize(@callouts)) +File.new("#{prefix}resource.yml", 'w').write(yamlize(@resources)) diff --git a/dashboard/test/fixtures/resource.yml b/dashboard/test/fixtures/resource.yml new file mode 100644 index 0000000000000..b284815a0051d --- /dev/null +++ b/dashboard/test/fixtures/resource.yml @@ -0,0 +1,20 @@ +resource_101: + id: 101 + key: resource_101 + name: Code Studio + url: code.org +resource_102: + id: 102 + key: resource_102 + name: wiki + url: wikipedia.org +resource_103: + id: 103 + key: resource_103 + name: class slides + url: docs.google.com/slides +resource_104: + id: 104 + key: resource_104 + name: class site + url: testsebsite.fake diff --git a/dashboard/test/lib/resources_autocomplete_test.rb b/dashboard/test/lib/resources_autocomplete_test.rb new file mode 100644 index 0000000000000..734a48caf45fb --- /dev/null +++ b/dashboard/test/lib/resources_autocomplete_test.rb @@ -0,0 +1,24 @@ +require 'test_helper' + +class ResourcesAutocompleteTest < ActiveSupport::TestCase + # We rely on fulltext indices to be used in this test. + # In order to get this test to work, we need to have the resources created + # as fixtures. These are defined in test/fixtures/resource.yml + + test "finds resource with matching name" do + matches = ResourcesAutocomplete.get_search_matches('Studi', 5) + assert_equal 1, matches.length + assert_equal 'Code Studio', matches[0]['name'] + end + + test "finds resource with matching url" do + matches = ResourcesAutocomplete.get_search_matches("wikip", 5) + assert_equal 1, matches.length + assert_equal 'wiki', matches[0]['name'] + end + + test "finds multiple matches" do + matches = ResourcesAutocomplete.get_search_matches("class", 5) + assert_equal 2, matches.length + end +end From b121c20ea1f66cc90f0c756bae82229b4a3526b6 Mon Sep 17 00:00:00 2001 From: Bethany Connor Date: Tue, 13 Oct 2020 22:12:02 +0000 Subject: [PATCH 2/4] Actually use the resources autocomplete --- dashboard/app/controllers/resources_controller.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dashboard/app/controllers/resources_controller.rb b/dashboard/app/controllers/resources_controller.rb index c2d4c3b9f80a4..6472b06fe760c 100644 --- a/dashboard/app/controllers/resources_controller.rb +++ b/dashboard/app/controllers/resources_controller.rb @@ -1,7 +1,6 @@ class ResourcesController < ApplicationController # GET /resourcesearch/:q/:limit def search - # TODO: use the query to provide real suggestions - render json: Resource.limit(params[:limit]).map(&:attributes) + render json: ResourcesAutocomplete.get_search_matches(params[:q], params[:limit]) end end From c067d9df5c3af44e7b0ce5acfd89b1875d0fa352 Mon Sep 17 00:00:00 2001 From: Bethany Connor Date: Wed, 14 Oct 2020 15:47:37 +0000 Subject: [PATCH 3/4] Remove unneeded change --- dashboard/scripts/generate_fixtures.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/dashboard/scripts/generate_fixtures.rb b/dashboard/scripts/generate_fixtures.rb index 24168f111a0b0..accabe677b357 100644 --- a/dashboard/scripts/generate_fixtures.rb +++ b/dashboard/scripts/generate_fixtures.rb @@ -47,7 +47,6 @@ @level_concept_difficulty = {} @level_sources = {} @callouts = {} -@resources = {} def handle_level(level) attributes = level.attributes.clone @@ -141,4 +140,3 @@ def yamlize(hsh) File.new("#{prefix}level_concept_difficulty.yml", 'w').write(yamlize(@level_concept_difficulty)) File.new("#{prefix}level_source.yml", 'w').write(yamlize(@level_sources)) File.new("#{prefix}callout.yml", 'w').write(yamlize(@callouts)) -File.new("#{prefix}resource.yml", 'w').write(yamlize(@resources)) From 5e7cec366dd1d0fda3ccf60342feec3a8df819f0 Mon Sep 17 00:00:00 2001 From: Bethany Connor Date: Wed, 14 Oct 2020 22:34:08 +0000 Subject: [PATCH 4/4] Add a couple of tests and clarify some code --- dashboard/lib/resources_autocomplete.rb | 2 +- dashboard/test/lib/resources_autocomplete_test.rb | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/dashboard/lib/resources_autocomplete.rb b/dashboard/lib/resources_autocomplete.rb index 2840b8a816483..429e911bb439f 100644 --- a/dashboard/lib/resources_autocomplete.rb +++ b/dashboard/lib/resources_autocomplete.rb @@ -3,8 +3,8 @@ def self.get_search_matches(query, limit) limit = format_limit(limit) rows = Resource.limit(limit) + return [] if query.length < MIN_WORD_LENGTH query = format_query(query) - return [] if query.length < MIN_WORD_LENGTH + 2 rows = rows. where("MATCH(name,url) AGAINST(? in BOOLEAN MODE)", query) return rows.map(&:attributes) diff --git a/dashboard/test/lib/resources_autocomplete_test.rb b/dashboard/test/lib/resources_autocomplete_test.rb index 734a48caf45fb..063dc8ee5ba7f 100644 --- a/dashboard/test/lib/resources_autocomplete_test.rb +++ b/dashboard/test/lib/resources_autocomplete_test.rb @@ -21,4 +21,14 @@ class ResourcesAutocompleteTest < ActiveSupport::TestCase matches = ResourcesAutocomplete.get_search_matches("class", 5) assert_equal 2, matches.length end + + test "only returns up to limit matches" do + matches = ResourcesAutocomplete.get_search_matches("class", 1) + assert_equal 1, matches.length + end + + test "returns empty list for short query" do + matches = ResourcesAutocomplete.get_search_matches("cl", 5) + assert_equal [], matches + end end