From 02b06d92f54becf36da6a4d97c39ff790718df64 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 7 Apr 2023 12:55:41 +0900 Subject: [PATCH] Cut 2.19.0 --- CHANGELOG.md | 2 + config/default.yml | 6 +- docs/antora.yml | 2 +- docs/modules/ROOT/pages/cops.adoc | 1 + docs/modules/ROOT/pages/cops_rails.adoc | 120 +++++++++++++++++++----- lib/rubocop/rails/version.rb | 2 +- relnotes/v2.19.0.md | 24 +++++ 7 files changed, 128 insertions(+), 29 deletions(-) create mode 100644 relnotes/v2.19.0.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 0575408bfc..15cd581120 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ ## master (unreleased) +## 2.19.0 (2023-04-07) + ### New features * [#337](https://github.com/rubocop/rubocop-rails/issues/337): Add new `Rails/ThreeStateBooleanColumn` cop. ([@fatkodima][]) diff --git a/config/default.yml b/config/default.yml index 39c6aab247..5fe5788785 100644 --- a/config/default.yml +++ b/config/default.yml @@ -462,7 +462,7 @@ Rails/FindEach: Enabled: true Safe: false VersionAdded: '0.30' - VersionChanged: '<>' + VersionChanged: '2.19' Include: - app/models/**/*.rb AllowedMethods: @@ -845,7 +845,7 @@ Rails/ResponseParsedBody: Enabled: pending Safe: false VersionAdded: '2.18' - VersionChanged: '<>' + VersionChanged: '2.19' Include: - spec/controllers/**/*.rb - spec/requests/**/*.rb @@ -1001,7 +1001,7 @@ Rails/ThreeStateBooleanColumn: Description: 'Add a default value and a `NOT NULL` constraint to boolean columns.' StyleGuide: 'https://rails.rubystyle.guide/#three-state-boolean' Enabled: pending - VersionAdded: '<>' + VersionAdded: '2.19' Include: - db/**/*.rb diff --git a/docs/antora.yml b/docs/antora.yml index 9e0ff48acb..3caa8ea625 100644 --- a/docs/antora.yml +++ b/docs/antora.yml @@ -2,6 +2,6 @@ name: rubocop-rails title: RuboCop Rails # We always provide version without patch here (e.g. 1.1), # as patch versions should not appear in the docs. -version: ~ +version: '2.19' nav: - modules/ROOT/nav.adoc diff --git a/docs/modules/ROOT/pages/cops.adoc b/docs/modules/ROOT/pages/cops.adoc index 2775221eac..c56351643e 100644 --- a/docs/modules/ROOT/pages/cops.adoc +++ b/docs/modules/ROOT/pages/cops.adoc @@ -123,6 +123,7 @@ based on the https://rails.rubystyle.guide/[Rails Style Guide]. * xref:cops_rails.adoc#railssquishedsqlheredocs[Rails/SquishedSQLHeredocs] * xref:cops_rails.adoc#railsstripheredoc[Rails/StripHeredoc] * xref:cops_rails.adoc#railstablenameassignment[Rails/TableNameAssignment] +* xref:cops_rails.adoc#railsthreestatebooleancolumn[Rails/ThreeStateBooleanColumn] * xref:cops_rails.adoc#railstimezone[Rails/TimeZone] * xref:cops_rails.adoc#railstimezoneassignment[Rails/TimeZoneAssignment] * xref:cops_rails.adoc#railstoformatteds[Rails/ToFormattedS] diff --git a/docs/modules/ROOT/pages/cops_rails.adoc b/docs/modules/ROOT/pages/cops_rails.adoc index a5700b45a8..191cdbbd98 100644 --- a/docs/modules/ROOT/pages/cops_rails.adoc +++ b/docs/modules/ROOT/pages/cops_rails.adoc @@ -41,11 +41,6 @@ class HomeController < ApplicationController end ---- -=== References - -* https://rails.rubystyle.guide/#flash-before-render -* https://api.rubyonrails.org/classes/ActionController/FlashBeforeRender.html - == Rails/ActionControllerTestCase |=== @@ -1163,10 +1158,12 @@ tag(name, class: 'classname') | - |=== -Checks the migration for which timestamps are not included -when creating a new table. +Checks the migration for which timestamps are not included when creating a new table. In many cases, timestamps are useful information and should be added. +NOTE: Allow `timestamps` not written when `id: false` because this emphasizes respecting +user's editing intentions. + === Examples [source,ruby] @@ -1203,6 +1200,12 @@ create_table :users do |t| t.datetime :updated_at, default: -> { 'CURRENT_TIMESTAMP' } end + +# good +create_table :users, articles, id: false do |t| + t.integer :user_id + t.integer :article_id +end ---- === Configurable attributes @@ -1386,6 +1389,14 @@ end # good delegate :bar, to: :foo +# bad +def bar + self.bar +end + +# good +delegate :bar, to: :self + # good def bar foo&.bar @@ -2088,9 +2099,8 @@ date.all_year | 2.4 |=== -Identifies usages of file path joining process -to use `Rails.root.join` clause. It is used to add uniformity when -joining paths. +Identifies usages of file path joining process to use `Rails.root.join` clause. +It is used to add uniformity when joining paths. === Examples @@ -2100,11 +2110,16 @@ joining paths. ---- # bad Rails.root.join('app', 'models', 'goober') + +# good +Rails.root.join('app/models/goober') + +# bad File.join(Rails.root, 'app/models/goober') "#{Rails.root}/app/models/goober" # good -Rails.root.join('app/models/goober') +Rails.root.join('app/models/goober').to_s ---- ==== EnforcedStyle: arguments @@ -2113,11 +2128,16 @@ Rails.root.join('app/models/goober') ---- # bad Rails.root.join('app/models/goober') + +# good +Rails.root.join('app', 'models', 'goober') + +# bad File.join(Rails.root, 'app/models/goober') "#{Rails.root}/app/models/goober" # good -Rails.root.join('app', 'models', 'goober') +Rails.root.join('app', 'models', 'goober').to_s ---- === Configurable attributes @@ -2233,14 +2253,19 @@ User.find(id) | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed | Enabled -| Yes -| Yes +| No +| Yes (Unsafe) | 0.30 -| 2.9 +| 2.19 |=== -Identifies usages of `all.each` and -change them to use `all.find_each` instead. +Identifies usages of `all.each` and change them to use `all.find_each` instead. + +=== Safety + +This cop is unsafe if the receiver object is not an Active Record object. +Also, `all.each` returns an `Array` instance and `all.find_each` returns nil, +so the return values are different. === Examples @@ -2506,6 +2531,9 @@ If you are running Rails < 5 you should disable the Rails/HttpPositionalArguments cop or set your TargetRailsVersion in your .rubocop.yml file to 4.2. +NOTE: It does not detect any cases where `include Rack::Test::Methods` is used +which makes the http methods incompatible behavior. + === Examples [source,ruby] @@ -4450,7 +4478,7 @@ end | Enabled | No -| No +| Yes (Unsafe) | 0.64 | 2.10 |=== @@ -4770,19 +4798,19 @@ require_dependency 'some_lib' | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed | Pending -| Yes +| No | Yes (Unsafe) | 2.18 -| - +| 2.19 |=== Prefer `response.parsed_body` to `JSON.parse(response.body)`. === Safety -This cop's autocorrection is unsafe because Content-Type may not be `application/json`. For example, the -proprietary Content-Type provided by corporate entities such as `application/vnd.github+json` is not -supported at `response.parsed_body` by default, so you still have to use `JSON.parse(response.body)` there. +This cop is unsafe because Content-Type may not be `application/json`. For example, the proprietary +Content-Type provided by corporate entities such as `application/vnd.github+json` is not supported at +`response.parsed_body` by default, so you still have to use `JSON.parse(response.body)` there. === Examples @@ -5801,6 +5829,50 @@ self.table_name = :some_other_name * https://rails.rubystyle.guide/#keep-ar-defaults +== Rails/ThreeStateBooleanColumn + +|=== +| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed + +| Pending +| Yes +| No +| 2.19 +| - +|=== + +Enforces that boolean columns are created with default values (`false` or `true`) and +`NOT NULL` constraint. + +=== Examples + +[source,ruby] +---- +# bad +add_column :users, :active, :boolean +t.column :active, :boolean +t.boolean :active + +# good +add_column :users, :active, :boolean, default: true, null: false +t.column :active, :boolean, default: true, null: false +t.boolean :active, default: true, null: false +---- + +=== Configurable attributes + +|=== +| Name | Default value | Configurable values + +| Include +| `+db/**/*.rb+` +| Array +|=== + +=== References + +* https://rails.rubystyle.guide/#three-state-boolean + == Rails/TimeZone |=== @@ -6200,7 +6272,7 @@ customer.favourites.distinct.pluck(:color) |=== When you define a uniqueness validation in Active Record model, -you also should add a unique index for the column. There are two reasons +you also should add a unique index for the column. There are two reasons. First, duplicated records may occur even if Active Record's validation is defined. Second, it will cause slow queries. The validation executes a `SELECT` diff --git a/lib/rubocop/rails/version.rb b/lib/rubocop/rails/version.rb index 4517b3af13..887f195c0d 100644 --- a/lib/rubocop/rails/version.rb +++ b/lib/rubocop/rails/version.rb @@ -4,7 +4,7 @@ module RuboCop module Rails # This module holds the RuboCop Rails version information. module Version - STRING = '2.18.0' + STRING = '2.19.0' def self.document_version STRING.match('\d+\.\d+').to_s diff --git a/relnotes/v2.19.0.md b/relnotes/v2.19.0.md new file mode 100644 index 0000000000..14677d33a0 --- /dev/null +++ b/relnotes/v2.19.0.md @@ -0,0 +1,24 @@ +### New features + +* [#337](https://github.com/rubocop/rubocop-rails/issues/337): Add new `Rails/ThreeStateBooleanColumn` cop. ([@fatkodima][]) +* [#45](https://github.com/rubocop/rubocop-rails/issues/45): Make `Rails/Delegate` aware of `self`. ([@koic][]) + +### Bug fixes + +* [#934](https://github.com/rubocop/rubocop-rails/issues/934): Fix a false negative for `Rails/Output` when print methods without arguments. ([@koic][]) +* [#99](https://github.com/rubocop/rubocop-rails/issues/99): Fix a false positive for `Rails/HttpPositionalArguments` when using `include Rack::Test::Methods`. ([@koic][]) +* [#501](https://github.com/rubocop/rubocop-rails/issues/501): Fix a false positive for `Rails/OutputSafety` when using `html_safe` for `I18n` methods. ([@koic][]) +* [#860](https://github.com/rubocop/rubocop-rails/issues/860): Fix a false positive for `Rails/Pluck` when using regexp literal key for `String#[]`. ([@koic][]) +* [#938](https://github.com/rubocop/rubocop-rails/issues/938): Fix an error for `Rails/WhereNotWithMultipleConditions` when using `where.not.lt(condition)` as a Mongoid API'. ([@koic][]) +* [#941](https://github.com/rubocop/rubocop-rails/issues/941): Remove redundant config for `Style/InverseMethods`. ([@koic][]) + +### Changes + +* [#299](https://github.com/rubocop/rubocop-rails/pull/299): Add autocorrection for `Rails/ReflectionClassName`. ([@tejasbubane][]) +* [#34](https://github.com/rubocop/rubocop-rails/issues/34): Allow `CreateTableWithTimestamps` when using `id: false` and not include `timestamps`. ([@koic][]) +* [#694](https://github.com/rubocop/rubocop-rails/issues/694): Mark `Rails/FindEach` as unsafe. ([@koic][]) +* [#940](https://github.com/rubocop/rubocop-rails/issues/940): Mark `Rails/ResponseParsedBody` as unsafe. ([@koic][]) + +[@fatkodima]: https://github.com/fatkodima +[@koic]: https://github.com/koic +[@tejasbubane]: https://github.com/tejasbubane