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

Add Support for classes defined with compact style #364

Merged
merged 1 commit into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

* [#362](https://github.com/rubocop-hq/rubocop-rails/pull/362): Add new `Rails/WhereEquals` cop. ([@eugeneius][])

### Bug fixes

* [#364](https://github.com/rubocop-hq/rubocop-rails/pull/364): Fix a problem that `Rails/UniqueValidationWithoutIndex` doesn't work in classes defined with compact style. ([@sinsoku][])

## 2.8.1 (2020-09-16)

### Bug fixes
Expand Down
7 changes: 4 additions & 3 deletions lib/rubocop/cop/mixin/active_record_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ def table_name(class_node)
table_name = find_set_table_name(class_node).to_a.last&.first_argument
return table_name.value.to_s if table_name

namespaces = class_node.each_ancestor(:class, :module)
[class_node, *namespaces]
class_nodes = class_node.defined_module.each_node
namespaces = class_node.each_ancestor(:class, :module).map(&:identifier)
[*class_nodes, *namespaces]
.reverse
.map { |klass| klass.identifier.children[1] }.join('_')
.map { |node| node.children[1] }.join('_')
.tableize
end

Expand Down
56 changes: 56 additions & 0 deletions spec/rubocop/cop/active_record_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,60 @@
it { is_expected.to be_nil }
end
end

describe '#table_name' do
subject { cop.table_name(class_node) }

context 'when the class is simple' do
let(:class_node) { parse_source(<<~RUBY).ast }
class User
end
RUBY

it { is_expected.to eq 'users' }
end

context 'when the self.table_name is set' do
let(:class_node) { parse_source(<<~RUBY).ast }
class Foo
self.table_name = 'bar'
end
RUBY

it { is_expected.to eq 'bar' }
end

context 'when the class is defined in a module' do
let(:class_node) { parse_source(<<~RUBY).ast.each_child_node(:class).first }
module Admin
class User
end
end
RUBY

it { is_expected.to eq 'admin_users' }
end

context 'when the class is defined in nested modules' do
let(:class_node) { parse_source(<<~RUBY).ast.each_descendant(:class).first }
module Cop
module Admin
class User
end
end
end
RUBY

it { is_expected.to eq 'cop_admin_users' }
end

context 'when the class is defined with compact style' do
let(:class_node) { parse_source(<<~RUBY).ast }
class Cop::Admin::User
end
RUBY

it { is_expected.to eq 'cop_admin_users' }
end
end
end
13 changes: 11 additions & 2 deletions spec/rubocop/cop/rails/unique_validation_without_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ class User
end
end

context 'with nested class' do
context 'with namespaced model' do
let(:schema) { <<~RUBY }
ActiveRecord::Schema.define(version: 2020_02_02_075409) do
create_table "admin_users", force: :cascade do |t|
Expand All @@ -402,7 +402,7 @@ class User
end
RUBY

it 'registers an offense' do
it 'registers an offense for nested class' do
expect_offense(<<~RUBY)
module Admin
class User
Expand All @@ -412,6 +412,15 @@ class User
end
RUBY
end

it 'registers an offense for compact styled class' do
expect_offense(<<~RUBY)
class Admin::User
validates :account, uniqueness: true
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Uniqueness validation should be with a unique index.
end
RUBY
end
end

context 'with expression indexes' do
Expand Down