Sha256: 20045b77365dc3edd0871fd18967beba57fb6e2c24b1cc801cb40334549dfc56

Contents?: true

Size: 1.17 KB

Versions: 39

Compression:

Stored size: 1.17 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?,
             :[],
             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

39 entries across 39 versions & 2 rubygems

Version Path
thredded-0.15.1 app/view_models/thredded/topics_page_view.rb
threddedDANIEL-0.14.5 app/view_models/thredded/topics_page_view.rb
thredded-0.14.4 app/view_models/thredded/topics_page_view.rb
thredded-0.14.3 app/view_models/thredded/topics_page_view.rb
thredded-0.14.2 app/view_models/thredded/topics_page_view.rb
thredded-0.14.1 app/view_models/thredded/topics_page_view.rb
thredded-0.14.0 app/view_models/thredded/topics_page_view.rb
thredded-0.13.8 app/view_models/thredded/topics_page_view.rb
thredded-0.13.7 app/view_models/thredded/topics_page_view.rb
thredded-0.13.6 app/view_models/thredded/topics_page_view.rb
thredded-0.13.5 app/view_models/thredded/topics_page_view.rb
thredded-0.13.4 app/view_models/thredded/topics_page_view.rb
thredded-0.13.3 app/view_models/thredded/topics_page_view.rb
thredded-0.13.2 app/view_models/thredded/topics_page_view.rb
thredded-0.13.1 app/view_models/thredded/topics_page_view.rb
thredded-0.13.0 app/view_models/thredded/topics_page_view.rb
thredded-0.12.4 app/view_models/thredded/topics_page_view.rb
thredded-0.12.3 app/view_models/thredded/topics_page_view.rb
thredded-0.12.2 app/view_models/thredded/topics_page_view.rb