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:



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

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

Class Method Details

.from_user(topic, user) ⇒ Object



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

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

Instance Method Details

#can_moderate?Boolean

Returns:

  • (Boolean)


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

def can_moderate?
  @policy.moderate?
end

#destroy_pathObject



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

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

#edit_pathObject



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

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

#follow_pathObject



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

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

#follow_reasonObject



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

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.



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

def followed?
  @follow
end

#messageboard_pathObject



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

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

#statesObject



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

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

#unfollow_pathObject



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

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