From d72f02aff934f22bf9bb68560661da4c9675338e Mon Sep 17 00:00:00 2001 From: milaaraujo Date: Thu, 2 Aug 2018 20:49:09 -0700 Subject: [PATCH] using find_users --- app/models/user.rb | 2 +- app/services/search_service.rb | 10 +++++----- app/services/srch_scope.rb | 8 ++++---- test/fixtures/nodes.yml | 4 ++-- test/fixtures/revisions.yml | 4 ++-- test/functional/search_api_test.rb | 8 ++++---- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index f5319f4c3c..da5231e8ff 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -43,7 +43,7 @@ class User < ActiveRecord::Base after_destroy :destroy_drupal_user def self.search(query) - User.where('MATCH(username, bio) AGAINST(?)', query) + User.where('MATCH(username, bio) AGAINST (?)', query) end def new_contributor diff --git a/app/services/search_service.rb b/app/services/search_service.rb index 757c78013c..9100c028a5 100644 --- a/app/services/search_service.rb +++ b/app/services/search_service.rb @@ -50,19 +50,19 @@ 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 textSearch_profiles(search_criteria) - user_scope = User.where('username LIKE ? AND rusers.status = 1', '%' + search_criteria.query + '%') + user_scope = SrchScope.find_users(search_criteria.query, limit = 10) + .reorder('') user_scope = if search_criteria.sort_by == "recent" user_scope.joins(:revisions) .order("node_revisions.timestamp #{search_criteria.order_direction}") - .distinct # do we need unique/distinct on user id ? - + .distinct else - user_scope.order(id: :desc) # order by what when order_by is not present? id? username? + user_scope.order(id: :desc) end - users = user_scope.limit(10) # verify limit? + users = user_scope.limit(10) sresult = DocList.new users.each do |match| diff --git a/app/services/srch_scope.rb b/app/services/srch_scope.rb index a2262ce44d..7f63acb5e5 100644 --- a/app/services/srch_scope.rb +++ b/app/services/srch_scope.rb @@ -3,12 +3,12 @@ class SrchScope def self.find_users(input, limit) if ActiveRecord::Base.connection.adapter_name == 'Mysql2' User.search(input) - .order('id DESC') # do we need this line? - .where('rusers.status = ?', 1) + .order('id DESC') + .where(status: 1) .limit(limit) else - User.order('id DESC') - .where('username LIKE ? AND rusers.status = 1', '%' + input + '%') + User.where('username LIKE ? AND rusers.status = 1', '%' + input + '%') + .order('id DESC') .limit(limit) end end diff --git a/test/fixtures/nodes.yml b/test/fixtures/nodes.yml index 3503d49598..64f286e753 100644 --- a/test/fixtures/nodes.yml +++ b/test/fixtures/nodes.yml @@ -273,7 +273,7 @@ post_test1: post_test2: nid: 24 - uid: 16 + uid: 17 title: "Post Test 2" path: "/notes/user2/<%= DateTime.new(2018,8,8).strftime("%m-%d-%Y") %>/post-test2" created: <%= DateTime.new(2018,8,8).to_i %> @@ -285,7 +285,7 @@ post_test2: post_test3: nid: 25 - uid: 17 + uid: 16 title: "Post Test 3" path: "/notes/user3/<%= DateTime.new(2018,8,15).strftime("%m-%d-%Y") %>/post-test3" created: <%= DateTime.new(2018,8,15).to_i %> diff --git a/test/fixtures/revisions.yml b/test/fixtures/revisions.yml index ba0996f62d..f0ac930edd 100644 --- a/test/fixtures/revisions.yml +++ b/test/fixtures/revisions.yml @@ -287,14 +287,14 @@ post_test1: post_test2: nid: 24 - uid: 16 + uid: 17 title: Post test review 2 body: Post test review 2 timestamp: <%= DateTime.new(2018,8,8).to_i %> post_test3: nid: 25 - uid: 17 + uid: 16 title: Post test review 2 body: Post test review 2 timestamp: <%= DateTime.new(2018,8,15).to_i %> diff --git a/test/functional/search_api_test.rb b/test/functional/search_api_test.rb index 0ce8fc8b3b..8d34a7c2f7 100644 --- a/test/functional/search_api_test.rb +++ b/test/functional/search_api_test.rb @@ -117,8 +117,8 @@ def app json = JSON.parse(last_response.body) - assert_equal "steff3", json['items'][0]['docTitle'] - assert_equal "steff2", json['items'][1]['docTitle'] + assert_equal "steff2", json['items'][0]['docTitle'] + assert_equal "steff3", json['items'][1]['docTitle'] assert_equal "steff1", json['items'][2]['docTitle'] assert matcher =~ json @@ -142,8 +142,8 @@ def app json = JSON.parse(last_response.body) assert_equal "/profile/steff1", json['items'][0]['docUrl'] - assert_equal "/profile/steff2", json['items'][1]['docUrl'] - assert_equal "/profile/steff3", json['items'][2]['docUrl'] + assert_equal "/profile/steff3", json['items'][1]['docUrl'] + assert_equal "/profile/steff2", json['items'][2]['docUrl'] assert matcher =~ json end