summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorddavison <ddavison@gitlab.com>2018-11-13 12:52:29 -0800
committerddavison <ddavison@gitlab.com>2018-11-13 12:52:29 -0800
commit6d794dac2ab52daf75eb1b50247ac188cf1d276b (patch)
tree873d8d742aea635b8f30dfe84b759d01f58206f1
parent132e6c9f95a7a2d3aa7f78072b18c331128f8065 (diff)
downloadgitlab-ce-dd-rails5-applicationrecord-cop.tar.gz
Add Gitlab/ModelsInheritApplicationRecordCop.dd-rails5-applicationrecord-cop
New models after rails 5 upgrade should inherit from ApplicationRecord, not ActiveRecord::Base Signed-off-by: ddavison <ddavison@gitlab.com>
-rw-r--r--.rubocop.yml3
-rw-r--r--rubocop/cop/gitlab/models_inherit_application_record.rb41
-rw-r--r--rubocop/model_helpers.rb10
-rw-r--r--rubocop/rubocop.rb1
4 files changed, 55 insertions, 0 deletions
diff --git a/.rubocop.yml b/.rubocop.yml
index a95ded8af1c..ffa77799a91 100644
--- a/.rubocop.yml
+++ b/.rubocop.yml
@@ -163,6 +163,9 @@ Gitlab/ModuleWithInstanceVariables:
Gitlab/HTTParty:
Enabled: true
+Gitlab/ModelsInheritApplicationRecord:
+ Enabled: true
+
GitlabSecurity/PublicSend:
Enabled: true
Exclude:
diff --git a/rubocop/cop/gitlab/models_inherit_application_record.rb b/rubocop/cop/gitlab/models_inherit_application_record.rb
new file mode 100644
index 00000000000..1172930d1a2
--- /dev/null
+++ b/rubocop/cop/gitlab/models_inherit_application_record.rb
@@ -0,0 +1,41 @@
+require_relative '../../model_helpers'
+
+module RuboCop
+ module Cop
+ module Models
+ # This cop checks for models that extend from ActiveRecord::Base
+ #
+ # @example
+ # # bad
+ # class MyModel < ActiveRecord::Base
+ #
+ # # good
+ # class MyModel < ApplicationRecord
+ class ModelsInheritApplicationRecord < RuboCop::Cop::Cop
+ include ModelHelpers
+
+ MESSAGE = <<~EOL.freeze
+ Models should not inherit from ActiveRecord::Base anymore. Please inherit ApplicationRecord instead
+
+ Rails 5 upgrade item 5.2 dictates all Models inherit from ApplicationRecord
+
+ https://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#active-record-models-now-inherit-from-applicationrecord-by-default
+ EOL
+
+
+ def_node_matcher :extends_activerecord_base?, <<~PATTERN
+ (class
+ (const nil ...)
+ (const
+ (const nil :ActiveRecord) :Base) nil)
+ PATTERN
+
+ def on_send(node)
+ return unless model?(node)
+
+ add_offense(node, location: :expression, message: MESSAGE) if extends_activerecord_base?(node)
+ end
+ end
+ end
+ end
+end
diff --git a/rubocop/model_helpers.rb b/rubocop/model_helpers.rb
new file mode 100644
index 00000000000..177dc5cd923
--- /dev/null
+++ b/rubocop/model_helpers.rb
@@ -0,0 +1,10 @@
+module RuboCop
+ # Module containing helper methods for writing Model cops.
+ module ModelHelpers
+ def model?(node)
+ path = node.location.expression.source_buffer.name
+
+ path.start_with?(File.join(Dir.pwd, 'models'))
+ end
+ end
+end
diff --git a/rubocop/rubocop.rb b/rubocop/rubocop.rb
index 6c9b8753c1a..bad746234ff 100644
--- a/rubocop/rubocop.rb
+++ b/rubocop/rubocop.rb
@@ -2,6 +2,7 @@
require_relative 'cop/gitlab/module_with_instance_variables'
require_relative 'cop/gitlab/predicate_memoization'
require_relative 'cop/gitlab/httparty'
+require_relative 'cop/gitlab/models_inherit_application_record'
require_relative 'cop/gitlab/finder_with_find_by'
require_relative 'cop/gitlab/union'
require_relative 'cop/include_sidekiq_worker'