@@ -2661,13 +2661,17 @@ render json: { foo: 'bar' }, status: 200
2661
2661
render plain: 'foo/bar', status: 304
2662
2662
redirect_to root_url, status: 301
2663
2663
head 200
2664
+ assert_response 200
2665
+ assert_redirected_to '/some/path', status: 301
2664
2666
2665
2667
# good
2666
2668
render :foo, status: :ok
2667
2669
render json: { foo: 'bar' }, status: :ok
2668
2670
render plain: 'foo/bar', status: :not_modified
2669
2671
redirect_to root_url, status: :moved_permanently
2670
2672
head :ok
2673
+ assert_response :ok
2674
+ assert_redirected_to '/some/path', status: :moved_permanently
2671
2675
----
2672
2676
2673
2677
==== EnforcedStyle: numeric
@@ -2680,13 +2684,17 @@ render json: { foo: 'bar' }, status: :not_found
2680
2684
render plain: 'foo/bar', status: :not_modified
2681
2685
redirect_to root_url, status: :moved_permanently
2682
2686
head :ok
2687
+ assert_response :ok
2688
+ assert_redirected_to '/some/path', status: :moved_permanently
2683
2689
2684
2690
# good
2685
2691
render :foo, status: 200
2686
2692
render json: { foo: 'bar' }, status: 404
2687
2693
render plain: 'foo/bar', status: 304
2688
2694
redirect_to root_url, status: 301
2689
2695
head 200
2696
+ assert_response 200
2697
+ assert_redirected_to '/some/path', status: 301
2690
2698
----
2691
2699
2692
2700
=== Configurable attributes
@@ -3659,13 +3667,25 @@ hash.exclude?(:key)
3659
3667
| 2.20
3660
3668
|===
3661
3669
3662
- Checks for add_column call with NOT NULL constraint in migration file.
3670
+ Checks for add_column calls with a NOT NULL constraint without a default
3671
+ value.
3663
3672
3664
- `TEXT` can have default values in PostgreSQL, but not in MySQL.
3665
- It will automatically detect an adapter from `development` environment
3666
- in `config/database.yml` or the environment variable `DATABASE_URL`
3667
- when the `Database` option is not set. If the database is MySQL,
3668
- this cop ignores offenses for the `TEXT`.
3673
+ This cop only applies when adding a column to an existing table, since
3674
+ existing records will not have a value for the new column. New tables
3675
+ can freely use NOT NULL columns without defaults, since there are no
3676
+ records that could violate the constraint.
3677
+
3678
+ If you need to add a NOT NULL column to an existing table, you must add
3679
+ it as nullable first, back-fill the data, and then use
3680
+ `change_column_null`. Alternatively, you could add the column with a
3681
+ default first to have the database automatically backfill existing rows,
3682
+ and then use `change_column_default` to remove the default.
3683
+
3684
+ `TEXT` cannot have a default value in MySQL.
3685
+ The cop will automatically detect an adapter from `development`
3686
+ environment in `config/database.yml` or the environment variable
3687
+ `DATABASE_URL` when the `Database` option is not set. If the database
3688
+ is MySQL, this cop ignores offenses for `TEXT` columns.
3669
3689
3670
3690
=== Examples
3671
3691
@@ -3674,12 +3694,18 @@ this cop ignores offenses for the `TEXT`.
3674
3694
# bad
3675
3695
add_column :users, :name, :string, null: false
3676
3696
add_reference :products, :category, null: false
3697
+ change_table :users do |t|
3698
+ t.string :name, null: false
3699
+ end
3677
3700
3678
3701
# good
3679
3702
add_column :users, :name, :string, null: true
3680
3703
add_column :users, :name, :string, null: false, default: ''
3704
+ change_table :users do |t|
3705
+ t.string :name, null: false, default: ''
3706
+ end
3681
3707
add_reference :products, :category
3682
- add_reference :products, :category, null: false, default: 1
3708
+ change_column_null :products, :category_id, false
3683
3709
----
3684
3710
3685
3711
=== Configurable attributes
@@ -3869,6 +3895,10 @@ Using `pluck` followed by `first` creates an intermediate array, which
3869
3895
`pick` avoids. When called on an Active Record relation, `pick` adds a
3870
3896
limit to the query so that only one value is fetched from the database.
3871
3897
3898
+ Note that when `pick` is added to a relation with an existing limit, it
3899
+ causes a subquery to be added. In most cases this is undesirable, and
3900
+ care should be taken while resolving this violation.
3901
+
3872
3902
=== Safety
3873
3903
3874
3904
This cop is unsafe because `pluck` is defined on both `ActiveRecord::Relation` and `Enumerable`,
@@ -6612,17 +6642,23 @@ Rails.env == 'production'
6612
6642
|===
6613
6643
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
6614
6644
6615
- | Pending
6645
+ | Disabled
6616
6646
| Yes
6617
6647
| No
6618
6648
| 2.11
6619
- | -
6649
+ | 2.25
6620
6650
|===
6621
6651
6622
6652
Suggests you remove a column that does not exist in the schema from `ignored_columns`.
6623
6653
`ignored_columns` is necessary to drop a column from RDBMS, but you don't need it after the migration
6624
6654
to drop the column. You avoid forgetting to remove `ignored_columns` by this cop.
6625
6655
6656
+ IMPORTANT: This cop can't be used to effectively check for unused columns because the development
6657
+ and production schema can be out of sync until the migration has been run on production. As such,
6658
+ this cop can cause `ignored_columns` to be removed even though the production schema still contains
6659
+ the column, which can lead to downtime when the migration is actually executed. Only enable this cop
6660
+ if you know your migrations will be run before any of your Rails applications boot with the modified code.
6661
+
6626
6662
=== Examples
6627
6663
6628
6664
[source,ruby]
@@ -6729,7 +6765,7 @@ validates :foo, length: true
6729
6765
validates :foo, numericality: true
6730
6766
validates :foo, presence: true
6731
6767
validates :foo, absence: true
6732
- validates :foo, size : true
6768
+ validates :foo, length : true
6733
6769
validates :foo, uniqueness: true
6734
6770
----
6735
6771
@@ -6984,3 +7020,48 @@ User.where.not('trashed = ? OR role = ?', true, 'admin')
6984
7020
=== References
6985
7021
6986
7022
* https://rails.rubystyle.guide/#where-not-with-multiple-attributes
7023
+
7024
+ == Rails/WhereRange
7025
+
7026
+ NOTE: Required Ruby version: 2.6
7027
+
7028
+ |===
7029
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
7030
+
7031
+ | Pending
7032
+ | Yes
7033
+ | Always
7034
+ | 2.25
7035
+ | -
7036
+ |===
7037
+
7038
+ Identifies places where manually constructed SQL
7039
+ in `where` can be replaced with ranges.
7040
+
7041
+ === Examples
7042
+
7043
+ [source,ruby]
7044
+ ----
7045
+ # bad
7046
+ User.where('age >= ?', 18)
7047
+ User.where.not('age >= ?', 18)
7048
+ User.where('age < ?', 18)
7049
+ User.where('age >= ? AND age < ?', 18, 21)
7050
+ User.where('age >= :start', start: 18)
7051
+ User.where('users.age >= ?', 18)
7052
+
7053
+ # good
7054
+ User.where(age: 18..)
7055
+ User.where.not(age: 18..)
7056
+ User.where(age: ...18)
7057
+ User.where(age: 18...21)
7058
+ User.where(users: { age: 18.. })
7059
+
7060
+ # good
7061
+ # There are no beginless ranges in ruby.
7062
+ User.where('age > ?', 18)
7063
+ ----
7064
+
7065
+ === References
7066
+
7067
+ * https://rails.rubystyle.guide/#where-ranges
0 commit comments