From f851f2396dd9893659b554872acf39ef1ea06ed9 Mon Sep 17 00:00:00 2001 From: Stefanni Date: Tue, 14 Aug 2018 21:52:05 -0700 Subject: [PATCH 1/7] Add field username to profiles endpoint to be used on the autocomplete call --- app/api/srch/search.rb | 7 ++++--- app/api/srch/shared_params.rb | 4 ++++ app/assets/javascripts/atwho_autocomplete.js | 2 +- app/models/user.rb | 4 ++++ app/services/search_criteria.rb | 5 +++-- app/services/search_service.rb | 7 ++++++- app/services/srch_scope.rb | 11 +++++++++++ doc/API.md | 11 +++++++++++ 8 files changed, 44 insertions(+), 7 deletions(-) diff --git a/app/api/srch/search.rb b/app/api/srch/search.rb index 4cf1e9739a..24650e72b6 100644 --- a/app/api/srch/search.rb +++ b/app/api/srch/search.rb @@ -21,14 +21,14 @@ class Search < Grape::API Search.execute(:all, params) end - # Request URL should be /api/srch/profiles?srchString=QRY[&sort_by=recent&order_direction=desc&seq=KEYCOUNT&showCount=NUM_ROWS&pageNum=PAGE_NUM] + # Request URL should be /api/srch/profiles?srchString=QRY[&sort_by=recent&order_direction=desc&field=username&seq=KEYCOUNT&showCount=NUM_ROWS&pageNum=PAGE_NUM] # Basic implementation from classic plots2 SearchController desc 'Perform a search of profiles', hidden: false, is_array: false, nickname: 'srchGetProfiles' params do - use :common, :sorting, :ordering + use :common, :sorting, :ordering, :field end get :profiles do Search.execute(:profiles, params) @@ -107,8 +107,9 @@ def self.execute(endpoint, params) tag_query = params[:tagName] order_query = params[:order_direction] sort_query = params[:sort_by] + field_query = params[:field] search_type = endpoint - search_criteria = SearchCriteria.new(search_query, tag: tag_query, sort_by: sort_query, order_direction: order_query) + search_criteria = SearchCriteria.new(search_query, tag: tag_query, sort_by: sort_query, order_direction: order_query, field: field_query) if search_criteria.valid? sresult = ExecuteSearch.new.by(search_type, search_criteria) diff --git a/app/api/srch/shared_params.rb b/app/api/srch/shared_params.rb index 2f948e7118..456d7ebe07 100644 --- a/app/api/srch/shared_params.rb +++ b/app/api/srch/shared_params.rb @@ -23,6 +23,10 @@ module SharedParams optional :sort_by, type: String, documentation: { example: 'recent' } end + params :field do + optional :field, type: String, documentation: { example: 'username' } + end + params :commontypeahead do requires :srchString, type: String, documentation: { example: 'Spec' } optional :seq, type: Integer, documentation: { example: 995 } diff --git a/app/assets/javascripts/atwho_autocomplete.js b/app/assets/javascripts/atwho_autocomplete.js index 6044d8b5be..20e68073bc 100644 --- a/app/assets/javascripts/atwho_autocomplete.js +++ b/app/assets/javascripts/atwho_autocomplete.js @@ -4,7 +4,7 @@ at: "@", callbacks: { remoteFilter: function(query, callback) { - $.getJSON("/api/srch/profiles?srchString=" + query, {}, function(data) { + $.getJSON("/api/srch/profiles?srchString=" + query + "&sort_by=recent&field=username", {}, function(data) { if (data.hasOwnProperty('items') && data.items.length > 0) { callback(data.items.map(function(i) { return i.docTitle })); } diff --git a/app/models/user.rb b/app/models/user.rb index c630bd0790..fa758eb78a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -46,6 +46,10 @@ def self.search(query) User.where('MATCH(bio, username) AGAINST(? IN BOOLEAN MODE)', query + '*') end + def self.search_by_username(query) + User.where('MATCH(username) AGAINST(? IN BOOLEAN MODE)', query + '*') + end + def new_contributor @uid = id return "New Contributor".html_safe if Node.where(:uid => @uid).length === 1 diff --git a/app/services/search_criteria.rb b/app/services/search_criteria.rb index acb82a6e34..ce00164925 100644 --- a/app/services/search_criteria.rb +++ b/app/services/search_criteria.rb @@ -1,11 +1,12 @@ class SearchCriteria - attr_reader :query, :tag, :sort_by + attr_reader :query, :tag, :sort_by, :field - def initialize(query, tag: nil, sort_by: nil, order_direction: "DESC") + def initialize(query, tag: nil, sort_by: nil, order_direction: "DESC", field: nil) @query = query @tag = tag @sort_by = sort_by @order_direction = order_direction + @field = field end def valid? diff --git a/app/services/search_service.rb b/app/services/search_service.rb index 439f502593..af3bda6858 100644 --- a/app/services/search_service.rb +++ b/app/services/search_service.rb @@ -50,7 +50,12 @@ def textSearch_all(search_criteria) # If no sort_by value present, then it returns a list of profiles ordered by id DESC # a recent activity may be a node creation or a node revision def profiles(search_criteria) - user_scope = SrchScope.find_users(search_criteria.query, limit = 10) + user_scope = + if search_criteria.field == "username" + SrchScope.find_by_username(search_criteria.query, limit = 10) + else + SrchScope.find_users(search_criteria.query, limit = 10) + end user_scope = if search_criteria.sort_by == "recent" diff --git a/app/services/srch_scope.rb b/app/services/srch_scope.rb index 09a5a83d4b..1e52818af6 100644 --- a/app/services/srch_scope.rb +++ b/app/services/srch_scope.rb @@ -11,6 +11,17 @@ def self.find_users(query, limit) end end + def self.find_by_username(query, limit) + if ActiveRecord::Base.connection.adapter_name == 'Mysql2' + User.search_by_username(query) + .where('rusers.status = ?', 1) + .limit(limit) + else + User.where('username LIKE ? AND rusers.status = 1', '%' + input + '%') + .limit(limit) + end + end + def self.find_tags(input, limit) tags = Tag.includes(:node) .references(:node) diff --git a/doc/API.md b/doc/API.md index cbb013ef8f..80438136c4 100644 --- a/doc/API.md +++ b/doc/API.md @@ -13,6 +13,17 @@ https://publiclab.org/api/swagger_doc.json Per-model API endpoints are: * Profiles: https://publiclab.org/api/srch/profiles?srchString=foo + +This also accepts the following additional parameters: + +- `sort_by`: if 'recent' is passed, it returns the profiles from users with the most recent activity, otherwise, the results are sorted by user id (desc); +- `order_direction`: `ASC` or `DESC` (the latter is the default); +- `field`: if 'username' is passed, it returns the profiles from users searched by username only, otherwise by username and bio for a broader search. + +Example of full URL with the optional params (using the order_by DESC default value): + +https://publiclab.org/api/srch/profiles?srchString=foo&sort_by=recent&field=username + * Questions: https://publiclab.org/api/srch/questions?srchString=foo * Tags: https://publiclab.org/api/srch/tags?srchString=foo * Notes: https://publiclab.org/api/srch/notes?srchString=foo From 9e1654c3dac5fbae200ef394d1cc7ac2218baefa Mon Sep 17 00:00:00 2001 From: milaaraujo Date: Wed, 15 Aug 2018 20:07:34 -0700 Subject: [PATCH 2/7] Version 1 --- app/services/srch_scope.rb | 4 +-- doc/API.md | 2 +- test/fixtures/drupal_users.yml | 7 ++++ test/fixtures/nodes.yml | 12 +++++++ test/fixtures/revisions.yml | 11 ++++-- test/fixtures/users.yml | 13 +++++++ test/functional/notes_controller_test.rb | 4 +-- test/functional/search_api_test.rb | 44 +++++++++++++++++++----- test/unit/node_test.rb | 3 +- 9 files changed, 83 insertions(+), 17 deletions(-) diff --git a/app/services/srch_scope.rb b/app/services/srch_scope.rb index 1e52818af6..23c06146ab 100644 --- a/app/services/srch_scope.rb +++ b/app/services/srch_scope.rb @@ -6,7 +6,7 @@ def self.find_users(query, limit) .where('rusers.status = ?', 1) .limit(limit) else - User.where('username LIKE ? AND rusers.status = 1', '%' + input + '%') + User.where('username LIKE ? AND rusers.status = 1', '%' + query + '%') .limit(limit) end end @@ -17,7 +17,7 @@ def self.find_by_username(query, limit) .where('rusers.status = ?', 1) .limit(limit) else - User.where('username LIKE ? AND rusers.status = 1', '%' + input + '%') + User.where('username LIKE ? AND rusers.status = 1', '%' + query + '%') .limit(limit) end end diff --git a/doc/API.md b/doc/API.md index 80438136c4..f7bff6a971 100644 --- a/doc/API.md +++ b/doc/API.md @@ -18,7 +18,7 @@ This also accepts the following additional parameters: - `sort_by`: if 'recent' is passed, it returns the profiles from users with the most recent activity, otherwise, the results are sorted by user id (desc); - `order_direction`: `ASC` or `DESC` (the latter is the default); -- `field`: if 'username' is passed, it returns the profiles from users searched by username only, otherwise by username and bio for a broader search. +- `field`: if 'username' is passed, it returns the profiles from users searched by username only. Otherwise it returns the profiles by username and bio for a broader search. Example of full URL with the optional params (using the order_by DESC default value): diff --git a/test/fixtures/drupal_users.yml b/test/fixtures/drupal_users.yml index 43164c9fb3..b6006cab85 100644 --- a/test/fixtures/drupal_users.yml +++ b/test/fixtures/drupal_users.yml @@ -116,3 +116,10 @@ steff3: mail: steff3@pxlshp.com uid: 17 created: <%= Time.now.to_i %> + +data: + name: data + status: 1 + mail: data@pxlshp.com + uid: 18 + created: <%= Time.now.to_i %> diff --git a/test/fixtures/nodes.yml b/test/fixtures/nodes.yml index 64f286e753..7a7bd9330f 100644 --- a/test/fixtures/nodes.yml +++ b/test/fixtures/nodes.yml @@ -294,3 +294,15 @@ post_test3: type: "note" cached_likes: 0 slug: user3-<%= DateTime.new(2018,8,15).strftime("%m-%d-%Y") %>-post-test3 + +post_test4: + nid: 26 + uid: 18 + title: "Post Test 4" + path: "/notes/user4/<%= DateTime.new(2018,8,20).strftime("%m-%d-%Y") %>/post-test4" + created: <%= DateTime.new(2018,8,20).to_i %> + changed: <%= DateTime.new(2018,8,20).to_i %> + status: 1 + type: "note" + cached_likes: 0 + slug: user3-<%= DateTime.new(2018,8,20).strftime("%m-%d-%Y") %>-post-test4 diff --git a/test/fixtures/revisions.yml b/test/fixtures/revisions.yml index f0ac930edd..f120b0e418 100644 --- a/test/fixtures/revisions.yml +++ b/test/fixtures/revisions.yml @@ -295,6 +295,13 @@ post_test2: post_test3: nid: 25 uid: 16 - title: Post test review 2 - body: Post test review 2 + title: Post test review 3 + body: Post test review 3 timestamp: <%= DateTime.new(2018,8,15).to_i %> + +post_test4: + nid: 26 + uid: 18 + title: Post test review 4 + body: Post test review 4 + timestamp: <%= DateTime.new(2018,8,20).to_i %> diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 1d6e4cfc4d..43d92dd7b5 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -210,3 +210,16 @@ steff3: bio: '' created_at: <%= Time.now %> updated_at: <%= Time.now %> + +data: + username: data + status: 1 + email: data@pxlshp.com + id: 18 + password_salt: <%= salt = Authlogic::Random.hex_token %> + crypted_password: <%= Authlogic::CryptoProviders::Sha512.encrypt("secretive" + salt) %> + persistence_token: <%= Authlogic::Random.hex_token %> + last_request_at: <%= Time.now %> + bio: 'data is a nice cat and he loves steff!' + created_at: <%= Time.now %> + updated_at: <%= Time.now %> diff --git a/test/functional/notes_controller_test.rb b/test/functional/notes_controller_test.rb index 191306568f..f8ec575d35 100644 --- a/test/functional/notes_controller_test.rb +++ b/test/functional/notes_controller_test.rb @@ -313,7 +313,7 @@ def teardown assert_response :success selector = css_select 'div.note' - assert_equal selector.size, 20 + assert_equal selector.size, 21 assert_select "div p", 'Pending approval by community moderators. Please be patient!' end @@ -342,7 +342,7 @@ def teardown assert_response :success selector = css_select 'div.note' - assert_equal selector.size, 20 + assert_equal selector.size, 21 assert_select "p", "Moderate first-time post: \n Approve\n Spam" end diff --git a/test/functional/search_api_test.rb b/test/functional/search_api_test.rb index 8d34a7c2f7..527164964c 100644 --- a/test/functional/search_api_test.rb +++ b/test/functional/search_api_test.rb @@ -75,9 +75,9 @@ def app end - # returns users by id when order_by is not provided and sorted direction default DESC - test 'search profiles without order_by and default sort_direction' do - get '/api/srch/profiles?srchString=steff' + # search by username and returns users by id when order_by is not provided and sorted direction default DESC + test 'search profiles by username without order_by and default sort_direction' do + get '/api/srch/profiles?srchString=steff&field=username' assert last_response.ok? # Expected search pattern @@ -100,9 +100,9 @@ def app end - # returns users sorteded by recent activity and order direction default DESC - test 'search recent profiles with sort_by=recent present' do - get '/api/srch/profiles?srchString=steff&sort_by=recent' + # search by username and returns users sorteded by recent activity and order direction default DESC + test 'search recent profiles by username with sort_by=recent present' do + get '/api/srch/profiles?srchString=steff&field=username&sort_by=recent' assert last_response.ok? # Expected search pattern @@ -124,9 +124,9 @@ def app assert matcher =~ json end - # returns users ordered by recent activity and sorted by ASC direction - test 'search recent profiles with sort_by=recent present and order_direction ASC' do - get '/api/srch/profiles?srchString=steff&sort_by=recent&order_direction=ASC' + # search by username and returns users ordered by recent activity and sorted by ASC direction + test 'search recent profiles by username with sort_by=recent present and order_direction ASC' do + get '/api/srch/profiles?srchString=steff&field=username&sort_by=recent&order_direction=ASC' assert last_response.ok? # Expected search pattern @@ -148,6 +148,32 @@ def app assert matcher =~ json end + # search by username and bio, returns users by id when order_by is not provided and sorted direction default DESC + test 'search profiles by username and bio without order_by and default sort_direction' do + get '/api/srch/profiles?srchString=steff' + assert last_response.ok? + + # User.search() only works for mysql/mariadb + if ActiveRecord::Base.connection.adapter_name == 'Mysql2' + # Expected search pattern + pattern = { + srchParams: { + srchString: 'steff', + seq: nil, + }.ignore_extra_keys! + }.ignore_extra_keys! + + matcher = JsonExpressions::Matcher.new(pattern) + + assert_equal "/profile/data", json['items'][0]['docUrl'] + assert_equal "/profile/steff3", json['items'][1]['docUrl'] + assert_equal "/profile/steff2", json['items'][2]['docUrl'] + assert_equal "/profile/steff1", json['items'][3]['docUrl'] + + assert matcher =~ json + end + end + test 'search notes functionality' do get '/api/srch/notes?srchString=Blog' assert last_response.ok? diff --git a/test/unit/node_test.rb b/test/unit/node_test.rb index 46ca04d103..5d778b3c0f 100644 --- a/test/unit/node_test.rb +++ b/test/unit/node_test.rb @@ -248,7 +248,8 @@ class NodeTest < ActiveSupport::TestCase notes = Node.research_notes expected = [nodes(:one), nodes(:spam), nodes(:first_timer_note), nodes(:blog), nodes(:moderated_user_note), nodes(:activity), nodes(:upgrade), - nodes(:draft), nodes(:post_test1), nodes(:post_test2), nodes(:post_test3)] + nodes(:draft), nodes(:post_test1), nodes(:post_test2), + nodes(:post_test3), nodes(:post_test4)] assert_equal expected, notes end From f7a09d5da99b7452e87f515bcc612cf9ea0db322 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camila=20Ara=C3=BAjo?= Date: Wed, 15 Aug 2018 22:30:53 -0700 Subject: [PATCH 3/7] Added missing json --- test/functional/search_api_test.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/functional/search_api_test.rb b/test/functional/search_api_test.rb index 527164964c..53302e7355 100644 --- a/test/functional/search_api_test.rb +++ b/test/functional/search_api_test.rb @@ -165,6 +165,8 @@ def app matcher = JsonExpressions::Matcher.new(pattern) + json = JSON.parse(last_response.body) + assert_equal "/profile/data", json['items'][0]['docUrl'] assert_equal "/profile/steff3", json['items'][1]['docUrl'] assert_equal "/profile/steff2", json['items'][2]['docUrl'] From 14c6823e3ae23e1acb4eb9f188dcbf885cd2b382 Mon Sep 17 00:00:00 2001 From: Stefanni Date: Thu, 16 Aug 2018 19:33:02 -0700 Subject: [PATCH 4/7] Change the params to create a new SearchCriteria instance --- app/api/srch/search.rb | 7 +------ app/api/srch/typeahead.rb | 3 +-- app/services/search_criteria.rb | 12 ++++++------ 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/app/api/srch/search.rb b/app/api/srch/search.rb index 24650e72b6..841697c9e7 100644 --- a/app/api/srch/search.rb +++ b/app/api/srch/search.rb @@ -103,13 +103,8 @@ class Search < Grape::API def self.execute(endpoint, params) sresult = DocList.new - search_query = params[:srchString] - tag_query = params[:tagName] - order_query = params[:order_direction] - sort_query = params[:sort_by] - field_query = params[:field] search_type = endpoint - search_criteria = SearchCriteria.new(search_query, tag: tag_query, sort_by: sort_query, order_direction: order_query, field: field_query) + search_criteria = SearchCriteria.new(params) if search_criteria.valid? sresult = ExecuteSearch.new.by(search_type, search_criteria) diff --git a/app/api/srch/typeahead.rb b/app/api/srch/typeahead.rb index 3f1827a2b0..86af96c84b 100644 --- a/app/api/srch/typeahead.rb +++ b/app/api/srch/typeahead.rb @@ -88,9 +88,8 @@ class Typeahead < Grape::API def self.execute(endpoint, params) sresult = TagList.new - search_query = params[:srchString] search_type = endpoint - search_criteria = SearchCriteria.new(search_query) + search_criteria = SearchCriteria.new(params) if search_criteria.valid? sresult = ExecuteTypeahead.new.by(search_type, search_criteria, TYPEAHEAD_LIMIT) diff --git a/app/services/search_criteria.rb b/app/services/search_criteria.rb index ce00164925..10aa73f450 100644 --- a/app/services/search_criteria.rb +++ b/app/services/search_criteria.rb @@ -1,12 +1,12 @@ class SearchCriteria attr_reader :query, :tag, :sort_by, :field - def initialize(query, tag: nil, sort_by: nil, order_direction: "DESC", field: nil) - @query = query - @tag = tag - @sort_by = sort_by - @order_direction = order_direction - @field = field + def initialize(params) + @query = params[:srchString] + @tag = params[:tagName] + @sort_by = params[:sort_by] + @order_direction = params[:order_direction] + @field = params[:field] end def valid? From 330b104271735b233311e9e1313a69f2dc568759 Mon Sep 17 00:00:00 2001 From: milaaraujo Date: Thu, 16 Aug 2018 19:59:12 -0700 Subject: [PATCH 5/7] Adding limit parameter --- app/api/srch/shared_params.rb | 1 + app/services/search_criteria.rb | 3 ++- app/services/search_service.rb | 9 +++++---- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/api/srch/shared_params.rb b/app/api/srch/shared_params.rb index 456d7ebe07..57d31ed7fa 100644 --- a/app/api/srch/shared_params.rb +++ b/app/api/srch/shared_params.rb @@ -25,6 +25,7 @@ module SharedParams params :field do optional :field, type: String, documentation: { example: 'username' } + optional :limit, type: Integer, documentation: { example: 0 } end params :commontypeahead do diff --git a/app/services/search_criteria.rb b/app/services/search_criteria.rb index 10aa73f450..581b983127 100644 --- a/app/services/search_criteria.rb +++ b/app/services/search_criteria.rb @@ -1,5 +1,5 @@ class SearchCriteria - attr_reader :query, :tag, :sort_by, :field + attr_reader :query, :tag, :sort_by, :field, :limit def initialize(params) @query = params[:srchString] @@ -7,6 +7,7 @@ def initialize(params) @sort_by = params[:sort_by] @order_direction = params[:order_direction] @field = params[:field] + @limit = params[:limit] end def valid? diff --git a/app/services/search_service.rb b/app/services/search_service.rb index af3bda6858..e067b82557 100644 --- a/app/services/search_service.rb +++ b/app/services/search_service.rb @@ -50,11 +50,13 @@ def textSearch_all(search_criteria) # If no sort_by value present, then it returns a list of profiles ordered by id DESC # a recent activity may be a node creation or a node revision def profiles(search_criteria) + limit = search_criteria.limit ? search_criteria.limit : 10 + user_scope = if search_criteria.field == "username" - SrchScope.find_by_username(search_criteria.query, limit = 10) + SrchScope.find_by_username(search_criteria.query, limit) else - SrchScope.find_users(search_criteria.query, limit = 10) + SrchScope.find_users(search_criteria.query, limit) end user_scope = @@ -62,12 +64,11 @@ def profiles(search_criteria) user_scope.joins(:revisions) .order("node_revisions.timestamp #{search_criteria.order_direction}") .distinct - else user_scope.order(id: :desc) end - users = user_scope.limit(10) + users = user_scope.limit(limit) sresult = DocList.new users.each do |match| From 0b92a20235ad34fd48ef0855bd6657b060db5150 Mon Sep 17 00:00:00 2001 From: Stefanni Date: Thu, 16 Aug 2018 20:07:59 -0700 Subject: [PATCH 6/7] Refactor find_users and add bio search test --- app/services/search_service.rb | 7 +------ app/services/srch_scope.rb | 21 ++++----------------- test/fixtures/users.yml | 2 +- test/functional/search_api_test.rb | 29 +++++++++++++++++++++++++++-- 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/app/services/search_service.rb b/app/services/search_service.rb index e067b82557..7ec87a39bb 100644 --- a/app/services/search_service.rb +++ b/app/services/search_service.rb @@ -52,12 +52,7 @@ def textSearch_all(search_criteria) def profiles(search_criteria) limit = search_criteria.limit ? search_criteria.limit : 10 - user_scope = - if search_criteria.field == "username" - SrchScope.find_by_username(search_criteria.query, limit) - else - SrchScope.find_users(search_criteria.query, limit) - end + user_scope = SrchScope.find_users(search_criteria.query, search_criteria.field, limit = 10) user_scope = if search_criteria.sort_by == "recent" diff --git a/app/services/srch_scope.rb b/app/services/srch_scope.rb index 23c06146ab..751db50606 100644 --- a/app/services/srch_scope.rb +++ b/app/services/srch_scope.rb @@ -1,25 +1,12 @@ # This class provides common methods that Typehead and Search services use class SrchScope - def self.find_users(query, limit) + def self.find_users(query, type = nil, limit) if ActiveRecord::Base.connection.adapter_name == 'Mysql2' - User.search(query) - .where('rusers.status = ?', 1) - .limit(limit) + users = type == "username" ? User.search_by_username(query).where('rusers.status = ?', 1) : User.search(query).where('rusers.status = ?', 1) else - User.where('username LIKE ? AND rusers.status = 1', '%' + query + '%') - .limit(limit) - end - end - - def self.find_by_username(query, limit) - if ActiveRecord::Base.connection.adapter_name == 'Mysql2' - User.search_by_username(query) - .where('rusers.status = ?', 1) - .limit(limit) - else - User.where('username LIKE ? AND rusers.status = 1', '%' + query + '%') - .limit(limit) + users = User.where('username LIKE ? AND rusers.status = 1', '%' + query + '%') end + users = users.limit(limit) end def self.find_tags(input, limit) diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 43d92dd7b5..27f647e4de 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -129,7 +129,7 @@ test_user: password_salt: <%= salt = Authlogic::Random.hex_token %> crypted_password: <%= Authlogic::CryptoProviders::Sha512.encrypt("secretive" + salt) %> persistence_token: <%= Authlogic::Random.hex_token %> - bio: '' + bio: 'I love ruby' created_at: <%= Time.now %> updated_at: <%= Time.now %> diff --git a/test/functional/search_api_test.rb b/test/functional/search_api_test.rb index 53302e7355..4e4112b959 100644 --- a/test/functional/search_api_test.rb +++ b/test/functional/search_api_test.rb @@ -166,8 +166,8 @@ def app matcher = JsonExpressions::Matcher.new(pattern) json = JSON.parse(last_response.body) - - assert_equal "/profile/data", json['items'][0]['docUrl'] + + assert_equal "/profile/data", json['items'][0]['docUrl'] assert_equal "/profile/steff3", json['items'][1]['docUrl'] assert_equal "/profile/steff2", json['items'][2]['docUrl'] assert_equal "/profile/steff1", json['items'][3]['docUrl'] @@ -176,6 +176,31 @@ def app end end + # search by bio only + test 'search profiles by bio without order_by and default sort_direction' do + get '/api/srch/profiles?srchString=ruby' + assert last_response.ok? + + # User.search() only works for mysql/mariadb + if ActiveRecord::Base.connection.adapter_name == 'Mysql2' + # Expected search pattern + pattern = { + srchParams: { + srchString: 'ruby', + seq: nil, + }.ignore_extra_keys! + }.ignore_extra_keys! + + matcher = JsonExpressions::Matcher.new(pattern) + + json = JSON.parse(last_response.body) + + assert_equal "/profile/testuser", json['items'][0]['docUrl'] + + assert matcher =~ json + end + end + test 'search notes functionality' do get '/api/srch/notes?srchString=Blog' assert last_response.ok? From fcb2c902170d8004308f93cefd1a5977998c829e Mon Sep 17 00:00:00 2001 From: Stefanni Date: Thu, 16 Aug 2018 20:19:10 -0700 Subject: [PATCH 7/7] fix codeclimate --- app/services/srch_scope.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/services/srch_scope.rb b/app/services/srch_scope.rb index 751db50606..b9b831c693 100644 --- a/app/services/srch_scope.rb +++ b/app/services/srch_scope.rb @@ -1,11 +1,12 @@ # This class provides common methods that Typehead and Search services use class SrchScope def self.find_users(query, type = nil, limit) - if ActiveRecord::Base.connection.adapter_name == 'Mysql2' - users = type == "username" ? User.search_by_username(query).where('rusers.status = ?', 1) : User.search(query).where('rusers.status = ?', 1) - else - users = User.where('username LIKE ? AND rusers.status = 1', '%' + query + '%') - end + users = + if ActiveRecord::Base.connection.adapter_name == 'Mysql2' + type == "username" ? User.search_by_username(query).where('rusers.status = ?', 1) : User.search(query).where('rusers.status = ?', 1) + else + User.where('username LIKE ? AND rusers.status = 1', '%' + query + '%') + end users = users.limit(limit) end