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

[Fix #1238] Add new Rails/EnumSyntax cop #1336

Merged
merged 1 commit into from
Aug 24, 2024

Conversation

koic
Copy link
Member

@koic koic commented Aug 22, 2024

Fixes #1238.
Based on #1239.

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


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@koic koic mentioned this pull request Aug 22, 2024
9 tasks
@koic koic force-pushed the add_new_rails_enum_syntax_cop branch from 0b94def to a72d477 Compare August 22, 2024 09:02
@Earlopain
Copy link
Contributor

Earlopain commented Aug 22, 2024

Can you add a test for defining two enums with one method call?

enum enum1: [:active], enum2: [:archived]

I was curious if this syntax works, and it indeed does:

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", "~> 7.0"
  gem "sqlite3"
end

require "active_record"
require "minitest/autorun"
require "logger"

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :conversations, force: true do |t|
    t.column :enum1, :integer, default: 0
    t.column :enum2, :integer, default: 0
  end
end

class Conversation < ActiveRecord::Base
  enum enum1: [:active], enum2: [:archived]
end

class BugTest < Minitest::Test
  def test_association_stuff
    conversation = Conversation.create!
    assert conversation.active?
    assert conversation.archived?
  end
end

I don't autocorrect is needed for these cases, seems very rare and I don't find documentation for it (though in code, it is pretty explicit https://github.com/rails/rails/blob/914caca2d31bd753f47f9168f2a375921d9e91cc/activerecord/lib/active_record/enum.rb#L160)

@koic koic force-pushed the add_new_rails_enum_syntax_cop branch from a72d477 to 36559ec Compare August 23, 2024 03:54
Defining enums with keyword arguments is deprecated
and will be removed in Rails 8.0.
@koic koic force-pushed the add_new_rails_enum_syntax_cop branch from 36559ec to 2201bd8 Compare August 23, 2024 03:55
@koic
Copy link
Member Author

koic commented Aug 23, 2024

Yeah, autocorrect might be provided in the future, but for now, let's start by registering an offense only.

@Earlopain
Copy link
Contributor

👍 Looks good

@koic koic merged commit aa2bf3e into rubocop:master Aug 24, 2024
14 checks passed
@koic koic deleted the add_new_rails_enum_syntax_cop branch August 24, 2024 02:59
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.

Cop to detect deprecated enums with keyword arguments
3 participants