Skip to content

Commit

Permalink
FEATURE: user directory returns staged users during search
Browse files Browse the repository at this point in the history
  • Loading branch information
gschlager committed Nov 19, 2017
1 parent 546b206 commit 92a831b
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 53 deletions.
2 changes: 1 addition & 1 deletion app/controllers/directory_items_controller.rb
Expand Up @@ -23,7 +23,7 @@ def index

user_ids = nil
if params[:name].present?
user_ids = UserSearch.new(params[:name]).search.pluck(:id)
user_ids = UserSearch.new(params[:name], include_staged_users: true).search.pluck(:id)
if user_ids.present?
# Add the current user if we have at least one other match
if current_user && result.dup.where(user_id: user_ids).exists?
Expand Down
4 changes: 3 additions & 1 deletion app/models/user_search.rb
Expand Up @@ -9,14 +9,16 @@ def initialize(term, opts = {})
@topic_id = opts[:topic_id]
@topic_allowed_users = opts[:topic_allowed_users]
@searching_user = opts[:searching_user]
@include_staged_users = opts[:include_staged_users] || false
@limit = opts[:limit] || 20
@group = opts[:group]
@guardian = Guardian.new(@searching_user)
@guardian.ensure_can_see_group!(@group) if @group
end

def scoped_users
users = User.where(active: true, staged: false)
users = User.where(active: true)
users = users.where(staged: false) unless @include_staged_users

if @group
users = users.where('users.id IN (
Expand Down
51 changes: 0 additions & 51 deletions spec/controllers/directory_items_controller_spec.rb

This file was deleted.

3 changes: 3 additions & 0 deletions spec/models/user_search_spec.rb
Expand Up @@ -134,6 +134,9 @@ def search_for(*args)
# don't return staged users
results = search_for(staged.username)
expect(results).to be_blank

results = search_for(staged.username, include_staged_users: true)
expect(results.first.username).to eq(staged.username)
end

end
83 changes: 83 additions & 0 deletions spec/requests/directory_items_controller_spec.rb
@@ -0,0 +1,83 @@
require 'rails_helper'

describe DirectoryItemsController do
let!(:user) { Fabricate(:user) }


it "requires a `period` param" do
expect do
get '/directory_items.json'
end.to raise_error(ActionController::ParameterMissing)
end

it "requires a proper `period` param" do
get '/directory_items.json', params: { period: 'eviltrout' }
expect(response).not_to be_success
end

context "without data" do

context "and a logged in user" do
before { sign_in(user) }

it "succeeds" do
get '/directory_items.json', params: { period: 'all' }
expect(response).to be_success
end
end

end

context "with data" do
before do
Fabricate(:evil_trout)
Fabricate(:walter_white)
Fabricate(:staged, username: 'stage_user')

DirectoryItem.refresh!
end

it "succeeds with a valid value" do
get '/directory_items.json', params: { period: 'all' }
expect(response).to be_success
json = ::JSON.parse(response.body)

expect(json).to be_present
expect(json['directory_items']).to be_present
expect(json['total_rows_directory_items']).to be_present
expect(json['load_more_directory_items']).to be_present

expect(json['directory_items'].length).to eq(4)
expect(json['total_rows_directory_items']).to eq(4)
end

it "fails when the directory is disabled" do
SiteSetting.enable_user_directory = false

get '/directory_items.json', params: { period: 'all' }
expect(response).not_to be_success
end

it "finds user by name" do
get '/directory_items.json', params: { period: 'all', name: 'eviltrout' }
expect(response).to be_success

json = ::JSON.parse(response.body)
expect(json).to be_present
expect(json['directory_items'].length).to eq(1)
expect(json['total_rows_directory_items']).to eq(1)
expect(json['directory_items'][0]['user']['username']).to eq('eviltrout')
end

it "finds staged user by name" do
get '/directory_items.json', params: { period: 'all', name: 'stage_user' }
expect(response).to be_success

json = ::JSON.parse(response.body)
expect(json).to be_present
expect(json['directory_items'].length).to eq(1)
expect(json['total_rows_directory_items']).to eq(1)
expect(json['directory_items'][0]['user']['username']).to eq('stage_user')
end
end
end

3 comments on commit 92a831b

@coding-horror
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should stated users show up in user search? Is this admin only?

@gschlager
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Staged users are always shown in the user directory. It was just not possible to search for them. It affects only the search on the user directory, not anywhere else. Sam requested it, but I can revert if we don't want this feature after all.

@coding-horror
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's OK I was just curious about it. I am not sure staged users should show up in the user directory at all though? Sure on the admin side but on the user-facing side??

Please sign in to comment.