rubocop / rails-style-guide

A community-driven Ruby on Rails style guide
http://rails.rubystyle.guide
6.48k stars 1.06k forks source link

Controller action ordering #289

Open mollerhoj opened 3 years ago

mollerhoj commented 3 years ago

The default order of controller actions in rails scaffolds are:

index show new edit create update destroy

I often see controllers where this order is not followed. For consistency's sake, I'd like to have a rubocop to enforce this ordering of the public methods in my controllers.

As a side note: We strive towards never using any other actions in our controllers (https://www.youtube.com/watch?v=HctYHe-YjnE)

pirj commented 3 years ago

I'd love to see a confirmation that this order is followed by real-world Rails apps.

andyw8 commented 3 years ago

Just for the sake of discussion, another possible way of ordering could be to follow CRUD, e.g.:

new create index show edit update destroy

andyw8 commented 3 years ago

Also, as this Stack Overflow post says, some people may prefer to group the new/create and edit/update pairs together.

mollerhoj commented 3 years ago

I honestly like the CRUD order better, but consistency is my no. 1 priority. (I really just want to see a rubocop, but they refered me to this guide).

koic commented 3 years ago

The default order of controller actions in rails scaffolds are:

I agree with the default order if this rule is needed. I think it's better to follow the scaffold order. This is the official starting point of Rails.

pirj commented 3 years ago

What do you think of a soft wording for the guideline? e.g.

Use the default order of resourceful actions in your controller. Feel free to intermingle them with additional non-resourceful actions deliberately.

mollerhoj commented 3 years ago

I don't think the default order is logical - How about "Use a consistent order of resourceful actions in your controller"

andyw8 commented 3 years ago

Feel free to intermingle them with additional non-resourceful actions deliberately.

This feels like it encourages non-resourceful actions, rather than just permitting them. How about:

Use a consistent order for the resourceful actions in your controller. Non-resourceful actions can be interspersed where needed.

ydakuka commented 10 months ago

The default order of controller actions in rails scaffolds are: index show new edit create update destroy

Yes, and the order is grouped by HTTP method.

However, the order displayed at http://localhost:3000/rails/info/routes is different: index create new edit show update destroy

andyw8 commented 10 months ago

Since it's proving difficult to reach an agreed ordering, perhaps the guideline should just be to use a consistent ordering.

A cop can then be written to allow the developer to configure their preferred order (without any default).

ydakuka commented 10 months ago

A cop can then be written to allow the developer to configure their preferred order (without any default).

The cop has been already written.

I believe the issue remains open due to this comment.