Sha256: 494820e1582d3ff8c7abd1a3ee9de37158d47cdeb35c33559f57ac6723fb3886

Contents?: true

Size: 1.21 KB

Versions: 36

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module Thredded
  class UserTopicFollow < ActiveRecord::Base
    enum reason: %i[manual posted mentioned auto]

    belongs_to :user, inverse_of: :thredded_topic_follows, class_name: Thredded.user_class_name
    belongs_to :topic, inverse_of: :user_follows

    validates :user_id, presence: true
    validates :topic_id, presence: true

    # shim to behave like postable-related (though actually only ever related to topic)
    alias_attribute :postable_id, :topic_id
    alias_attribute :postable, :topic

    # Creates a follow or finds the existing one.
    #
    # This method is safe to call concurrently from different processes. Lookup and creation happen in a transaction.
    # If an ActiveRecord::RecordNotUnique error is raised, the find is retried.
    #
    # @return [Thredded::UserTopicFollow]
    def self.create_unless_exists(user_id, topic_id, reason = :manual)
      uncached do
        transaction(requires_new: true) do
          create_with(reason: reason).find_or_create_by(user_id: user_id, topic_id: topic_id)
        end
      end
    rescue ActiveRecord::RecordNotUnique
      # The record has been created from another connection, retry to find it.
      retry
    end
  end
end

Version data entries

36 entries across 36 versions & 2 rubygems

Version Path
thredded-1.1.0 app/models/thredded/user_topic_follow.rb
thredded-1.0.1 app/models/thredded/user_topic_follow.rb
thredded-1.0.0 app/models/thredded/user_topic_follow.rb
thredded-0.16.16 app/models/thredded/user_topic_follow.rb
thredded-0.16.15 app/models/thredded/user_topic_follow.rb
thredded-0.16.14 app/models/thredded/user_topic_follow.rb
thredded-0.16.13 app/models/thredded/user_topic_follow.rb
thredded-0.16.12 app/models/thredded/user_topic_follow.rb
thredded-0.16.11 app/models/thredded/user_topic_follow.rb
thredded-0.16.10 app/models/thredded/user_topic_follow.rb
thredded-0.16.9 app/models/thredded/user_topic_follow.rb
thredded-0.16.8 app/models/thredded/user_topic_follow.rb
thredded-0.16.7 app/models/thredded/user_topic_follow.rb
thredded-0.16.6 app/models/thredded/user_topic_follow.rb
thredded-0.16.5 app/models/thredded/user_topic_follow.rb
thredded-0.16.4 app/models/thredded/user_topic_follow.rb
thredded-0.16.3 app/models/thredded/user_topic_follow.rb
thredded-0.16.1 app/models/thredded/user_topic_follow.rb
thredded-0.16.0 app/models/thredded/user_topic_follow.rb
thredded-0.15.5 app/models/thredded/user_topic_follow.rb