forked from rubocop/rubocop-rails
/
reflection_class_name_spec.rb
131 lines (110 loc) · 4.05 KB
/
reflection_class_name_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Rails::ReflectionClassName, :config do
context "registers an offense when using `foreign_key: 'account_id'`" do
it 'has_many' do
expect_offense(<<~RUBY)
has_many :accounts, class_name: Account, foreign_key: :account_id
^^^^^^^^^^^^^^^^^^^ Use a string value for `class_name`.
RUBY
end
it '.name' do
expect_offense(<<~RUBY)
has_many :accounts, class_name: Account.name
^^^^^^^^^^^^^^^^^^^^^^^^ Use a string value for `class_name`.
RUBY
end
it '.to_s' do
expect_offense(<<~RUBY)
has_many :accounts, class_name: Account.to_s
^^^^^^^^^^^^^^^^^^^^^^^^ Use a string value for `class_name`.
RUBY
end
it 'has_one' do
expect_offense(<<~RUBY)
has_one :account, class_name: Account
^^^^^^^^^^^^^^^^^^^ Use a string value for `class_name`.
RUBY
end
it 'belongs_to' do
expect_offense(<<~RUBY)
belongs_to :account, class_name: Account
^^^^^^^^^^^^^^^^^^^ Use a string value for `class_name`.
RUBY
end
end
context 'when a relation has a scope parameter' do
it 'registers an offense' do
expect_offense(<<~RUBY)
belongs_to :account, -> { distinct }, class_name: Account
^^^^^^^^^^^^^^^^^^^ Use a string value for `class_name`.
RUBY
end
end
it 'does not register an offense when using string with interpolation' do
expect_no_offenses(<<~'RUBY')
has_many :accounts, class_name: "#{prefix}Account"
RUBY
end
it 'does not register an offense when using `class_name: do_something.to_s`' do
expect_no_offenses(<<~'RUBY')
has_many :accounts, class_name: do_something.to_s
RUBY
end
it 'does not register an offense when using `class_name: to_s`' do
expect_no_offenses(<<~'RUBY')
has_many :accounts, class_name: to_s
RUBY
end
it 'does not register an offense when using `foreign_key :account_id`' do
expect_no_offenses(<<~RUBY)
has_many :accounts, class_name: 'Account', foreign_key: :account_id
has_one :account, class_name: 'Account'
belongs_to :account, class_name: 'Account'
RUBY
end
it 'does not register an offense when using symbol for `class_name`' do
expect_no_offenses(<<~RUBY)
has_many :accounts, class_name: :Account, foreign_key: :account_id
has_one :account, class_name: :Account
belongs_to :account, class_name: :Account
RUBY
end
it 'registers an offense when parameter value is a local variable assigned a constant' do
expect_offense(<<~RUBY)
class_name = Account
has_many :accounts, class_name: class_name
^^^^^^^^^^^^^^^^^^^^^^ Use a string value for `class_name`.
RUBY
end
it 'does not register an offense when parameter value is a local variable assigned a string' do
expect_no_offenses(<<~RUBY)
class_name = 'Account'
has_many :accounts, class_name: class_name
RUBY
end
it 'does not register an offense when parameter value is a method call' do
expect_no_offenses(<<~RUBY)
has_many :accounts, class_name: class_name
RUBY
end
context 'Ruby >= 3.1', :ruby31 do
it 'registers an offense when shorthand syntax value is a local variable assigned a constant' do
expect_offense(<<~RUBY)
class_name = Account
has_many :accounts, class_name:
^^^^^^^^^^^ Use a string value for `class_name`.
RUBY
end
it 'does not register an offense when shorthand syntax value is a local variable assigned a string' do
expect_no_offenses(<<~RUBY)
class_name = 'Account'
has_many :accounts, class_name:
RUBY
end
it 'does not register an offense when shorthand syntax value is a local variable assigned a method call' do
expect_no_offenses(<<~RUBY)
has_many :accounts, class_name:
RUBY
end
end
end