Sha256: b126a67e5e136a3de94795923668c1b41467a01bdc6bf8e2da0eac3bc91b60b1

Contents?: true

Size: 1.85 KB

Versions: 3

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true
module Thredded
  # A view model for Topic.
  class TopicView < Thredded::BaseTopicView
    delegate :categories, :id, :blocked?, :last_moderation_record, :followers,
             :last_post, :messageboard_id, :messageboard_name,
             to: :@topic

    # @param topic [TopicCommon]
    # @param read_state [UserTopicReadStateCommon, nil]
    # @param policy [#destroy?]
    def initialize(topic, read_state, follow, policy)
      super(topic, read_state, policy)
      @follow = follow
    end

    def self.from_user(topic, user)
      read_state = follow = nil
      if user && !user.thredded_anonymous?
        read_state = Thredded::UserTopicReadState.find_by(user_id: user.id, postable_id: topic.id)
        follow = Thredded::UserTopicFollow.find_by(user_id: user.id, topic_id: topic.id)
      end
      new(topic, read_state, follow, Pundit.policy!(user, topic))
    end

    def states
      super + [
        (:locked if @topic.locked?),
        (:sticky if @topic.sticky?),
        (@follow ? :following : :notfollowing)
      ].compact
    end

    # @return [Boolean] whether the topic is followed by the current user.
    def followed?
      @follow
    end

    def follow_reason
      @follow.try(:reason)
    end

    def can_moderate?
      @policy.moderate?
    end

    def edit_path
      Thredded::UrlsHelper.edit_messageboard_topic_path(@topic.messageboard, @topic)
    end

    def destroy_path
      Thredded::UrlsHelper.messageboard_topic_path(@topic.messageboard, @topic)
    end

    def follow_path
      Thredded::UrlsHelper.follow_messageboard_topic_path(@topic.messageboard, @topic)
    end

    def unfollow_path
      Thredded::UrlsHelper.unfollow_messageboard_topic_path(@topic.messageboard, @topic)
    end

    def messageboard_path
      Thredded::UrlsHelper.messageboard_topics_path(@topic.messageboard)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
thredded-0.9.3 app/view_models/thredded/topic_view.rb
thredded-0.9.2 app/view_models/thredded/topic_view.rb
thredded-0.9.1 app/view_models/thredded/topic_view.rb