Class: Thredded::TopicView

Inherits:
BaseTopicView show all
Defined in:
app/view_models/thredded/topic_view.rb

Overview

A view model for Topic.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseTopicView

#can_destroy?, #can_update?, #path

Constructor Details

#initialize(topic, read_state, follow, policy) ⇒ TopicView

Returns a new instance of TopicView.

Parameters:



13
14
15
16
# File 'app/view_models/thredded/topic_view.rb', line 13

def initialize(topic, read_state, follow, policy)
  super(topic, read_state, policy)
  @follow = follow
end

Class Method Details

.from_user(topic, user) ⇒ Object



18
19
20
21
22
23
24
25
# File 'app/view_models/thredded/topic_view.rb', line 18

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

Instance Method Details

#can_moderate?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'app/view_models/thredded/topic_view.rb', line 44

def can_moderate?
  @policy.moderate?
end

#destroy_pathObject



52
53
54
# File 'app/view_models/thredded/topic_view.rb', line 52

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

#edit_pathObject



48
49
50
# File 'app/view_models/thredded/topic_view.rb', line 48

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

#follow_pathObject



56
57
58
# File 'app/view_models/thredded/topic_view.rb', line 56

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

#follow_reasonObject



40
41
42
# File 'app/view_models/thredded/topic_view.rb', line 40

def follow_reason
  @follow.try(:reason)
end

#followed?Boolean

Returns whether the topic is followed by the current user.

Returns:

  • (Boolean)

    whether the topic is followed by the current user.



36
37
38
# File 'app/view_models/thredded/topic_view.rb', line 36

def followed?
  @follow
end

#messageboard_pathObject



64
65
66
# File 'app/view_models/thredded/topic_view.rb', line 64

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

#statesObject



27
28
29
30
31
32
33
# File 'app/view_models/thredded/topic_view.rb', line 27

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

#unfollow_pathObject



60
61
62
# File 'app/view_models/thredded/topic_view.rb', line 60

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