-
-
Notifications
You must be signed in to change notification settings - Fork 91
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
Nested RESTful resource(s) #51
Conversation
@@ -1,6 +1,7 @@ | |||
require 'lotus/utils/class_attribute' | |||
require 'lotus/routing/resource/options' | |||
require 'lotus/routing/resource/action' | |||
# require 'lotus/routing/resources' |
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.
[reminder] remove
8debb02
to
7109574
Compare
close #47 |
Thank you for this @AlfonsoUceda ! |
@jodosha @joneslee85 when you have time, can you review this PR? thanks ;) |
This is exactly what I needed. Thanks @AlfonsoUceda! |
Hey @AlfonsoUceda, it looks like nested routes lack the ability to accept options: router.define do
resources 'products' do
resources 'variants', only: [:index, :show]
end
end
#=> ArgumentError: wrong number of arguments (2 for 1) |
@Erol Good catch, I am going to fix that |
👍 |
7109574
to
4b08e61
Compare
@Erol fixed |
Great, thanks! |
Hi @AlfonsoUceda, found another one. Namespaces are being dropped inside nested routes: router.define do
namespace 'api' do
resources 'products' do
resources 'variants'
end
end
end Here are the routes generated:
|
thanks @Erol I'm goint to fix that |
4b08e61
to
8b979f8
Compare
Ok @Erol the problem was I wasn't merging parents options |
Thanks again for this @AlfonsoUceda! |
8b979f8
to
8d58f18
Compare
I added more tests |
@AlfonsoUceda Thank you for this PR 💯 ✨ |
@jodosha IMO add nested routes to member and collection block it doesn't make sense. could you add an example? thanks ;) |
@AlfonsoUceda |
@jodosha how would it be the code (routes definition)? |
fcdb7ed
to
97d069f
Compare
@jodosha I created another router in test, because with namespaced router routes is different, you can see it at the end of the file |
97d069f
to
52f1b95
Compare
This is awesome work! 👍 |
|
||
resource :user do | ||
resources :comments | ||
resource :api_keys |
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.
@AlfonsoUceda Shouldn't be this api_key
instead of api_keys
?
52f1b95
to
e85f7e1
Compare
end | ||
|
||
it 'for resource -> resource' do | ||
@inspector.must_match 'new_user_api_key GET, HEAD /user/:user_id/api_key/new Nested::Controllers::User::ApiKey::New' |
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.
@AlfonsoUceda This is wrong, it should be: /user/api_key/new
. With resource
you never have :user_id
around. That is something that resources
has (notice the plural).
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 are rigth @jodosha I'll fix that ;)
@AlfonsoUceda Here's another issue. Given the following routes: resources :users do
resources :posts
end (byebug) puts @router.inspector
new_users_avatar GET, HEAD /users/:users_id/avatar/new Nested::Controllers::Users::Avatar::New
users_avatar POST /users/:users_id/avatar Nested::Controllers::Users::Avatar::Create
users_avatar GET, HEAD /users/:users_id/avatar Nested::Controllers::Users::Avatar::Show
edit_users_avatar GET, HEAD /users/:users_id/avatar/edit Nested::Controllers::Users::Avatar::Edit
users_avatar PATCH /users/:users_id/avatar Nested::Controllers::Users::Avatar::Update
users_avatar DELETE /users/:users_id/avatar Nested::Controllers::Users::Avatar::Destroy
users_posts GET, HEAD /users/:users_id/posts Nested::Controllers::Users::Posts::Index
new_users_posts GET, HEAD /users/:users_id/posts/new Nested::Controllers::Users::Posts::New
users_posts POST /users/:users_id/posts Nested::Controllers::Users::Posts::Create
users_posts GET, HEAD /users/:users_id/posts/:id Nested::Controllers::Users::Posts::Show
edit_users_posts GET, HEAD /users/:users_id/posts/:id/edit Nested::Controllers::Users::Posts::Edit
users_posts PATCH /users/:users_id/posts/:id Nested::Controllers::Users::Posts::Update
users_posts DELETE /users/:users_id/posts/:id Nested::Controllers::Users::Posts::Destroy
users GET, HEAD /users Nested::Controllers::Users::Index
new_users GET, HEAD /users/new Nested::Controllers::Users::New
users POST /users Nested::Controllers::Users::Create
users GET, HEAD /users/:id Nested::Controllers::Users::Show
edit_users GET, HEAD /users/:id/edit Nested::Controllers::Users::Edit
users PATCH /users/:id Nested::Controllers::Users::Update
users DELETE /users/:id Nested::Controllers::Users::Destroy
nil
(byebug) puts @router.path(:users_posts, users_id: 1, id: 23)
/users/1/posts/23
nil
(byebug) puts @router.path(:users_posts, users_id: 1)
*** HttpRouter::InvalidRouteException Exception: No route (path) could be generated for :users_posts # THIS SHOULD BE GENERATED
nil
(byebug) puts @router.path(:users)
/users
nil
(byebug) puts @router.path(:users, id: 1)
/users/1
nil There is a conflict between Right now in master, we have this conflict as well, but it works fine. See the last two lines above: At this point, instead of fixing this I'm wondering if it's the time to introduce the right pluralization rules. In this way it won't create conflicts, and at the same time is improves the dev experience: The would become: (byebug) puts @router.inspector
new_user_avatar GET, HEAD /users/:user_id/avatar/new Nested::Controllers::Users::Avatar::New
user_avatar POST /users/:user_id/avatar Nested::Controllers::Users::Avatar::Create
user_avatar GET, HEAD /users/:user_id/avatar Nested::Controllers::Users::Avatar::Show
edit_user_avatar GET, HEAD /users/:user_id/avatar/edit Nested::Controllers::Users::Avatar::Edit
user_avatar PATCH /users/:user_id/avatar Nested::Controllers::Users::Avatar::Update
user_avatar DELETE /users/:user_id/avatar Nested::Controllers::Users::Avatar::Destroy
user_posts GET, HEAD /users/:user_id/posts Nested::Controllers::Users::Posts::Index
new_user_post GET, HEAD /users/:user_id/posts/new Nested::Controllers::Users::Posts::New
user_posts POST /users/:user_id/posts Nested::Controllers::Users::Posts::Create
user_post GET, HEAD /users/:user_id/posts/:id Nested::Controllers::Users::Posts::Show
edit_user_post GET, HEAD /users/:user_id/posts/:id/edit Nested::Controllers::Users::Posts::Edit
user_post PATCH /users/:user_id/posts/:id Nested::Controllers::Users::Posts::Update
user_post DELETE /users/:user_id/posts/:id Nested::Controllers::Users::Posts::Destroy
users GET, HEAD /users Nested::Controllers::Users::Index
new_user GET, HEAD /users/new Nested::Controllers::Users::New
users POST /users Nested::Controllers::Users::Create
user GET, HEAD /users/:id Nested::Controllers::Users::Show
edit_user GET, HEAD /users/:id/edit Nested::Controllers::Users::Edit
user PATCH /users/:id Nested::Controllers::Users::Update
user DELETE /users/:id Nested::Controllers::Users::Destroy
nil This would fix the problem above: /cc @joneslee85 |
ca1fc0b
to
2b78dae
Compare
|
||
def _calculate(param_wildcard, resource = nil) | ||
return if resource.nil? | ||
@path << resource.wildcard_param(param_wildcard.pop) |
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.
what do you think about aligning to the same level as return
?
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.
can you more detailed? thanks @joneslee85 ;)
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 think @joneslee85 meant lines 36-38 should have the same indentation. :)
return if resource.nil?
@path << resource.wildcard_param(param_wildcard.pop)
_calculate(param_wildcard, resource.parent_resource)
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.
Thanks guys I didn't see it sorry ;P
9044eea
to
9205df4
Compare
@@ -179,7 +188,8 @@ def rest_path | |||
# @api private | |||
# @since 0.1.0 | |||
def as | |||
namespace.relative_join(resource_name, self.class.named_route_separator).to_sym | |||
singularized_as = resource_name.to_s.split(NESTED_ROUTES_SEPARATOR).map { |name| Lotus::Utils::String.new(name).singularize }.join(self.class.named_route_separator) |
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.
This long is a bit long, can you break into 2 lines?
It is probably the longest PR I've ever reviewed, overall, it looks good to me |
9205df4
to
57b462c
Compare
This PR adds the feature of nested routes to router: