forked from rubocop/rubocop-rails
/
active_record_callbacks_order_spec.rb
145 lines (123 loc) · 4.13 KB
/
active_record_callbacks_order_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Rails::ActiveRecordCallbacksOrder do
subject(:cop) { described_class.new }
it 'registers an offense and corrects when declared callbacks are not correctly ordered' do
expect_offense(<<~RUBY)
class User < ApplicationRecord
scope :admins, -> { where(admin: true) }
after_commit :after_commit_callback
after_save :after_save_callback
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `after_save` is supposed to appear before `after_commit`.
def some_method
end
before_validation :before_validation_callback
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `before_validation` is supposed to appear before `after_save`.
some_other_macros :foo
end
RUBY
expect_correction(<<~RUBY)
class User < ApplicationRecord
scope :admins, -> { where(admin: true) }
before_validation :before_validation_callback
after_save :after_save_callback
after_commit :after_commit_callback
def some_method
end
some_other_macros :foo
end
RUBY
end
it 'correcly autocorrects when there is a preceding comment for callback method' do
new_source = autocorrect_source(<<~RUBY)
class User < ApplicationRecord
# This is a
# multiline
# comment for after_commit.
after_commit :after_commit_callback
# This is another
# multiline
# comment for after_save.
after_save :after_save_callback
end
RUBY
expect(new_source).to eq(<<~RUBY)
class User < ApplicationRecord
# This is another
# multiline
# comment for after_save.
after_save :after_save_callback
# This is a
# multiline
# comment for after_commit.
after_commit :after_commit_callback
end
RUBY
end
it 'correcly autocorrects when there is an inline comment for callback method' do
new_source = autocorrect_source(<<~RUBY)
class User < ApplicationRecord
after_commit :after_commit_callback # after_commit inline comment
after_save :after_save_callback # after_save inline comment
end
RUBY
expect(new_source).to eq(<<~RUBY)
class User < ApplicationRecord
after_save :after_save_callback # after_save inline comment
after_commit :after_commit_callback # after_commit inline comment
end
RUBY
end
it 'correcly autocorrects when there are multiple callbacks of the same type' do
new_source = autocorrect_source(<<~RUBY)
class User < ApplicationRecord
after_commit :after_commit_callback1
after_save :after_save_callback
after_commit :after_commit_callback2
end
RUBY
expect(new_source).to eq(<<~RUBY)
class User < ApplicationRecord
after_save :after_save_callback
after_commit :after_commit_callback1
after_commit :after_commit_callback2
end
RUBY
end
it 'preserves the original order of callbacks of the same type' do
expect_offense(<<~RUBY)
class User < ApplicationRecord
after_commit :after_commit_callback
after_save :after_save_callback1
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `after_save` is supposed to appear before `after_commit`.
after_save :after_save_callback2
end
RUBY
expect_correction(<<~RUBY)
class User < ApplicationRecord
after_save :after_save_callback1
after_save :after_save_callback2
after_commit :after_commit_callback
end
RUBY
end
it 'does not register an offense when declared callbacks are correctly ordered' do
expect_no_offenses(<<~RUBY)
class User < ApplicationRecord
scope :admins, -> { where(admin: true) }
before_validation :before_validation_callback
after_save :after_save_callback
def some_method
end
after_commit :after_commit_callback
end
RUBY
end
it 'does not register an offense when there are no callbacks' do
expect_no_offenses(<<~RUBY)
class User < ApplicationRecord
def some_method
end
end
RUBY
end
end