Skip to content

Commit

Permalink
Add autocomplete controller
Browse files Browse the repository at this point in the history
  • Loading branch information
dzaporozhets committed Mar 27, 2015
1 parent dfb4fcb commit 26053c8
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
30 changes: 30 additions & 0 deletions app/controllers/autocomplete_controller.rb
@@ -0,0 +1,30 @@
class AutocompleteController < ApplicationController
def users
@users =
if params[:project_id].present?
project = Project.find(params[:project_id])

if can?(current_user, :read_project, project)
project.team.users
end
elsif params[:group_id]
group = Group.find(params[:group_id])

if can?(current_user, :read_group, group)
group.users
end
else
User.all
end

@users = @users.search(params[:search]) if params[:search].present?
@users = @users.active
@users = @users.page(params[:page]).per(PER_PAGE)
render json: @users, only: [:name, :username, :id], methods: [:avatar_url]
end

def user
@user = User.find(params[:id])
render json: @user, only: [:name, :username, :id], methods: [:avatar_url]
end
end
5 changes: 5 additions & 0 deletions config/routes.rb
Expand Up @@ -8,6 +8,11 @@
authorizations: 'oauth/authorizations'
end

# Autocomplete
get '/autocomplete/users' => 'autocomplete#users'
get '/autocomplete/users/:id' => 'autocomplete#user'


# Search
get 'search' => 'search#show'
get 'search/autocomplete' => 'search#autocomplete', as: :search_autocomplete
Expand Down
51 changes: 51 additions & 0 deletions spec/controllers/autocomplete_controller_spec.rb
@@ -0,0 +1,51 @@
require 'spec_helper'

describe AutocompleteController do
let!(:project) { create(:project) }
let!(:user) { create(:user) }
let!(:user2) { create(:user) }

context 'project members' do
before do
sign_in(user)
project.team << [user, :master]

get(:users, project_id: project.id)
end

let(:body) { JSON.parse(response.body) }

it { body.should be_kind_of(Array) }
it { body.size.should eq(1) }
it { body.first["username"].should == user.username }
end

context 'group members' do
let(:group) { create(:group) }

before do
sign_in(user)
group.add_owner(user)

get(:users, group_id: group.id)
end

let(:body) { JSON.parse(response.body) }

it { body.should be_kind_of(Array) }
it { body.size.should eq(1) }
it { body.first["username"].should == user.username }
end

context 'all users' do
before do
sign_in(user)
get(:users)
end

let(:body) { JSON.parse(response.body) }

it { body.should be_kind_of(Array) }
it { body.size.should eq(User.count) }
end
end

0 comments on commit 26053c8

Please sign in to comment.