-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use user single resources instead of /users/me #44
Conversation
27cb458
to
c69b8a8
Compare
@@ -129,9 +129,49 @@ API BASE is an internal TopTier project created to facilitate and standardize de | |||
} | |||
} | |||
|
|||
## Get other user's profile [/api/v1/users/{id}] |
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.
is this endpoint to get the current user profile? I think that this is to get any user profile and this one is to get the current user profile https://github.com/rootstrap/rails_api_base/pull/44/files#diff-2625016b50d68d922257f74801cac29cR53 right?
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 copied/pasted 🙂 , the subtitle below is wrong, it should be Get user
.
c69b8a8
to
124726d
Compare
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.
Great work!!!
124726d
to
324a1db
Compare
@@ -1,23 +1,25 @@ | |||
require 'rails_helper' | |||
|
|||
describe 'PUT api/v1/users/me', type: :request do | |||
let(:user) { create(:user) } | |||
let(:user) { create(:user) } | |||
let(:api_v1_user_path) { '/api/v1/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.
I don't like this hack, but this helpers returns /api/v1/user/:id
otherwise.
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.
Got it.
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.
That's a Rails bug right?
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.
Seems to be, I couldn't find it in the rails issues
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.
324a1db
to
6dc5d61
Compare
6dc5d61
to
bf34219
Compare
8e37790
to
faffb4c
Compare
|
||
+ Request (application/json) | ||
+ Parameters | ||
+ id (integer, required) |
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.
Isn't this implicit in the route? I think that if you specify just the route adding the id should be enough
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.
It just to give more information about the params, this allows to use the apiary live test tool. So I think is fine here, but not in the projects
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.
Nice 👍
apiary.apib
Outdated
{ | ||
"email": "test@test.com", | ||
"username": "test", | ||
"first_name": "Juanito", |
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 will avoid all these things. I was going to create a PR to modify this. There's no need to specify the name of someone here. I would use common example names in programming such as John Doe
or Jane Doe
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.
@matiasmansilla1989 fault, I just copied/pasted haha
@@ -4,10 +4,17 @@ module Api | |||
module V1 | |||
class UsersController < Api::V1::ApiController | |||
def show | |||
@user = User.find(params[:id]) |
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 would prefer to use a helper method for user in this case since it's being used in the view rendered.
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.
But there is one with current_user
, which one do you recommend adding to the helper method?
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 mean something like this:
helper_method :user
def user
@user ||= User.find(params[:id]) || current_user
end
make sense?
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.
Maybe User.find_by(id: params[:id])
, because find
blows up when it can't find it
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.
Exactly. Using find_by will be the correct way to go here 👍
d1289a6
to
b10e568
Compare
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.
Nice! I don't like the me
either.
If you have time, tests for profile
would be nice.
@@ -16,6 +22,10 @@ def update | |||
def user_params | |||
params.require(:user).permit(:username, :first_name, :last_name, :email) | |||
end | |||
|
|||
def user | |||
@user ||= User.find_by(id: params[:id]) || current_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.
Another way to write this would be: params[:id].present? ? User.find(id: params[:id]) : current_user
It's longer but you would be avoiding the extra query if params[:id]
is nil.
Also, if you receive an invalid id
you will return an error instead of current_user
@@ -2,16 +2,17 @@ | |||
|
|||
describe 'GET api/v1/users/me', type: :request do |
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.
We need to change this too.
@@ -1,23 +1,25 @@ | |||
require 'rails_helper' | |||
|
|||
describe 'PUT api/v1/users/me', type: :request do | |||
let(:user) { create(:user) } | |||
let(:user) { create(:user) } | |||
let(:api_v1_user_path) { '/api/v1/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.
That's a Rails bug right?
b10e568
to
61d61b8
Compare
61d61b8
to
3d66914
Compare
Resolves #33
Using
/users/me
make the next nested resources use theresource_id
param instead ofid
. Alsome
isn't too much REST, it's recommended to use ids not hardcoded strings.The downside of this, is that rails shares the same controller for the
resources :users
, so in the future if developers want to add it, what controller do you recommend? Or maybe rename the current one asuser_controller
and tell that to the resource route.