app/controllers/thredded/topics_controller.rb in thredded-0.6.3 vs app/controllers/thredded/topics_controller.rb in thredded-0.7.0

- old
+ new

@@ -1,8 +1,9 @@ # frozen_string_literal: true require_dependency 'thredded/posts_page_view' require_dependency 'thredded/topics_page_view' +# rubocop:disable Metrics/ClassLength module Thredded class TopicsController < Thredded::ApplicationController before_action :thredded_require_login!, only: %i(edit new update create destroy follow unfollow) after_action :update_user_activity @@ -14,11 +15,11 @@ authorize_reading messageboard @topics = Thredded::TopicsPageView.new( thredded_current_user, policy_scope(messageboard.topics) - .order_sticky_first.order_recently_updated_first + .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? @@ -50,11 +51,11 @@ ) @topics = Thredded::TopicsPageView.new( thredded_current_user, topics_scope .search_query(@query) - .order_recently_updated_first + .order_recently_posted_first .includes(:categories, :last_user, :user) .page(current_page) ) end @@ -68,11 +69,11 @@ @category = messageboard.categories.friendly.find(params[:category_id]) @topics = Thredded::TopicsPageView.new( thredded_current_user, policy_scope(@category.topics) .unstuck - .order_recently_updated_first + .order_recently_posted_first .page(current_page) ) render :index end @@ -108,24 +109,29 @@ end def follow authorize topic, :read? UserTopicFollow.create_unless_exists(thredded_current_user.id, topic.id) - redirect_to messageboard_topic_url(messageboard, topic), - notice: t('thredded.topics.followed_notice') + follow_change_response(following: true) end def unfollow authorize topic, :read? - follow = thredded_current_user.following?(topic) - follow.destroy if follow - redirect_to messageboard_topic_url(messageboard, topic), - notice: t('thredded.topics.unfollowed_notice') + UserTopicFollow.find_by(topic_id: topic.id, user_id: thredded_current_user.id).try(:destroy) + follow_change_response(following: false) end private + def follow_change_response(following:) + notice = following ? t('thredded.topics.followed_notice') : t('thredded.topics.unfollowed_notice') + respond_to do |format| + format.html { redirect_to messageboard_topic_url(messageboard, topic), notice: notice } + format.json { render(json: { follow: following }) } + end + end + def topic @topic ||= messageboard.topics.find_by_slug!(params[:id]) end def topic_params @@ -148,5 +154,6 @@ def current_page (params[:page] || 1).to_i end end end +# rubocop:enable Metrics/ClassLength