Sha256: 5a5ace2c61276a16bcdb6fb268bc445dc610ae593ca1747c06dc9417b9bbfb8e

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module Thredded
  # A view model for a page of BaseTopicViews.
  class TopicsPageView
    delegate :to_a,
             :to_ary,
             :blank?,
             :empty?,
             :[],
             :each,
             :map,
             :size,
             to: :@topic_views
    delegate :total_pages,
             :current_page,
             :limit_value,
             to: :@topics_paginator

    # @param user [Thredded.user_class] the user who is viewing the topics page
    # @param topics_page_scope [ActiveRecord::Relation<Thredded::Topic>] a kaminari-decorated ".page" scope
    # @param topic_view_class [Class<TopicView>] view_model for topic instances
    def initialize(user, topics_page_scope, topic_view_class: TopicView)
      @topics_paginator = refine_scope(topics_page_scope)
      @topic_views = @topics_paginator.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-1.0.1 app/view_models/thredded/topics_page_view.rb
thredded-1.0.0 app/view_models/thredded/topics_page_view.rb