Class: Thredded::MessageboardGroupView

Inherits:
Object
  • Object
show all
Defined in:
app/view_models/thredded/messageboard_group_view.rb

Overview

A view model for a page of MessageboardGroupViews.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group, messageboards) ⇒ MessageboardGroupView

Returns a new instance of MessageboardGroupView.

Parameters:



46
47
48
49
# File 'app/view_models/thredded/messageboard_group_view.rb', line 46

def initialize(group, messageboards)
  @group = group
  @messageboards = messageboards
end

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group.



7
8
9
# File 'app/view_models/thredded/messageboard_group_view.rb', line 7

def group
  @group
end

#messageboardsObject (readonly)

Returns the value of attribute messageboards.



7
8
9
# File 'app/view_models/thredded/messageboard_group_view.rb', line 7

def messageboards
  @messageboards
end

Class Method Details

.grouped(messageboards_scope, user: Thredded::NullUser.new, with_unread_topics_counts: !user.thredded_anonymous?) ⇒ Array<MessageboardGroupView>

Parameters:

  • messageboards_scope (ActiveRecord::Relation<Thredded::Messageboard>)
  • user (Thredded.user_class) (defaults to: Thredded::NullUser.new)

    The user viewing the messageboards.

  • with_unread_topics_counts (Boolean) (defaults to: !user.thredded_anonymous?)

Returns:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/view_models/thredded/messageboard_group_view.rb', line 13

def self.grouped( # rubocop:disable Metrics/MethodLength,Metrics/CyclomaticComplexity
  messageboards_scope, user: Thredded::NullUser.new, with_unread_topics_counts: !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
  topics_scope = Thredded::TopicPolicy::Scope.new(user, Thredded::Topic.all).resolve
  if with_unread_topics_counts
    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
  topic_counts = topics_scope.group(:messageboard_id).count
  posts_scope = Thredded::PostPolicy::Scope.new(user, Thredded::Post.all).resolve
  post_counts = posts_scope.group(:messageboard_id).count
  scope.group_by(&:group).map do |(group, messageboards)|
    MessageboardGroupView.new(group, messageboards.map do |messageboard|
      MessageboardView.new(
        messageboard,
        topics_count: topic_counts[messageboard.id] || 0,
        posts_count: post_counts[messageboard.id] || 0,
        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