Skip to content

Commit

Permalink
[Fix rubocop#1270] Fix an incorrect autocorrect for Rails/Validation
Browse files Browse the repository at this point in the history
Fixes rubocop#1270.

This PR fixes an incorrect autocorrect for `Rails/Validation`
when using `validates_size_of`.
  • Loading branch information
koic authored and fwolfst committed Jun 3, 2024
1 parent 6bad31b commit 6ba16df
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1270](https://github.com/rubocop/rubocop-rails/issues/1270): Fix an incorrect autocorrect for `Rails/Validation` when using `validates_size_of`. ([@koic][])
6 changes: 4 additions & 2 deletions lib/rubocop/cop/rails/validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module Rails
# validates :foo, numericality: true
# validates :foo, presence: true
# validates :foo, absence: true
# validates :foo, size: true
# validates :foo, length: true
# validates :foo, uniqueness: true
#
class Validation < Base
Expand Down Expand Up @@ -120,7 +120,9 @@ def correct_validate_type_for_array(corrector, node, arguments, loc)
end

def validate_type(node)
node.method_name.to_s.split('_')[1]
type = node.method_name.to_s.split('_')[1]

type == 'size' ? 'length' : type
end

def frozen_array_argument?(argument)
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/rails/validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
described_class::TYPES.each do |type|
context "with validates_#{type}_of" do
let(:autocorrected_source) do
type = 'length' if type == 'size'

"validates :full_name, :birth_date, #{type}: true"
end

Expand All @@ -40,6 +42,8 @@

context "with validates_#{type}_of when method arguments are enclosed in parentheses" do
let(:autocorrected_source) do
type = 'length' if type == 'size'

"validates(:full_name, :birth_date, #{type}: true)"
end

Expand All @@ -52,6 +56,8 @@

context "with validates_#{type}_of when attributes are specified with array literal" do
let(:autocorrected_source) do
type = 'length' if type == 'size'

"validates :full_name, :birth_date, #{type}: true"
end

Expand All @@ -64,6 +70,8 @@

context "with validates_#{type}_of when attributes are specified with frozen array literal" do
let(:autocorrected_source) do
type = 'length' if type == 'size'

"validates :full_name, :birth_date, #{type}: true"
end

Expand All @@ -76,6 +84,8 @@

context "with validates_#{type}_of when attributes are specified with symbol array literal" do
let(:autocorrected_source) do
type = 'length' if type == 'size'

"validates :full_name, :birth_date, #{type}: true"
end

Expand All @@ -88,6 +98,8 @@

context "with validates_#{type}_of when attributes are specified with frozen symbol array literal" do
let(:autocorrected_source) do
type = 'length' if type == 'size'

"validates :full_name, :birth_date, #{type}: true"
end

Expand Down

0 comments on commit 6ba16df

Please sign in to comment.