Class: Thredded::TopicsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/thredded/topics_controller.rb

Instance Method Summary collapse

Methods included from UrlsHelper

#delete_post_path, #edit_post_path, #edit_preferences_path, #edit_preferences_url, #post_path, #post_url, #search_path, #topic_path, #topic_url, #user_path

Instance Method Details

#categoryObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/thredded/topics_controller.rb', line 67

def category
  authorize_reading messageboard
  @category = messageboard.categories.friendly.find(params[:category_id])
  @topics = Thredded::TopicsPageView.new(
    thredded_current_user,
    policy_scope(@category.topics)
      .unstuck
      .order_recently_posted_first
      .page(current_page)
  )
  render :index
end

#createObject



80
81
82
83
84
85
86
87
88
# File 'app/controllers/thredded/topics_controller.rb', line 80

def create
  @new_topic = TopicForm.new(new_topic_params)
  authorize_creating @new_topic.topic
  if @new_topic.save
    redirect_to messageboard_topics_path(messageboard)
  else
    render :new
  end
end

#destroyObject



104
105
106
107
108
109
# File 'app/controllers/thredded/topics_controller.rb', line 104

def destroy
  authorize topic, :destroy?
  topic.destroy!
  redirect_to messageboard_topics_path(messageboard),
              notice: t('thredded.topics.deleted_notice')
end

#editObject



90
91
92
# File 'app/controllers/thredded/topics_controller.rb', line 90

def edit
  authorize topic, :update?
end

#followObject



111
112
113
114
115
# File 'app/controllers/thredded/topics_controller.rb', line 111

def follow
  authorize topic, :read?
  UserTopicFollow.create_unless_exists(thredded_current_user.id, topic.id)
  follow_change_response(following: true)
end

#indexObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/thredded/topics_controller.rb', line 14

def index
  authorize_reading messageboard

  @topics = Thredded::TopicsPageView.new(
    thredded_current_user,
    policy_scope(messageboard.topics)
      .order_sticky_first.order_recently_posted_first
      .includes(:categories, :last_user, :user)
      .page(current_page)
  )
  TopicForm.new(messageboard: messageboard, user: thredded_current_user).tap do |form|
    @new_topic = form if policy(form.topic).create?
  end
end

#newObject



62
63
64
65
# File 'app/controllers/thredded/topics_controller.rb', line 62

def new
  @new_topic = TopicForm.new(new_topic_params)
  authorize_creating @new_topic.topic
end

#searchObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/thredded/topics_controller.rb', line 42

def search
  authorize_reading messageboard if messageboard_or_nil
  @query = params[:q].to_s
  topics_scope = policy_scope(
    if messageboard_or_nil
      messageboard.topics
    else
      Topic.where(messageboard_id: policy_scope(Messageboard.all).pluck(:id))
    end
  )
  @topics = Thredded::TopicsPageView.new(
    thredded_current_user,
    topics_scope
      .search_query(@query)
      .order_recently_posted_first
      .includes(:categories, :last_user, :user)
      .page(current_page)
  )
end

#showObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/thredded/topics_controller.rb', line 29

def show
  authorize topic, :read?
  page_scope = policy_scope(topic.posts)
    .order_oldest_first
    .includes(:user, :messageboard, :postable)
    .page(current_page)
  @posts = Thredded::TopicPostsPageView.new(thredded_current_user, topic, page_scope)

  UserTopicReadState.touch!(thredded_current_user.id, topic.id, page_scope.last, current_page) if signed_in?

  @new_post = messageboard.posts.build(postable: topic)
end

#unfollowObject



117
118
119
120
121
# File 'app/controllers/thredded/topics_controller.rb', line 117

def unfollow
  authorize topic, :read?
  UserTopicFollow.find_by(topic_id: topic.id, user_id: thredded_current_user.id).try(:destroy)
  follow_change_response(following: false)
end

#updateObject



94
95
96
97
98
99
100
101
102
# File 'app/controllers/thredded/topics_controller.rb', line 94

def update
  authorize topic, :update?
  if topic.update(topic_params.merge(last_user_id: thredded_current_user.id))
    redirect_to messageboard_topic_url(messageboard, topic),
                notice: t('thredded.topics.updated_notice')
  else
    render :edit
  end
end