summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/controllers/autocomplete_controller.rb23
-rw-r--r--app/finders/yet_another_users_finder.rb42
-rw-r--r--spec/finders/yet_another_users_finder_spec.rb86
3 files changed, 129 insertions, 22 deletions
diff --git a/app/controllers/autocomplete_controller.rb b/app/controllers/autocomplete_controller.rb
index dfc8bd0ba81..8e945f90f76 100644
--- a/app/controllers/autocomplete_controller.rb
+++ b/app/controllers/autocomplete_controller.rb
@@ -6,28 +6,7 @@ class AutocompleteController < ApplicationController
before_action :find_users, only: [:users]
def users
- @users ||= User.none
- @users = @users.active
- @users = @users.reorder(:name)
- @users = @users.search(params[:search]) if params[:search].present?
- @users = @users.where.not(id: params[:skip_users]) if params[:skip_users].present?
- @users = @users.page(params[:page]).per(params[:per_page])
-
- if params[:todo_filter].present? && current_user
- @users = @users.todo_authors(current_user.id, params[:todo_state_filter])
- end
-
- if params[:search].blank?
- # Include current user if available to filter by "Me"
- if params[:current_user].present? && current_user
- @users = [current_user, *@users].uniq
- end
-
- if params[:author_id].present? && current_user
- author = User.find_by_id(params[:author_id])
- @users = [author, *@users].uniq if author
- end
- end
+ @users = YetAnotherUsersFinder.new(params: params, current_user: current_user, users: @users).execute
render json: @users, only: [:name, :username, :id], methods: [:avatar_url]
end
diff --git a/app/finders/yet_another_users_finder.rb b/app/finders/yet_another_users_finder.rb
new file mode 100644
index 00000000000..5f7f3913160
--- /dev/null
+++ b/app/finders/yet_another_users_finder.rb
@@ -0,0 +1,42 @@
+class YetAnotherUsersFinder
+ attr_reader :current_user, :users, :search, :skip_users, :page,
+ :per_page, :author_id, :params
+
+ def initialize(params:, current_user:, users: nil)
+ @current_user = current_user
+ @users = users
+ @search = params[:search]
+ @skip_users = params[:skip_users]
+ @page = params[:page]
+ @per_page = params[:per_page]
+ @author_id = params[:author_id]
+ @params = params
+ end
+
+ def execute
+ items = users || User.none
+ items = items.active
+ items = items.reorder(:name)
+ items = items.search(search) if search.present?
+ items = items.where.not(id: skip_users) if skip_users.present?
+ items = items.page(page).per(per_page)
+
+ if params[:todo_filter].present? && current_user
+ items = items.todo_authors(current_user.id, params[:todo_state_filter])
+ end
+
+ if search.blank?
+ # Include current user if available to filter by "Me"
+ if params[:current_user].present? && current_user
+ items = [current_user, *items].uniq
+ end
+
+ if author_id.present? && current_user
+ author = User.find_by_id(author_id)
+ items = [author, *items].uniq if author
+ end
+ end
+
+ items
+ end
+end
diff --git a/spec/finders/yet_another_users_finder_spec.rb b/spec/finders/yet_another_users_finder_spec.rb
new file mode 100644
index 00000000000..98a4fc4d20e
--- /dev/null
+++ b/spec/finders/yet_another_users_finder_spec.rb
@@ -0,0 +1,86 @@
+require 'spec_helper'
+
+describe YetAnotherUsersFinder do
+ describe '#execute' do
+ let!(:user1) { create(:user, username: 'johndoe') }
+ let!(:user2) { create(:user, :blocked, username: 'notsorandom') }
+ let!(:external_user) { create(:user, :external) }
+ let!(:omniauth_user) { create(:omniauth_user, provider: 'twitter', extern_uid: '123456') }
+ let(:current_user) { create(:user) }
+ let(:params) { {} }
+
+ it 'returns no users' do
+ users = described_class.new(params: params, current_user: current_user).execute
+
+ expect(users.size).to eq(0)
+ end
+
+
+ # context 'with a normal user' do
+ # let(:user) { create(:user) }
+
+ # it 'returns all users' do
+ # users = described_class.new(user).execute
+
+ # expect(users).to contain_exactly(user, user1, user2, omniauth_user)
+ # end
+
+ # it 'filters by username' do
+ # users = described_class.new(user, username: 'johndoe').execute
+
+ # expect(users).to contain_exactly(user1)
+ # end
+
+ # it 'filters by search' do
+ # users = described_class.new(user, search: 'orando').execute
+
+ # expect(users).to contain_exactly(user2)
+ # end
+
+ # it 'filters by blocked users' do
+ # users = described_class.new(user, blocked: true).execute
+
+ # expect(users).to contain_exactly(user2)
+ # end
+
+ # it 'filters by active users' do
+ # users = described_class.new(user, active: true).execute
+
+ # expect(users).to contain_exactly(user, user1, omniauth_user)
+ # end
+
+ # it 'returns no external users' do
+ # users = described_class.new(user, external: true).execute
+
+ # expect(users).to contain_exactly(user, user1, user2, omniauth_user)
+ # end
+
+ # it 'filters by created_at' do
+ # filtered_user_before = create(:user, created_at: 3.days.ago)
+ # filtered_user_after = create(:user, created_at: Time.now + 3.days)
+
+ # users = described_class.new(user,
+ # created_after: 2.days.ago,
+ # created_before: Time.now + 2.days).execute
+
+ # expect(users.map(&:username)).not_to include([filtered_user_before.username, filtered_user_after.username])
+ # end
+ # end
+
+ # context 'with an admin user' do
+ # let(:admin) { create(:admin) }
+
+ # it 'filters by external users' do
+ # users = described_class.new(admin, external: true).execute
+
+ # expect(users).to contain_exactly(external_user)
+ # end
+
+ # it 'returns all users' do
+ # users = described_class.new(admin).execute
+
+ # expect(users).to contain_exactly(admin, user1, user2, external_user, omniauth_user)
+ # end
+ # end
+ end
+end