|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RuboCop::Cop::Rails::TableNameAssignment, :config do |
| 4 | + context 'when table_name is defined' do |
| 5 | + context 'when string' do |
| 6 | + it 'registers an offense' do |
| 7 | + expect_offense(<<~RUBY) |
| 8 | + class AModule::SomeModel < ApplicationRecord |
| 9 | + self.table_name = 'some_other_table_name' |
| 10 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use `self.table_name =`. |
| 11 | + end |
| 12 | + RUBY |
| 13 | + end |
| 14 | + end |
| 15 | + |
| 16 | + context 'when string = foo' do |
| 17 | + it 'registers an offense' do |
| 18 | + expect_offense(<<~RUBY) |
| 19 | + class AModule::SomeModel < ApplicationRecord |
| 20 | + self.table_name = 'foo' |
| 21 | + ^^^^^^^^^^^^^^^^^^^^^^^ Do not use `self.table_name =`. |
| 22 | + end |
| 23 | + RUBY |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + context 'when symbol' do |
| 28 | + it 'registers an offense' do |
| 29 | + expect_offense(<<~RUBY) |
| 30 | + class AModule::SomeModel < ApplicationRecord |
| 31 | + self.table_name = :some_other_table_name |
| 32 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use `self.table_name =`. |
| 33 | + end |
| 34 | + RUBY |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + context 'when symbol = :foo' do |
| 39 | + it 'registers an offense' do |
| 40 | + expect_offense(<<~RUBY) |
| 41 | + class AModule::SomeModel < ApplicationRecord |
| 42 | + self.table_name = :foo |
| 43 | + ^^^^^^^^^^^^^^^^^^^^^^ Do not use `self.table_name =`. |
| 44 | + end |
| 45 | + RUBY |
| 46 | + end |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + context 'when table_name is not defined' do |
| 51 | + it 'does not register an offense' do |
| 52 | + expect_no_offenses(<<~RUBY) |
| 53 | + class AModule::SomeModel < ApplicationRecord |
| 54 | + has_many :other_thing |
| 55 | + has_one :parent_thing |
| 56 | + end |
| 57 | + RUBY |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + # Case for STI base classes |
| 62 | + context 'when class is named `Base`' do |
| 63 | + context 'when class is declared with module' do |
| 64 | + it 'does not register an offense' do |
| 65 | + expect_no_offenses(<<~RUBY) |
| 66 | + class A::B::Base < ApplicationRecord |
| 67 | + has_many :other_thing |
| 68 | + has_one :parent_thing |
| 69 | +
|
| 70 | + self.table_name = 'special_table_name' |
| 71 | + end |
| 72 | + RUBY |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + context 'when class is declared within module' do |
| 77 | + it 'does not register an offense' do |
| 78 | + expect_no_offenses(<<~RUBY) |
| 79 | + module A |
| 80 | + module B |
| 81 | + class Base < ApplicationRecord |
| 82 | + has_many :other_thing |
| 83 | + has_one :parent_thing |
| 84 | +
|
| 85 | + self.table_name = 'special_table_name' |
| 86 | + end |
| 87 | + end |
| 88 | + end |
| 89 | + RUBY |
| 90 | + end |
| 91 | + end |
| 92 | + end |
| 93 | +end |
0 commit comments