Sha256: 176cafbd6f3ea52e61f13eaad8cc12bf5de8d05655d042c2797502f3c890dde2

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true
module Thredded
  # A view model for Topic.
  class TopicView < BaseTopicView
    delegate :categories, :id, :blocked?, :last_moderation_record,
             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 = UserTopicReadState.find_by(user_id: user.id, postable_id: topic.id)
        follow = 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

    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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
thredded-0.6.2 app/view_models/thredded/topic_view.rb
thredded-0.6.1 app/view_models/thredded/topic_view.rb
thredded-0.6.0 app/view_models/thredded/topic_view.rb