Sha256: 63f51d45a7d3116873fed6ef44a37e1774a780f73b991764aea1b832c41c7efd

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

module Thredded
  class PrivatePost < ActiveRecord::Base
    include Thredded::PostCommon

    belongs_to :user,
               class_name: Thredded.user_class_name,
               inverse_of: :thredded_private_posts
    belongs_to :postable,
               class_name:    'Thredded::PrivateTopic',
               inverse_of:    :posts,
               counter_cache: :posts_count
    belongs_to :user_detail,
               inverse_of:  :private_posts,
               primary_key: :user_id,
               foreign_key: :user_id

    after_commit :update_parent_last_user_and_timestamp, on: %i[create destroy]
    after_commit :notify_users, on: [:create]

    # @param [Integer] per_page
    def page(per_page: self.class.default_per_page)
      calculate_page(postable.posts, per_page)
    end

    def private_topic_post?
      true
    end

    # @return [ActiveRecord::Relation<Thredded.user_class>] users that can read this post.
    def readers
      collection_proxy = postable.users
      if persisted?
        collection_proxy.scope
      else
        Thredded.user_class.where(id: collection_proxy.to_a.map(&:id))
      end
    end

    private

    def notify_users
      Thredded::NotifyPrivateTopicUsersJob.perform_later(id)
    end

    def update_parent_last_user_and_timestamp
      return if postable.destroyed?
      last_post = if destroyed?
                    postable.posts.order_oldest_first.select(:user_id, :created_at).last
                  else
                    self
                  end
      postable.update_columns(
        last_user_id: last_post.user_id,
        last_post_at: last_post.created_at,
        updated_at: Time.zone.now
      )
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
thredded-0.12.2 app/models/thredded/private_post.rb