Class: Course::Forum::PostsController

Inherits:
ComponentController show all
Includes:
Discussion::PostsConcern
Defined in:
app/controllers/course/forum/posts_controller.rb

Instance Method Summary collapse

Methods included from Discussion::PostsConcern

#update_topic_pending_status

Methods included from ApplicationSignInCallbacksConcern

#sign_in

Methods included from ApplicationPaginationConcern

#page_param

Methods included from ApplicationAnnouncementsConcern

#global_announcements, #unread_global_announcements

Methods included from ApplicationInstanceUserConcern

#current_instance_user

Methods included from ApplicationUserTimeZoneConcern

#set_time_zone

Methods included from ApplicationUserConcern

#handle_access_denied, #publicly_accessible?

Methods included from ApplicationComponentsConcern

#handle_component_not_found

Instance Method Details

#createObject


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/course/forum/posts_controller.rb', line 11

def create
  result = @post.class.transaction do
    raise ActiveRecord::Rollback unless @post.save && create_topic_subscription && update_topic_pending_status
    raise ActiveRecord::Rollback unless @topic.update_column(:latest_post_at, @post.created_at)
    true
  end

  if result
    send_created_notification(@post)
    redirect_to course_forum_topic_path(current_course, @forum, @topic),
                success: t('course.discussion.posts.create.success')
  else
    redirect_to course_forum_topic_path(current_course, @forum, @topic),
                danger: t('course.discussion.posts.create.failure',
                          error: @post.errors.full_messages.to_sentence)
  end
end

#create_topic_subscriptionObject (protected)


77
78
79
# File 'app/controllers/course/forum/posts_controller.rb', line 77

def create_topic_subscription
  @topic.ensure_subscribed_by(current_user)
end

#destroyObject


55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/course/forum/posts_controller.rb', line 55

def destroy
  if @topic.posts.count == 1 && @topic.destroy
    redirect_to course_forum_path(current_course, @forum),
                success: t('course.forum.topics.destroy.success', title: @topic.title)
  elsif @post.destroy
    @topic.update_column(:latest_post_at, @topic.posts.last&.created_at || @topic.created_at)
    redirect_to course_forum_topic_path(current_course, @forum, @topic),
                success: t('course.discussion.posts.destroy.success')
  else
    redirect_to course_forum_topic_path(current_course, @forum, @topic),
                danger: t('course.discussion.posts.destroy.failure',
                          error: @post.errors.full_messages.to_sentence)
  end
end

#discussion_topicObject (protected)


81
82
83
# File 'app/controllers/course/forum/posts_controller.rb', line 81

def discussion_topic
  @topic.acting_as
end

#replyObject

Render a new post in a separate page


71
72
73
# File 'app/controllers/course/forum/posts_controller.rb', line 71

def reply
  @reply_post = @post.children.build
end

#skip_update_topic_statusObject (protected)


85
86
87
# File 'app/controllers/course/forum/posts_controller.rb', line 85

def skip_update_topic_status
  true
end

#toggle_answerObject

Mark/unmark the post as the correct answer


45
46
47
48
49
50
51
52
53
# File 'app/controllers/course/forum/posts_controller.rb', line 45

def toggle_answer
  authorize!(:toggle_answer, @topic)
  if @post.toggle_answer
    redirect_to course_forum_topic_path(current_course, @forum, @topic), success: t('.success')
  else
    redirect_to course_forum_topic_path(current_course, @forum, @topic),
                danger: @post.errors.full_messages.to_sentence
  end
end

#updateObject


29
30
31
32
33
34
35
36
# File 'app/controllers/course/forum/posts_controller.rb', line 29

def update
  if @post.update_attributes(post_params)
    redirect_to course_forum_topic_path(current_course, @forum, @topic),
                success: t('course.discussion.posts.update.success')
  else
    render 'edit'
  end
end

#voteObject


38
39
40
41
42
# File 'app/controllers/course/forum/posts_controller.rb', line 38

def vote
  @post.cast_vote!(current_user, post_vote_param)
  redirect_to course_forum_topic_path(current_course, @forum, @topic),
              success: t('.success')
end