Sha256: 3a4aed5f3b6923987b5a4931e4e0a75964292718a39a4f6e1197e14ffbb1558e

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

module Thredded
  # A view model for a page of MessageboardGroupViews.
  class MessageboardGroupView
    delegate :name, to: :@group, allow_nil: true
    attr_reader :group, :messageboards

    # @param [ActiveRecord::Relation<Thredded::Messageboard>] messageboards_scope
    # @param [Thredded.user_class] user The user viewing the messageboards.
    # @param [Boolean] with_unread_topics_counts
    # @return [Array<MessageboardGroupView>]
    def self.grouped(messageboards_scope, user: nil, with_unread_topics_counts: user && !user.thredded_anonymous?)
      scope = messageboards_scope.preload(last_topic: [:last_user])
        .eager_load(:group)
        .order(Arel.sql('COALESCE(thredded_messageboard_groups.position, 0) ASC, thredded_messageboard_groups.id ASC'))
        .ordered
      if with_unread_topics_counts
        topics_scope = Pundit.policy_scope!(user, Thredded::Topic)
        unread_topics_counts = messageboards_scope.unread_topics_counts(user: user, topics_scope: topics_scope)
        unread_followed_topics_counts = messageboards_scope.unread_topics_counts(
          user: user, topics_scope: topics_scope.followed_by(user)
        )
      end
      scope.group_by(&:group).map do |(group, messageboards)|
        MessageboardGroupView.new(group, messageboards.map do |messageboard|
          MessageboardView.new(
            messageboard,
            unread_topics_count: with_unread_topics_counts && unread_topics_counts[messageboard.id] || 0,
            unread_followed_topics_count:
              with_unread_topics_counts && unread_followed_topics_counts[messageboard.id] || 0
          )
        end)
      end
    end

    # @param [Thredded::MessageboardGroup] group
    # @param [Array<Thredded::MessageboardView>] messageboards
    def initialize(group, messageboards)
      @group = group
      @messageboards = messageboards
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
thredded-0.16.0 app/view_models/thredded/messageboard_group_view.rb