Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Using typeahead when adding users to a team #547

Merged
merged 1 commit into from
Nov 12, 2015
Merged

Conversation

jloehel
Copy link
Contributor

@jloehel jloehel commented Nov 9, 2015

Fixes: #39

Signed-off-by: Jürgen Löhel [email protected]

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)
Copy link
Contributor

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)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @salzig

@mssola
Copy link
Collaborator

mssola commented Nov 10, 2015

This branch has conflicts because I've merged #534

@jloehel jloehel force-pushed the typeahead_2 branch 5 times, most recently from 070e400 to 1445955 Compare November 11, 2015 14:00
authorize @team
@query = params[:query]
matches = User.search_from_query(@team.member_ids,
"#{@query}%").map { |user| { username: user } }
Copy link
Contributor

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.

Copy link
Contributor Author

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)

Copy link
Contributor

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 } }

Copy link
Contributor

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}
Copy link
Collaborator

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.

Copy link
Contributor Author

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

Copy link
Collaborator

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

@jloehel jloehel force-pushed the typeahead_2 branch 2 times, most recently from f136130 to 31fc44a Compare November 12, 2015 11:04
@mssola
Copy link
Collaborator

mssola commented Nov 12, 2015

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.

@mssola
Copy link
Collaborator

mssola commented Nov 12, 2015

LGTM

@flavio
Copy link
Member

flavio commented Nov 12, 2015

Good job @jloehel! LGMT

@salzig
Copy link
Contributor

salzig commented Nov 12, 2015

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" }
Copy link
Collaborator

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.

mssola added a commit that referenced this pull request Nov 12, 2015
Using typeahead when adding users to a team
@mssola mssola merged commit a8782b5 into SUSE:master Nov 12, 2015
@mssola
Copy link
Collaborator

mssola commented Nov 12, 2015

Good job 👏

@flavio
Copy link
Member

flavio commented Nov 12, 2015

@mssola or @jloehel: could you also update the changelog?

@mssola
Copy link
Collaborator

mssola commented Nov 12, 2015

@flavio let me do it

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants