Skip to content
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

Merged
merged 1 commit into from
Jul 14, 2017

Conversation

MaicolBen
Copy link
Contributor

@MaicolBen MaicolBen commented Mar 10, 2017

Resolves #33

Using /users/me make the next nested resources use the resource_id param instead of id. Also me 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 as user_controller and tell that to the resource route.

@MaicolBen MaicolBen force-pushed the refactor/single-resource branch from 27cb458 to c69b8a8 Compare March 14, 2017 15:08
@MaicolBen MaicolBen removed the On hold label Mar 14, 2017
@@ -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}]
Copy link
Contributor

@matiasmansilla1989 matiasmansilla1989 Mar 14, 2017

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?

Copy link
Contributor Author

@MaicolBen MaicolBen Mar 14, 2017

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.

@MaicolBen MaicolBen force-pushed the refactor/single-resource branch from c69b8a8 to 124726d Compare March 14, 2017 15:39
Copy link
Contributor

@matiasmansilla1989 matiasmansilla1989 left a comment

Choose a reason for hiding this comment

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

Great work!!!

@MaicolBen MaicolBen force-pushed the refactor/single-resource branch from 124726d to 324a1db Compare April 18, 2017 19:40
@@ -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' }
Copy link
Contributor Author

@MaicolBen MaicolBen Apr 18, 2017

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it.

Copy link
Contributor

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

screen shot 2017-07-13 at 2 15 59 pm

@MaicolBen MaicolBen force-pushed the refactor/single-resource branch from 324a1db to 6dc5d61 Compare April 18, 2017 19:46
@MaicolBen MaicolBen force-pushed the refactor/single-resource branch from 6dc5d61 to bf34219 Compare April 28, 2017 14:52
@MaicolBen MaicolBen force-pushed the refactor/single-resource branch 2 times, most recently from 8e37790 to faffb4c Compare June 20, 2017 17:39
@MaicolBen MaicolBen requested a review from GAKINDUSTRIES June 20, 2017 17:42

+ Request (application/json)
+ Parameters
+ id (integer, required)
Copy link
Contributor

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

Copy link
Contributor Author

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

Copy link
Contributor

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",
Copy link
Contributor

@GAKINDUSTRIES GAKINDUSTRIES Jun 20, 2017

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

Copy link
Contributor Author

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

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.

Copy link
Contributor Author

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?

Copy link
Contributor

@GAKINDUSTRIES GAKINDUSTRIES Jun 20, 2017

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?

Copy link
Contributor Author

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

Copy link
Contributor

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 👍

@MaicolBen MaicolBen force-pushed the refactor/single-resource branch 2 times, most recently from d1289a6 to b10e568 Compare June 20, 2017 19:08
@MaicolBen MaicolBen requested a review from santiagovidal June 20, 2017 20:43
Copy link
Contributor

@santiagovidal santiagovidal left a 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
Copy link
Contributor

@santiagovidal santiagovidal Jun 21, 2017

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

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' }
Copy link
Contributor

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?

@MaicolBen MaicolBen force-pushed the refactor/single-resource branch from 61d61b8 to 3d66914 Compare July 13, 2017 17:54
@MaicolBen MaicolBen merged commit 5b007d6 into master Jul 14, 2017
@MaicolBen MaicolBen deleted the refactor/single-resource branch July 14, 2017 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants