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

Add "Positional arguments" subsection #356

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,8 @@ end

=== Enums [[enums]]

=== Hash syntax [[hash-syntax]]
Copy link
Member

Choose a reason for hiding this comment

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

This should be a sub-section, so four ====


Prefer using the hash syntax for `enum`. Array makes the database values implicit
& any insertion/removal/rearrangement of values in the middle will most probably
lead to broken code.
Expand All @@ -440,6 +442,22 @@ class Transaction < ApplicationRecord
end
----

=== Positional arguments [[positional-arguments]]

Prefer using positional arguments instead of keyword arguments when defining enums.
Copy link
Member

Choose a reason for hiding this comment

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

I’m on the fence if this should be in the guide.
I can see the Rails team trying to get rid of the code that detects the enum attribute name.

I can also see potential mistakes like enum status: {a: 1}, priority: {high:1,…}. But is it common?

But how does that stylistically differ for the rest of us? Why do people need to care? If it’s older Rails - use any of the teo. If it’s new, only the positional would work.

Copy link
Author

Choose a reason for hiding this comment

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

You're right - since deprecation notice captures the old syntax, seems like we don't need to have a dedicated cop for this at all, I'll close both PRs 😬

thanks for the feedback!

Defining enums with keyword arguments is deprecated and will be removed in Rails 7.3.

[source,ruby]
----
class Transaction < ApplicationRecord
# bad
enum status: { active: 0, archived: 1 }, _prefix: true

# good
enum :status, { active: 0, archived: 1 }, prefix: true
end
----

=== Macro Style Methods [[macro-style-methods]]

Group macro-style methods (`has_many`, `validates`, etc) in the beginning of the class definition.
Expand Down