-
Notifications
You must be signed in to change notification settings - Fork 473
Using typeahead when adding users to a team #547
Conversation
search = [] | ||
valid_users = User.enabled.where.not(id: @team.team_users.pluck(:user_id)) | ||
valid_users.each do |user| | ||
search.push(username: user.username) if user.username.start_with?(@query) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could do this in database.
team_user_ids = @team.team_users.pluck(:user_id)
User.enabled.where.not(id: team_user_ids).where(User.arel_table[:username].matches("#@query%")).pluck(:username)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @salzig
This branch has conflicts because I've merged #534 |
070e400
to
1445955
Compare
authorize @team | ||
@query = params[:query] | ||
matches = User.search_from_query(@team.member_ids, | ||
"#{@query}%").map { |user| { username: user } } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO: its a code smell to fetch and build objects where only a single attribute is needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bootstrap Typeahead for Rails (https://github.com/Nerian/bootstrap-typeahead-rails) uses typeahead 0.10.5.1 and for this version it is necessary to build objects. Should I switch to a newer version of typeahead? (0.11.1)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, i don't mean those hashes. I was thinking about those ActiveRecord Objects that get build right before you map them down to hashes. Given there are 100 results by search_from_query
you're building 100 ActiveRecord Objects containing all information stored for those results, just to map them down to username. A huge change would be to pluck the username, so ActiveRecord is only fetching the username from database.
matches = User.search_from_query(@team.member_ids,"#{@query}%").pluck(:username).map { |username| { username: username } }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sry, hadn't looked at the implementation of search_from_query
, and hadn't expect it, by the name, to return just matching usernames.
@@ -0,0 +1 @@ | |||
'#{@status} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this file. See comment above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I add the error message directly to the the application_controller.rb?
def deny_access
@status = 401
respond_to do |format|
format.html { render template: "errors/401", status: 401, layout: "errors" }
format.js { render js: @status }
format.json { render json: @status }
end
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
by above I meant below :P See my comment below about render nothing: true
f136130
to
31fc44a
Compare
The only problem that I got, and sorry for not noticing this before, is that one side-effect of this is that you've changed the size of the input element for the name (now it's much smaller). Could you change that? Besides that, it looks good to me. |
LGTM |
Good job @jloehel! LGMT |
beside comment in routes.rb:9 LGTM. Well done! |
resources :team_users, only: [:create, :destroy, :update] | ||
# get "teams/typeahead/:id/:query" => "teams#typeahead", :defaults => { format: "json" } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@salzig is right. Please remove this comment.
Fixes: SUSE#39 Signed-off-by: Jürgen Löhel <[email protected]>
Using typeahead when adding users to a team
Good job 👏 |
@flavio let me do it |
Fixes: #39
Signed-off-by: Jürgen Löhel [email protected]