Class: Thredded::TopicsController

Inherits:
ApplicationController show all
Includes:
NewPostParams, NewTopicParams
Defined in:
app/controllers/thredded/topics_controller.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Method Summary collapse

Methods included from UrlsHelper

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

Instance Method Details

#categoryObject



88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/controllers/thredded/topics_controller.rb', line 88

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



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

def create
  @new_topic = Thredded::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



137
138
139
140
141
142
# File 'app/controllers/thredded/topics_controller.rb', line 137

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

#editObject



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

def edit
  authorize topic, :update?
  return redirect_to(canonical_topic_params) unless params_match?(canonical_topic_params)
  @edit_topic = Thredded::EditTopicForm.new(user: thredded_current_user, topic: topic)
end

#followObject



144
145
146
147
148
# File 'app/controllers/thredded/topics_controller.rb', line 144

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

#indexObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/thredded/topics_controller.rb', line 19

def index
  authorize_reading messageboard
  unless params_match?(canonical_messageboard_params)
    skip_policy_scope
    return redirect_to(canonical_messageboard_params)
  end

  page_scope = policy_scope(messageboard.topics)
    .order_sticky_first.order_recently_posted_first
    .page(current_page)
  return redirect_to(last_page_params(page_scope)) if page_beyond_last?(page_scope)
  @topics = Thredded::TopicsPageView.new(thredded_current_user, page_scope)
  Thredded::TopicForm.new(messageboard: messageboard, user: thredded_current_user).tap do |form|
    @new_topic = form if policy(form.topic).create?
  end
end

#newObject



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

def new
  @new_topic = Thredded::TopicForm.new(new_topic_params)
  authorize_creating @new_topic.topic
  return redirect_to(canonical_messageboard_params) unless params_match?(canonical_messageboard_params)
  render
end

#searchObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/thredded/topics_controller.rb', line 55

def search
  in_messageboard = params.key?(:messageboard_id)
  if in_messageboard
    authorize_reading messageboard
    unless params_match?(canonical_messageboard_params)
      skip_policy_scope
      return redirect_to(canonical_messageboard_params)
    end
  end
  @query = params[:q].to_s
  topics_scope = policy_scope(
    if in_messageboard
      messageboard.topics
    else
      Thredded::Topic.where(messageboard_id: policy_scope(Thredded::Messageboard.all).pluck(:id))
    end
  )
  page_scope = topics_scope
    .search_query(@query)
    .order_recently_posted_first
    .includes(:categories, :last_user, :user)
    .page(current_page)
  return redirect_to(last_page_params(page_scope)) if page_beyond_last?(page_scope)
  @topics = Thredded::TopicsPageView.new(thredded_current_user, page_scope)
end

#showObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/thredded/topics_controller.rb', line 36

def show
  authorize topic, :read?
  return redirect_to(canonical_topic_params) unless params_match?(canonical_topic_params)
  page_scope = policy_scope(topic.posts)
    .order_oldest_first
    .includes(:user, :messageboard, :postable)
    .page(current_page)
  return redirect_to(last_page_params(page_scope)) if page_beyond_last?(page_scope)
  @posts = Thredded::TopicPostsPageView.new(thredded_current_user, topic, page_scope)

  if thredded_signed_in?
    Thredded::UserTopicReadState.touch!(
      thredded_current_user.id, topic.id, page_scope.last, current_page
    )
  end

  @new_post = Thredded::PostForm.new(user: thredded_current_user, topic: topic, post_params: new_post_params)
end

#unfollowObject



150
151
152
153
154
# File 'app/controllers/thredded/topics_controller.rb', line 150

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

#updateObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/controllers/thredded/topics_controller.rb', line 117

def update
  topic.assign_attributes(topic_params_for_update)
  authorize topic, :update?
  if topic.messageboard_id_changed?
    # Work around the association not being reset.
    # TODO: report issue to Rails. Looks like a regression of:
    # https://rails.lighthouseapp.com/projects/8994/tickets/2989
    topic.messageboard = Thredded::Messageboard.find(topic.messageboard_id)

    authorize topic.messageboard, :post?
  end
  @edit_topic = Thredded::EditTopicForm.new(user: thredded_current_user, topic: topic)
  if @edit_topic.save
    redirect_to messageboard_topic_url(topic.messageboard, topic),
                notice: t('thredded.topics.updated_notice')
  else
    render :edit
  end
end