Skip to content

Add singular resource routing and specs#247

Open
anuj-pal27 wants to merge 2 commits intorage-rb:mainfrom
anuj-pal27:feature/resource-helper
Open

Add singular resource routing and specs#247
anuj-pal27 wants to merge 2 commits intorage-rb:mainfrom
anuj-pal27:feature/resource-helper

Conversation

@anuj-pal27
Copy link
Copy Markdown
Contributor

@anuj-pal27 anuj-pal27 commented Mar 25, 2026

Summary

Implements the singular resource route helper in the router DSL and updates the spec suite to validate all supported options and behaviors.

Changes

  • Add resource helper routes for singular resources.
  • Include GET /:resource/new in the default action set.
  • Respect only / except filters.
  • Ignore param: for singular resources (but raise on invalid values).
  • Update specs for modules, paths, scopes, nesting, and multiple resources.

Testing

  • bundle exec rubocop spec/router/dsl_spec.rb
  • bundle exec rspec spec/router/dsl_spec.rb

fix #119

Copy link
Copy Markdown
Member

@rsamoilov rsamoilov left a comment

Choose a reason for hiding this comment

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

Hi @anuj-pal27,

Left several comments. Apart from that, looks good 👍

return
end

_module, _path, _only, _except, _param = opts.values_at(:module, :path, :only, :except, :param)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

_param doesn't seem to be used in this method.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review! You're right, I'll remove _param from the values_at call since singular resources don't use a param in the URL.


resource = _resources[0].to_s
__resource_scope(resource, _path, _module) do
get("/new", to: "#{resource}#new") if actions.include?(:new)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we actually need the new route? I'm not so sure.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added it based on the issue description which mentioned the /new route. I don’t think we need the new route here. Rage is API-only and resources already doesn’t generate new/edit, so generating GET /resource/new would be inconsistent and mostly useful for HTML form flows. I’ll remove :new from the default actions and drop the /new route.

Comment on lines +381 to +386
get("/new", to: "#{resource}#new") if actions.include?(:new)
post("/", to: "#{resource}#create") if actions.include?(:create)
get("/", to: "#{resource}#show") if actions.include?(:show)
patch("/", to: "#{resource}#update") if actions.include?(:update)
put("/", to: "#{resource}#update") if actions.include?(:update)
delete("/", to: "#{resource}#destroy") if actions.include?(:destroy)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We need to ensure resource is pluralised here - Rails maps singular resources to plural controllers.

You can use to_singular as a reference when implementing this. Feel free to keep it simple - Active Support is almost always loaded.

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.

[API] Add the resource route helper

2 participants