From b6e072303129851e606b8dd616ab4d044c0ea4bc Mon Sep 17 00:00:00 2001 From: rhymes Date: Fri, 7 Aug 2020 20:27:24 +0200 Subject: [PATCH] Fix Rails/HasManyOrHasOneDependent for Broadcast and Collection (#9662) --- .rubocop_todo.yml | 2 -- app/models/broadcast.rb | 2 +- app/models/collection.rb | 3 ++- spec/models/broadcast_spec.rb | 2 +- spec/models/collection_spec.rb | 2 +- 5 files changed, 5 insertions(+), 6 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 8bd056ae7..a632939f7 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -24,8 +24,6 @@ Performance/OpenStruct: Rails/HasManyOrHasOneDependent: Exclude: - 'app/models/article.rb' - - 'app/models/broadcast.rb' - - 'app/models/collection.rb' - 'app/models/concerns/user_subscription_sourceable.rb' - 'app/models/display_ad.rb' - 'app/models/html_variant.rb' diff --git a/app/models/broadcast.rb b/app/models/broadcast.rb index 33a1ce838..6f5485e33 100644 --- a/app/models/broadcast.rb +++ b/app/models/broadcast.rb @@ -2,7 +2,7 @@ class Broadcast < ApplicationRecord VALID_BANNER_STYLES = %w[default brand success warning error].freeze resourcify - has_many :notifications, as: :notifiable, inverse_of: :notifiable + has_many :notifications, as: :notifiable, inverse_of: :notifiable, dependent: :destroy validates :title, uniqueness: { scope: :type_of }, presence: true validates :type_of, :processed_html, presence: true diff --git a/app/models/collection.rb b/app/models/collection.rb index fd6f52431..ee86df8dc 100644 --- a/app/models/collection.rb +++ b/app/models/collection.rb @@ -1,5 +1,6 @@ class Collection < ApplicationRecord - has_many :articles + has_many :articles, dependent: :nullify + belongs_to :user belongs_to :organization, optional: true diff --git a/spec/models/broadcast_spec.rb b/spec/models/broadcast_spec.rb index 25e39a047..6cb37ac2d 100644 --- a/spec/models/broadcast_spec.rb +++ b/spec/models/broadcast_spec.rb @@ -8,7 +8,7 @@ RSpec.describe Broadcast, type: :model do it { is_expected.to validate_inclusion_of(:banner_style).in_array(%w[default brand success warning error]) } it { is_expected.to validate_uniqueness_of(:title).scoped_to(:type_of) } - it { is_expected.to have_many(:notifications) } + it { is_expected.to have_many(:notifications).dependent(:destroy) } it "validates that only one Broadcast with a type_of Announcement can be active" do create(:announcement_broadcast) diff --git a/spec/models/collection_spec.rb b/spec/models/collection_spec.rb index e673b731c..449cf8b38 100644 --- a/spec/models/collection_spec.rb +++ b/spec/models/collection_spec.rb @@ -7,7 +7,7 @@ RSpec.describe Collection, type: :model do describe "validations" do it { is_expected.to belong_to(:user) } it { is_expected.to belong_to(:organization).optional } - it { is_expected.to have_many(:articles) } + it { is_expected.to have_many(:articles).dependent(:nullify) } it { is_expected.to validate_presence_of(:user_id) } it { is_expected.to validate_presence_of(:slug) }