Sha256: c1c704c86cdc99bfe8fb77ded21d05f6de4cb1e6b31b6790a5f810b6ca074e6c

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true
module Thredded
  # A view model for a page of BaseTopicViews.
  class TopicsPageView
    delegate :to_ary,
             :blank?,
             :empty?,
             to: :@topic_views
    delegate :total_pages,
             :current_page,
             :limit_value,
             to: :@topics_page_scope

    # @param user [Thredded.user_class] the user who is viewing the posts page
    # @param topics_page_scope [ActiveRecord::Relation<Thredded::Topic>]
    # @param topic_view_class [Class] view_model for topic instances
    def initialize(user, topics_page_scope, topic_view_class: TopicView)
      @topics_page_scope = refine_scope(topics_page_scope)
      @topic_views = @topics_page_scope.with_read_and_follow_states(user).map do |(topic, read_state, follow)|
        topic_view_class.new(topic, read_state, follow, Pundit.policy!(user, topic))
      end
    end

    protected

    def refine_scope(topics_page_scope)
      scope = topics_page_scope.includes(:categories, :last_user, :user)
      if Thredded.show_topic_followers
        scope.includes(:followers)
      else
        scope
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
thredded-0.9.2 app/view_models/thredded/topics_page_view.rb
thredded-0.9.1 app/view_models/thredded/topics_page_view.rb