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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inferred defaults for routes #1297

Open
bkuhlmann opened this issue Mar 8, 2023 · 4 comments
Open

Add inferred defaults for routes #1297

bkuhlmann opened this issue Mar 8, 2023 · 4 comments

Comments

@bkuhlmann
Copy link

bkuhlmann commented Mar 8, 2023

Overview

Hello. 馃憢 I'm finding there is a lot of boilerplate code necessary to build routes especially if you want them named by default so you can leverage named routes like the following:

  • routes.path :api_tasks
  • routes.url :api_tasks

I'd like to propose that, when building routes, only the minimum syntax is necessary which infers safe defaults while still allowing complete flexibility to customize as necessary.

Steps to Recreate

Consider the following:

slice :api, at: "/api" do
  get "/tasks", to: "tasks.index", as: :tasks
  get "/tasks/:id", to: "tasks.show", as: :task
  get "/tasks/new", to: "tasks.new", as: :task_new
  get "/tasks/:id/edit", to: "tasks.edit", as: :task_edit
  post "/tasks", to: "tasks.create", as: :task_create
  patch "/tasks/:id", to: "tasks.patch", as: :task_patch
  put "/tasks/:id", to: "tasks.update", as: :task_update
  delete "/tasks/:id", to: "tasks.delete", as: :task_delete

  scope :chat do
    post "/slack", to: "chat.slack.create", as: :slack_create
  end
end

The above requires a lot of typing for little gain. Especially, since all of the above could be inferred from that path. Instead, I'd like to suggest that you be able to write the above as follows:

slice :api do
  get "/tasks"
  get "/tasks/:id"
  get "/tasks/new"
  get "/tasks/:id/edit"
  post "/tasks"
  patch "/tasks/:id"
  put "/tasks/:id"
  delete "/tasks/:id"

  scope :chat do
    post "/slack"
  end
end

The last code snippet is identical in behavior to the first snippet but without all of the typing! This can be done by translating the given block context plus the route path into the necessary keys. Here's a few examples:

  • Key: "tasks.index", Named Route: :tasks
  • Key: "tasks.show", Names Route: :task
  • Key: "chat.slack.create", Names Route: :slack_create

At any time you could override the inferred defaults by customizing :at, :to, and :as as you see fit but if you just wanted to roll with the inferred defaults you'd save yourself a lot of time.

Environment

  • Ruby: ruby 3.2.1 (2023-02-08 revision 31819e82c8) +YJIT [arm64-darwin22.3.0]
  • Hanami: 2.0.3

Notes

This issue came about from the discussion between Peter and me below so I've rephrased this issue to have a bit more impact.

@solnic
Copy link
Member

solnic commented Mar 9, 2023

I don't think it should. You may want to use whatever object as the handler. Setting the namespace implicitly would make it impossible.

What I would suggest instead would be setting to: by default by inferring its value from the context and the path.

@bkuhlmann
Copy link
Author

bkuhlmann commented Mar 9, 2023

Actually, yes, that would be a nice quality of life improvement. Expanding upon this, it would be nice to take this boilerplate code:

slice :api, at: "/api" do
  get "/tasks", to: "tasks.index", as: :tasks
  get "/tasks/:id", to: "tasks.show", as: :task
  get "/tasks/new", to: "tasks.new", as: :task_new
  get "/tasks/:id/edit", to: "tasks.edit", as: :task_edit
  post "/tasks", to: "tasks.create", as: :task_create
  patch "/tasks/:id", to: "tasks.patch", as: :task_patch
  put "/tasks/:id", to: "tasks.update", as: :task_update
  delete "/tasks/:id", to: "tasks.delete", as: :task_delete

  scope :chat do
    post "/slack", to: "chat.slack.create", as: :slack_create
  end
end

...and turn it into this:

slice :api do
  get "/tasks"
  get "/tasks/:id"
  get "/tasks/new"
  get "/tasks/:id/edit"
  post "/tasks"
  patch "/tasks/:id"
  put "/tasks/:id"
  delete "/tasks/:id"

  scope :chat do
    post "/slack"
  end
end

The last code snippet would be identical in behavior to the first snippet but without all of the typing! You could then override the inferred defaults -- per your suggestion -- by customizing :at, :to, and :as as you see fit but if you just wanted to roll with the inferred defaults you'd save yourself a lot of time.

Happy to rewrite this issue to reflect the above if you think this would be a welcomed change?

@bkuhlmann bkuhlmann changed the title Add scoped route key name Add inferred defaults for routes Mar 10, 2023
@bkuhlmann
Copy link
Author

Peter: I opted to rename and reword this issue based on our discussion since the resolution would have more impact to the community at large.

@4D-Coder
Copy link

Hi! I'm just introducing myself to Hanami and would like to try my hand at this issue!

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

No branches or pull requests

3 participants