I’m working on a project that uses OmniAuth to connect a user’s account to LinkedIn, Twitter, and Google Plus. Easy enough, right? On one screen in my app, I needed to display the services that a user had already connected, and the ones that were still available to connect.
The former is easy:
But what about displaying services that the user hadn’t connected yet? I didn’t want to store a constant of available providers, as that’d just be one more place I’d have to edit when adding a new provider. So, I dug around a bit and found this:
That returns an array of providers, with one caveat: it adds Developer, OAuth2, and Oauth to the list by default. I didn’t want those exposed to the end user, so I added a method in my Service.rb class:
1 2 3
Then, to display the services that the user hadn’t connected yet, I used:
1 2 3 4 5
This works great, but it doesn’t feel 100% right. Any suggestions on how to do it better?
Matt Brand reminded me that you can subtract arrays in ruby. While looking at that part of the code, I realized I’d sinned and had this
current_user.services.pluck(:provider) logic within the loop. While rails would’ve cached that query, it’s still bad form. I started to rewrite that block with:
1 2 3
But that’s just too much logic for an erb, so I added a method to my User.rb model:
1 2 3
And now my code for displaying the available services looks like this:
1 2 3
Now, I just need someone to recommend a better way to get available providers from OmniAuth, as
OmniAuth::Strategies.constants still feels wrong.