Commit fbbb985a authored by Maxim Rydkin's avatar Maxim Rydkin

extract finder and add first test

parent 5d952f75
...@@ -6,28 +6,7 @@ class AutocompleteController < ApplicationController ...@@ -6,28 +6,7 @@ class AutocompleteController < ApplicationController
before_action :find_users, only: [:users] before_action :find_users, only: [:users]
def users def users
@users ||= User.none @users = YetAnotherUsersFinder.new(params: params, current_user: current_user, users: @users).execute
@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
render json: @users, only: [:name, :username, :id], methods: [:avatar_url] render json: @users, only: [:name, :username, :id], methods: [:avatar_url]
end end
......
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
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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment