Skip to content

Commit

Permalink
Merge pull request #1271 from koic/fix_an_incorrect_autocorrect_for_r…
Browse files Browse the repository at this point in the history
…ails_validation

[Fix #1270] Fix an incorrect autocorrect for `Rails/Validation`
  • Loading branch information
koic committed Apr 19, 2024
2 parents 891de22 + 6cf1d84 commit cf400eb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
@@ -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
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
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 cf400eb

Please sign in to comment.