From 5afaef427ca0a60c39e238a7b8d0ce6dfed991e0 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 29 Oct 2023 21:24:52 +0100 Subject: [PATCH 1/4] Rename `CommentController` to `CommentsController` --- .../{comment_controller.rb => comments_controller.rb} | 2 +- app/views/{comment => comments}/index.html.haml | 0 app/views/{comment => comments}/show_reactions.html.haml | 0 config/routes.rb | 4 ++-- ...comment_controller_spec.rb => comments_controller_spec.rb} | 2 +- 5 files changed, 4 insertions(+), 4 deletions(-) rename app/controllers/{comment_controller.rb => comments_controller.rb} (91%) rename app/views/{comment => comments}/index.html.haml (100%) rename app/views/{comment => comments}/show_reactions.html.haml (100%) rename spec/controllers/{comment_controller_spec.rb => comments_controller_spec.rb} (97%) diff --git a/app/controllers/comment_controller.rb b/app/controllers/comments_controller.rb similarity index 91% rename from app/controllers/comment_controller.rb rename to app/controllers/comments_controller.rb index b3e12514..4aa92962 100644 --- a/app/controllers/comment_controller.rb +++ b/app/controllers/comments_controller.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class CommentController < ApplicationController +class CommentsController < ApplicationController def index answer = Answer.find(params[:id]) @comments = Comment.where(answer:).includes([{ user: :profile }, :smiles]) diff --git a/app/views/comment/index.html.haml b/app/views/comments/index.html.haml similarity index 100% rename from app/views/comment/index.html.haml rename to app/views/comments/index.html.haml diff --git a/app/views/comment/show_reactions.html.haml b/app/views/comments/show_reactions.html.haml similarity index 100% rename from app/views/comment/show_reactions.html.haml rename to app/views/comments/show_reactions.html.haml diff --git a/config/routes.rb b/config/routes.rb index 7e5e259c..dde406e3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -154,10 +154,10 @@ Rails.application.routes.draw do get "/@:username/a/:id", to: "answer#show", as: :answer post "/@:username/a/:id/pin", to: "answer#pin", as: :pin_answer delete "/@:username/a/:id/pin", to: "answer#unpin", as: :unpin_answer - get "/@:username/a/:id/comments", to: "comment#index", as: :comments + 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: "comment#show_reactions", as: :comment_reactions + get "/@:username/c/:id/reactions", to: "comments#show_reactions", 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/comment_controller_spec.rb b/spec/controllers/comments_controller_spec.rb similarity index 97% rename from spec/controllers/comment_controller_spec.rb rename to spec/controllers/comments_controller_spec.rb index 63812485..1140f1f3 100644 --- a/spec/controllers/comment_controller_spec.rb +++ b/spec/controllers/comments_controller_spec.rb @@ -2,7 +2,7 @@ require "rails_helper" -describe CommentController, type: :controller do +describe CommentsController, type: :controller do describe "#index" do shared_examples_for "succeeds" do it "returns the correct response" do From e8e833f9bdbef9da4c2f3b9baf0033495d5c1fa6 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 29 Oct 2023 21:32:39 +0100 Subject: [PATCH 2/4] 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 From 91716454c3bbb5d3155539b045cc35a57b80a21e Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 29 Oct 2023 21:34:13 +0100 Subject: [PATCH 3/4] Render correct template in spec --- spec/controllers/comments_controller_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/controllers/comments_controller_spec.rb b/spec/controllers/comments_controller_spec.rb index 82145dcc..209c1886 100644 --- a/spec/controllers/comments_controller_spec.rb +++ b/spec/controllers/comments_controller_spec.rb @@ -7,7 +7,7 @@ describe CommentsController, type: :controller do shared_examples_for "succeeds" do it "returns the correct response" do subject - expect(response).to have_rendered("comment/index") + expect(response).to have_rendered :index expect(response).to have_http_status(200) expect(assigns(:comments)).to eq(comments) expect(assigns(:comments)).to_not include(unrelated_comment) From 5d093c621bfeeeea55a67ecb18954fdc7b9eeb0e Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 29 Oct 2023 21:48:41 +0100 Subject: [PATCH 4/4] Rename `#show` to `#index` action for comment reactions --- app/controllers/comments/reactions_controller.rb | 2 +- .../reactions/{show.html.haml => index.html.haml} | 0 config/routes.rb | 2 +- spec/controllers/comments/reactions_controller_spec.rb | 10 +++++----- 4 files changed, 7 insertions(+), 7 deletions(-) rename app/views/comments/reactions/{show.html.haml => index.html.haml} (100%) diff --git a/app/controllers/comments/reactions_controller.rb b/app/controllers/comments/reactions_controller.rb index 11ca6726..0efe2c2a 100644 --- a/app/controllers/comments/reactions_controller.rb +++ b/app/controllers/comments/reactions_controller.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class Comments::ReactionsController < ApplicationController - def show + def index comment = Comment.find(params[:id]) @reactions = Reaction.where(parent_type: "Comment", parent: comment.id).includes([{ user: :profile }]) diff --git a/app/views/comments/reactions/show.html.haml b/app/views/comments/reactions/index.html.haml similarity index 100% rename from app/views/comments/reactions/show.html.haml rename to app/views/comments/reactions/index.html.haml diff --git a/config/routes.rb b/config/routes.rb index 906e2ff6..57e9e243 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/reactions#show", as: :comment_reactions + get "/@:username/c/:id/reactions", to: "comments/reactions#index", 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 index 2c4dbb4e..083b89b5 100644 --- a/spec/controllers/comments/reactions_controller_spec.rb +++ b/spec/controllers/comments/reactions_controller_spec.rb @@ -3,14 +3,14 @@ require "rails_helper" describe Comments::ReactionsController, type: :controller do - describe "#show" do + describe "#index" 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 } } + subject { get :index, params: { username: commenter.screen_name, id: comment.id } } it "should redirect to the answer page" do subject @@ -20,14 +20,14 @@ describe Comments::ReactionsController, type: :controller do end context "a Turbo Frame request" do - subject { get :show, params: { username: commenter.screen_name, id: comment.id } } + subject { get :index, params: { username: commenter.screen_name, id: comment.id } } - it "renders the show_reaction template" do + it "renders the index template" do @request.headers["Turbo-Frame"] = "some_id" subject - expect(response).to render_template(:show) + expect(response).to render_template(:index) end end end