diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2017-06-08 17:16:27 +0200 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2017-07-06 12:01:36 +0200 |
commit | 8fbbf41e29f5e0f56b7eb9d37aadba856b68bcce (patch) | |
tree | 55308daf8218231e33f7c50ae81cd5774c058576 /rubocop | |
parent | c63e3221587daf9e7464f7d2079ca8ed3111f6ff (diff) | |
download | gitlab-ce-8fbbf41e29f5e0f56b7eb9d37aadba856b68bcce.tar.gz |
Added Cop to blacklist the use of `dependent:`
This is allowed for existing instances so we don't end up 76 offenses
right away, but for new code one should _only_ use this if they _have_
to remove non database data. Even then it's usually better to do this in
a service class as this gives you more control over how to remove the
data (e.g. in bulk).
Diffstat (limited to 'rubocop')
-rw-r--r-- | rubocop/cop/active_record_dependent.rb | 26 | ||||
-rw-r--r-- | rubocop/rubocop.rb | 1 |
2 files changed, 27 insertions, 0 deletions
diff --git a/rubocop/cop/active_record_dependent.rb b/rubocop/cop/active_record_dependent.rb new file mode 100644 index 00000000000..8d15f150885 --- /dev/null +++ b/rubocop/cop/active_record_dependent.rb @@ -0,0 +1,26 @@ +require_relative '../model_helpers' + +module RuboCop + module Cop + # Cop that prevents the use of `dependent: ...` in ActiveRecord models. + class ActiveRecordDependent < RuboCop::Cop::Cop + include ModelHelpers + + MSG = 'Do not use `dependent: to remove associated data, ' \ + 'use foreign keys with cascading deletes instead'.freeze + + METHOD_NAMES = [:has_many, :has_one, :belongs_to].freeze + + def on_send(node) + return unless in_model?(node) + return unless METHOD_NAMES.include?(node.children[1]) + + node.children.last.each_node(:pair) do |pair| + key_name = pair.children[0].children[0] + + add_offense(pair, :expression) if key_name == :dependent + end + end + end + end +end diff --git a/rubocop/rubocop.rb b/rubocop/rubocop.rb index 69b4b29507c..1a9bdd9325e 100644 --- a/rubocop/rubocop.rb +++ b/rubocop/rubocop.rb @@ -4,6 +4,7 @@ require_relative 'cop/activerecord_serialize' require_relative 'cop/redirect_with_status' require_relative 'cop/polymorphic_associations' require_relative 'cop/project_path_helper' +require_relative 'cop/active_record_dependent' require_relative 'cop/migration/add_column' require_relative 'cop/migration/add_column_with_default_to_large_table' require_relative 'cop/migration/add_concurrent_foreign_key' |