From e8e833f9bdbef9da4c2f3b9baf0033495d5c1fa6 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 29 Oct 2023 21:32:39 +0100 Subject: [PATCH] Move comment reactions into own controller --- .../comments/reactions_controller.rb | 10 ++++++ app/controllers/comments_controller.rb | 7 ---- .../show.html.haml} | 0 config/routes.rb | 2 +- .../comments/reactions_controller_spec.rb | 34 +++++++++++++++++++ spec/controllers/comments_controller_spec.rb | 29 ---------------- 6 files changed, 45 insertions(+), 37 deletions(-) create mode 100644 app/controllers/comments/reactions_controller.rb rename app/views/comments/{show_reactions.html.haml => reactions/show.html.haml} (100%) create mode 100644 spec/controllers/comments/reactions_controller_spec.rb diff --git a/app/controllers/comments/reactions_controller.rb b/app/controllers/comments/reactions_controller.rb new file mode 100644 index 00000000..11ca6726 --- /dev/null +++ b/app/controllers/comments/reactions_controller.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class Comments::ReactionsController < ApplicationController + def show + comment = Comment.find(params[:id]) + @reactions = Reaction.where(parent_type: "Comment", parent: comment.id).includes([{ user: :profile }]) + + redirect_to answer_path(username: comment.answer.user.screen_name, id: comment.answer.id) unless turbo_frame_request? + end +end diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 4aa92962..25f4b435 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -7,11 +7,4 @@ class CommentsController < ApplicationController render "index", locals: { a: answer } end - - def show_reactions - comment = Comment.find(params[:id]) - @reactions = Reaction.where(parent_type: "Comment", parent: comment.id).includes([{ user: :profile }]) - - redirect_to answer_path(username: comment.answer.user.screen_name, id: comment.answer.id) unless turbo_frame_request? - end end diff --git a/app/views/comments/show_reactions.html.haml b/app/views/comments/reactions/show.html.haml similarity index 100% rename from app/views/comments/show_reactions.html.haml rename to app/views/comments/reactions/show.html.haml diff --git a/config/routes.rb b/config/routes.rb index dde406e3..906e2ff6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -157,7 +157,7 @@ Rails.application.routes.draw do get "/@:username/a/:id/comments", to: "comments#index", as: :comments get "/@:username/a/:id/reactions", to: "reaction#index", as: :reactions get "/@:username/q/:id", to: "question#show", as: :question - get "/@:username/c/:id/reactions", to: "comments#show_reactions", as: :comment_reactions + get "/@:username/c/:id/reactions", to: "comments/reactions#show", as: :comment_reactions get "/@:username/followers", to: "user#followers", as: :show_user_followers get "/@:username/followings", to: "user#followings", as: :show_user_followings get "/@:username/friends", to: redirect("/@%{username}/followings") diff --git a/spec/controllers/comments/reactions_controller_spec.rb b/spec/controllers/comments/reactions_controller_spec.rb new file mode 100644 index 00000000..2c4dbb4e --- /dev/null +++ b/spec/controllers/comments/reactions_controller_spec.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require "rails_helper" + +describe Comments::ReactionsController, type: :controller do + describe "#show" do + let(:answer_author) { FactoryBot.create(:user) } + let(:answer) { FactoryBot.create(:answer, user: answer_author) } + let(:commenter) { FactoryBot.create(:user) } + let(:comment) { FactoryBot.create(:comment, answer:, user: commenter) } + + context "a regular web navigation request" do + subject { get :show, params: { username: commenter.screen_name, id: comment.id } } + + it "should redirect to the answer page" do + subject + + expect(response).to redirect_to answer_path(username: answer_author.screen_name, id: answer.id) + end + end + + context "a Turbo Frame request" do + subject { get :show, params: { username: commenter.screen_name, id: comment.id } } + + it "renders the show_reaction template" do + @request.headers["Turbo-Frame"] = "some_id" + + subject + + expect(response).to render_template(:show) + end + end + end +end diff --git a/spec/controllers/comments_controller_spec.rb b/spec/controllers/comments_controller_spec.rb index 1140f1f3..82145dcc 100644 --- a/spec/controllers/comments_controller_spec.rb +++ b/spec/controllers/comments_controller_spec.rb @@ -34,33 +34,4 @@ describe CommentsController, type: :controller do end end end - - describe "#show_reactions" do - let(:answer_author) { FactoryBot.create(:user) } - let(:answer) { FactoryBot.create(:answer, user: answer_author) } - let(:commenter) { FactoryBot.create(:user) } - let(:comment) { FactoryBot.create(:comment, answer:, user: commenter) } - - context "a regular web navigation request" do - subject { get :show_reactions, params: { username: commenter.screen_name, id: comment.id } } - - it "should redirect to the answer page" do - subject - - expect(response).to redirect_to answer_path(username: answer_author.screen_name, id: answer.id) - end - end - - context "a Turbo Frame request" do - subject { get :show_reactions, params: { username: commenter.screen_name, id: comment.id } } - - it "renders the show_reaction template" do - @request.headers["Turbo-Frame"] = "some_id" - - subject - - expect(response).to render_template(:show_reactions) - end - end - end end