/
active_record_override_spec.rb
101 lines (89 loc) · 2.58 KB
/
active_record_override_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Rails::ActiveRecordOverride do
subject(:cop) { described_class.new(config) }
let(:config) { RuboCop::Config.new }
it 'registers an offense when overriding create' do
expect_offense(<<-RUBY.strip_indent)
class X < ApplicationRecord
def create
^^^^^^^^^^ Use `before_create`, `around_create`, or `after_create` callbacks instead of overriding the Active Record method `create`.
super
end
end
RUBY
end
it 'registers an offense when overriding destroy' do
expect_offense(<<-RUBY.strip_indent)
class X < ApplicationRecord
def destroy
^^^^^^^^^^^ Use `before_destroy`, `around_destroy`, or `after_destroy` callbacks instead of overriding the Active Record method `destroy`.
super
end
end
RUBY
end
it 'registers an offense when overriding save' do
expect_offense(<<-RUBY.strip_indent)
class X < ApplicationRecord
def save
^^^^^^^^ Use `before_save`, `around_save`, or `after_save` callbacks instead of overriding the Active Record method `save`.
super
end
end
RUBY
end
it 'registers an offense when overriding update' do
expect_offense(<<-RUBY.strip_indent)
class X < ActiveModel::Base
module_function
def update
^^^^^^^^^^ Use `before_update`, `around_update`, or `after_update` callbacks instead of overriding the Active Record method `update`.
super
end
end
RUBY
end
context 'when overriding without a super call' do
it 'registers no offense when overriding save' do
expect_no_offenses(<<-RUBY.strip_indent)
class X < ApplicationRecord
def save
@a = 5
end
end
RUBY
end
end
context 'when class is not an ActiveRecord model' do
it 'registers no offense when overriding save' do
expect_no_offenses(<<-RUBY.strip_indent)
class X
def save
super
end
end
RUBY
end
end
context 'when class is not an ActiveRecord model' do
it 'registers no offense when overriding save' do
expect_no_offenses(<<-RUBY.strip_indent)
class X < Y
def save
super
end
end
RUBY
end
end
context 'when class has no parent specified' do
it 'registers no offense when overriding save' do
expect_no_offenses(<<-RUBY.strip_indent)
class X
def initialize; end
def save; end
end
RUBY
end
end
end