Sha256: ff8a150940cb8ebaef49d897277a26619f4563376b6a64168983f7147b50c67c

Contents?: true

Size: 1.87 KB

Versions: 24

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

module Thredded
  # A view model for Topic.
  class TopicView < Thredded::BaseTopicView
    delegate :sticky?, :locked?, :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

24 entries across 24 versions & 2 rubygems

Version Path
thredded-0.16.0 app/view_models/thredded/topic_view.rb
thredded-0.15.5 app/view_models/thredded/topic_view.rb
thredded-0.15.4 app/view_models/thredded/topic_view.rb
thredded-0.15.3 app/view_models/thredded/topic_view.rb
thredded-0.15.2 app/view_models/thredded/topic_view.rb
thredded-0.15.1 app/view_models/thredded/topic_view.rb
threddedDANIEL-0.14.5 app/view_models/thredded/topic_view.rb
thredded-0.14.4 app/view_models/thredded/topic_view.rb
thredded-0.14.3 app/view_models/thredded/topic_view.rb
thredded-0.14.2 app/view_models/thredded/topic_view.rb
thredded-0.14.1 app/view_models/thredded/topic_view.rb
thredded-0.14.0 app/view_models/thredded/topic_view.rb
thredded-0.13.8 app/view_models/thredded/topic_view.rb
thredded-0.13.7 app/view_models/thredded/topic_view.rb
thredded-0.13.6 app/view_models/thredded/topic_view.rb
thredded-0.13.5 app/view_models/thredded/topic_view.rb
thredded-0.13.4 app/view_models/thredded/topic_view.rb
thredded-0.13.3 app/view_models/thredded/topic_view.rb
thredded-0.13.2 app/view_models/thredded/topic_view.rb
thredded-0.13.1 app/view_models/thredded/topic_view.rb