Skip to content

Commit

Permalink
Merge pull request #4737 from bgeuken/victors_pr
Browse files Browse the repository at this point in the history
refactored list_users method
  • Loading branch information
bgeuken committed Mar 27, 2018
2 parents a79d887 + 414d56f commit 22947a2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
19 changes: 2 additions & 17 deletions src/api/app/controllers/webui/user_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,27 +211,12 @@ def change_password

def autocomplete
required_parameters :term
render json: list_users(params[:term])
render json: User.autocomplete_login(params[:term])
end

def tokens
required_parameters :q
render json: list_users(params[:q], true)
end

protected

def list_users(prefix = nil, hash = nil)
names = []
users = User.arel_table
User.where(users[:login].matches("#{prefix}%")).pluck(:login).each do |user|
if hash
names << { 'name' => user }
else
names << user
end
end
names
render json: User.autocomplete_token(params[:q])
end

private
Expand Down
11 changes: 10 additions & 1 deletion src/api/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class User < ApplicationRecord
has_many :rss_feed_items, -> { order(created_at: :desc) }, class_name: 'Notification::RssFeedItem', as: :subscriber, dependent: :destroy

scope :all_without_nobody, -> { where('login != ?', NOBODY_LOGIN) }

scope :not_deleted, -> { where.not(state: 'deleted') }
scope :with_login_prefix, ->(prefix) { where('login LIKE ?', "#{prefix}%") }

validates :login, :state, presence: { message: 'must be given' }

Expand Down Expand Up @@ -98,6 +98,7 @@ class User < ApplicationRecord
validates :password, confirmation: true, allow_blank: true

after_create :create_home_project

def create_home_project
# avoid errors during seeding
return if login.in?([NOBODY_LOGIN, 'Admin'])
Expand Down Expand Up @@ -129,6 +130,14 @@ def create_home_project
self.login_failure_count = 0 if login_failure_count.nil?
end

def self.autocomplete_token(prefix = '')
autocomplete_login(prefix).collect { |user| { name: user } }
end

def self.autocomplete_login(prefix = '')
with_login_prefix(prefix).pluck(:login)
end

# the default state of a user based on the api configuration
def self.default_user_state
::Configuration.registration == 'confirmation' ? 'unconfirmed' : 'confirmed'
Expand Down
14 changes: 12 additions & 2 deletions src/api/spec/controllers/webui/user_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,21 @@
end

describe 'GET #autocomplete' do
skip
let!(:user) { create(:user, login: 'foobar') }

it 'returns user login' do
get :autocomplete, params: { term: 'foo', format: :json }
expect(JSON.parse(response.body)).to match_array(['foobar'])
end
end

describe 'GET #tokens' do
skip
let!(:user) { create(:user, login: 'foobaz') }

it 'returns user token as array of hash' do
get :tokens, params: { q: 'foo', format: :json }
expect(JSON.parse(response.body)).to match_array(['name' => 'foobaz'])
end
end

describe 'GET #notifications' do
Expand Down
17 changes: 17 additions & 0 deletions src/api/spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -475,4 +475,21 @@
end
end
end

describe 'autocomplete methods' do
let!(:foobar) { create(:confirmed_user, login: 'foobar') }
let!(:fobaz) { create(:confirmed_user, login: 'fobaz') }

context '#autocomplete_login' do
it { expect(User.autocomplete_login('foo')).to match_array(['foobar']) }
it { expect(User.autocomplete_login('bar')).to match_array([]) }
it { expect(User.autocomplete_login(nil)).to match_array(User.all.pluck(:login)) }
end

context '#autocomplete_token' do
subject { User.autocomplete_token('foo') }

it { expect(subject).to match_array([name: 'foobar']) }
end
end
end

0 comments on commit 22947a2

Please sign in to comment.