# frozen_string_literal: true

module Thredded
  class NotificationsForFollowedTopics < ActiveRecord::Base
    belongs_to :user,
               class_name: Thredded.user_class_name,
               inverse_of: :thredded_notifications_for_followed_topics
    belongs_to :messageboard # or is global
    belongs_to :user_preference,
               primary_key: :user_id,
               foreign_key: :user_id,
               inverse_of: :notifications_for_followed_topics

    validates :user_id, presence: true

    include Thredded::NotifierPreference

    def self.default(_notifier)
      # could be moved to  `notifier.defaults(:notifications_for_followed_topics)`
      Thredded::BaseNotifier::NotificationsDefault.new(true)
    end
  end
end

# frozen_string_literal: true

module Thredded
  class NotificationsForFollowedTopics < ActiveRecord::Base
    belongs_to :user,
               class_name: Thredded.user_class_name,
               inverse_of: :thredded_notifications_for_followed_topics
    belongs_to :messageboard,
               # If messageboard is `nil`, these are the global preferences.
               **(Thredded.rails_gte_51? ? { optional: true } : {})
    belongs_to :user_preference,
               primary_key: :user_id,
               foreign_key: :user_id,
               inverse_of: :notifications_for_followed_topics

    validates :user_id, presence: true

    include Thredded::NotifierPreference

    def self.default(_notifier)
      # could be moved to  `notifier.defaults(:notifications_for_followed_topics)`
      Thredded::BaseNotifier::NotificationsDefault.new(true)
    end
  end
end

Add missing `optional: true` in NotificationsForFollowedTopics
Add missing `optional: true` in NotificationsForFollowedTopics

Discovered in this fork:
https://github.com/aniki21/thredded/commit/4c143c3e1065161a0af6ad83bf1a8215c6230b63
